summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2019-03-06 16:47:32 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2019-03-13 13:52:39 (GMT)
commit9bb1721644f23d30b16cde389108c3959b9d8319 (patch)
treec3b9c6bef3e70a1e27f97c561c38b47c335719e7
parent4c36790c30e738caf7fe25408e3719b2b8df999c (diff)
downloadcombo-9bb1721644f23d30b16cde389108c3959b9d8319.zip
combo-9bb1721644f23d30b16cde389108c3959b9d8319.tar.gz
combo-9bb1721644f23d30b16cde389108c3959b9d8319.tar.bz2
adapt experiments to alternative bijoe response format
-rw-r--r--combo/apps/dataviz/views.py66
1 files changed, 32 insertions, 34 deletions
diff --git a/combo/apps/dataviz/views.py b/combo/apps/dataviz/views.py
index 661af9f..fc9290d 100644
--- a/combo/apps/dataviz/views.py
+++ b/combo/apps/dataviz/views.py
@@ -37,43 +37,32 @@ def dataviz_graph(request, *args, **kwargs):
cell = ChartCell.objects.get(id=kwargs.get('cell'))
# XXX: security
- data = requests.get(cell.url).json()['data'][0]
+ response = requests.get(cell.url).json()
# XXX: handling of visualization.loop (subplots?)
- # create table with data
- data_table = {}
- x_labels = []
- y_labels = []
-
- for row in data:
- x_label = row[0]['value']
- if not x_label in x_labels:
- x_labels.append(x_label)
- if not x_label in data_table:
- data_table[x_label] = {}
- if len(row) == 3:
- y_label = row[1]['value']
- else:
- y_label = '/'
- if not y_label in y_labels:
- y_labels.append(y_label)
- if not y_label in data_table[x_label]:
- data_table[x_label][y_label] = {}
- data_table.setdefault(x_label, {})[y_label] = float(row[-1]['value']) if row[-1]['value'] else 0
-
# plot
plt.style.use('bmh')
fig, ax = plt.subplots(figsize=(8, 4))
- n_groups = len(x_labels)
- index = np.arange(n_groups)
- bar_width = 0.8 / len(y_labels)
-
if False:
# 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)
+
for i, serie_label in enumerate(y_labels):
- values = [data_table[x][serie_label] for x in x_labels]
- if True:
+ if len(response['axis']) == 1:
+ values = response['data']
+ else:
+ values = [response['data'][i][j] for j in range(len(x_labels))]
+ if False:
ax.bar(index + bar_width*i, values, bar_width,
label=serie_label if len(y_labels) > 1 else None)
else:
@@ -81,7 +70,7 @@ def dataviz_graph(request, *args, **kwargs):
ax.plot(index + bar_width*i, values,
label=serie_label if len(y_labels) > 1 else None,
marker='o')
- if len(y_labels) > 1:
+ if len(response['axis']) > 1:
ax.legend()
ax.set_xticks(index + 0.25)
else:
@@ -92,7 +81,9 @@ def dataviz_graph(request, *args, **kwargs):
ax.grid()
elif False:
# heatmap (ex: weekday/hours)
- data = [[data_table[x][serie_label] for x in x_labels] for serie_label in y_labels]
+ data = response['data']
+ x_labels = response['axis'].get('x_labels')
+ y_labels = response['axis'].get('y_labels')
ax.imshow(data)
ax.set_xticks(np.arange(len(x_labels)))
ax.set_yticks(np.arange(len(y_labels)))
@@ -107,10 +98,17 @@ def dataviz_graph(request, *args, **kwargs):
ax.grid()
else:
# pie
- for i, serie_label in enumerate(y_labels):
- values = [data_table[x][serie_label] for x in x_labels]
- ax.pie(values, labels=x_labels, counterclock=False, startangle=90)
- ax.axis('equal')
+ x_labels = response['axis'].get('x_labels') or []
+ y_labels = response['axis'].get('y_labels') or []
+ if not x_labels:
+ 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')
fig.tight_layout()
buffer_ = BytesIO()