summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2019-03-13 13:32:49 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2019-03-13 13:52:39 (GMT)
commit58187b08955ba52966e80a0bd3d94085f5463326 (patch)
tree5c27a120e563e30ded9a812ad8fd8fb12a39707f
parent647a98ace052642be893fd05c6c4bcea5a45ff42 (diff)
downloadcombo-wip/20771-graphs.zip
combo-wip/20771-graphs.tar.gz
combo-wip/20771-graphs.tar.bz2
pygal experimentswip/20771-graphs
-rw-r--r--combo/apps/dataviz/templates/combo/dataviz-chart.html2
-rw-r--r--combo/apps/dataviz/views.py61
-rw-r--r--setup.py1
3 files changed, 20 insertions, 44 deletions
diff --git a/combo/apps/dataviz/templates/combo/dataviz-chart.html b/combo/apps/dataviz/templates/combo/dataviz-chart.html
index f2a9b9d..d2f822d 100644
--- a/combo/apps/dataviz/templates/combo/dataviz-chart.html
+++ b/combo/apps/dataviz/templates/combo/dataviz-chart.html
@@ -1,2 +1,2 @@
{% if title %}<h2>{{title}}</h2>{% endif %}
-<img src="{% url 'combo-dataviz-graph' cell=cell.id %}">
+<embed src="{% url 'combo-dataviz-graph' cell=cell.id %}" type="image/svg+xml"/>
diff --git a/combo/apps/dataviz/views.py b/combo/apps/dataviz/views.py
index b70bc23..2efc769 100644
--- a/combo/apps/dataviz/views.py
+++ b/combo/apps/dataviz/views.py
@@ -19,6 +19,8 @@ matplotlib.use('agg')
import numpy as np
import matplotlib.pyplot as plt
+import pygal
+
from django.http import HttpResponse
from django.utils.six import BytesIO
@@ -40,57 +42,33 @@ def dataviz_graph(request, *args, **kwargs):
response = requests.get(cell.url).json()
# XXX: handling of visualization.loop (subplots?)
- # plot
- plt.style.use('bmh')
- fig, ax = plt.subplots(figsize=(8, 4))
-
if True:
# barcharts
x_labels = response['axis'].get('x_labels') or []
y_labels = response['axis'].get('y_labels') or []
+
if not x_labels:
x_labels = y_labels
y_labels = ['']
elif not y_labels:
y_labels = ['']
- n_groups = len(x_labels)
- index = np.arange(n_groups)
- bar_width = 0.8 / len(y_labels)
- STACKED = True
- if STACKED:
- bar_width = 0.8
+ #chart = pygal.Line()
+ #chart = pygal.Bar()
+ chart = pygal.StackedBar()
+ chart = pygal.Dot(show_legend=False)
+ chart.config.height = 300
+ chart.config.explicit_size = True
+ chart.config.js = ['/static/js/pygal-tooltips.min.js']
+ chart.x_labels = x_labels
- bottom = np.array([0] * len(x_labels))
for i, serie_label in enumerate(y_labels):
if len(response['axis']) == 1:
values = response['data']
else:
values = [response['data'][i][j] for j in range(len(x_labels))]
- if True:
- if not STACKED:
- ax.bar(index + bar_width*i, values, bar_width,
- label=serie_label if len(y_labels) > 1 else None,
- bottom=bottom)
- else:
- ax.bar(index+0.25, values, bar_width,
- label=serie_label if len(y_labels) > 1 else None,
- bottom=bottom)
- bottom = bottom + np.array(values)
- else:
- # plot
- ax.plot(index + bar_width*i, values,
- label=serie_label if len(y_labels) > 1 else None,
- marker='o')
- if len(response['axis']) > 1:
- ax.legend()
- ax.set_xticks(index + 0.25)
- else:
- ax.set_xticks(index)
+ chart.add(serie_label, values)
- ax.set_xticklabels(x_labels)
- ax.set_axisbelow(True)
- ax.grid()
elif False:
# heatmap (ex: weekday/hours)
data = response['data']
@@ -116,13 +94,10 @@ def dataviz_graph(request, *args, **kwargs):
x_labels = y_labels
values = response['data']
- x_labels = [x for x, y in zip(x_labels, values) if y]
- values = [x for x in values if x]
-
- ax.pie(values, labels=x_labels, counterclock=False, startangle=90)
- ax.axis('equal')
+ chart = pygal.Pie()
+ for label, value in zip(x_labels, values):
+ if not value:
+ continue
+ chart.add(label, value)
- fig.tight_layout()
- buffer_ = BytesIO()
- fig.savefig(buffer_, transparent=True, format='png', bbox_inches="tight")
- return HttpResponse(buffer_.getvalue(), content_type='image/png')
+ return HttpResponse(chart.render(), content_type='image/svg+xml')
diff --git a/setup.py b/setup.py
index f985382..573889b 100644
--- a/setup.py
+++ b/setup.py
@@ -167,6 +167,7 @@ setup(
'Pillow',
'pyproj',
'matplotlib<3',
+ 'pygal',
],
zip_safe=False,
cmdclass={