dataviz: display total in tables (#37903)

This commit is contained in:
Valentin Deniaud 2020-01-08 11:36:26 +01:00
parent 6ab914d714
commit 7bc26d0365
2 changed files with 63 additions and 5 deletions

View File

@ -20,7 +20,7 @@ import os
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _, ungettext
from django.utils.translation import ugettext_lazy as _, ungettext, gettext
from django.conf import settings
from jsonfield import JSONField
@ -169,6 +169,7 @@ class ChartNgCell(CellBase):
else:
ctx['table'] = chart.render_table(
transpose=bool(chart.axis_count == 2),
total=chart.compute_sum,
)
ctx['table'] = ctx['table'].replace('<table>', '<table class="main">')
return ctx
@ -248,6 +249,14 @@ class ChartNgCell(CellBase):
# use a single colour for dots
chart.config.style.colors = ('#1f77b4',) * len(x_labels)
chart.compute_sum = bool(response.get('measure') == 'integer')
if chart.compute_sum and self.chart_type == 'table':
if chart.axis_count < 2: # workaround pygal
chart.compute_sum = False
if chart.axis_count == 1:
data.append(sum(data))
x_labels.append(gettext('Total'))
if self.chart_type != 'pie':
for i, serie_label in enumerate(y_labels):
if chart.axis_count < 2:

View File

@ -118,7 +118,8 @@ def bijoe_mock(url, request):
'data': [222, 134, 53],
'axis': {
'x_labels': ['web', 'mail', 'email']
}
},
'measure': 'integer',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/2/json/':
@ -127,7 +128,8 @@ def bijoe_mock(url, request):
'data': [222, 134, 53],
'axis': {
'y_labels': ['web', 'mail', 'email']
}
},
'measure': 'integer',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/3/json/':
@ -140,14 +142,16 @@ def bijoe_mock(url, request):
'axis': {
'x_labels': ['web', 'mail', 'email'],
'y_labels': ['foo', 'bar'],
}
},
'measure': 'integer',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/4/json/':
response = {
'format': '1',
'data': 222,
'axis': {}
'axis': {},
'measure': 'integer',
}
return {'content': json.dumps(response), 'request': request, 'status_code': 200}
if url.path == '/visualization/5/json/':
@ -463,3 +467,48 @@ def test_chartng_cell_manager(app, admin_user):
(u'plop:tenth', False, u'tenth visualization (percents)'),
(u'plop:third', False, u'third visualization (X/Y)'),
]
def test_table_cell(app, admin_user):
page = Page(title='One', slug='index')
page.save()
app = login(app)
with override_settings(KNOWN_SERVICES={
'bijoe': {'plop': {'title': 'test', 'url': 'https://bijoe.example.com',
'secret': 'combo', 'orig': 'combo'}}}):
with HTTMock(bijoe_mock):
cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.data_reference = 'plop:example'
cell.chart_type = 'table'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
resp = app.get('/')
assert resp.text.count('Total') == 1
cell.data_reference = 'plop:second'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
resp = app.get('/')
assert resp.text.count('Total') == 1
cell.data_reference = 'plop:third'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
resp = app.get('/')
assert '114' in resp.text
assert resp.text.count('Total') == 2
cell.data_reference = 'plop:fourth'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
resp = app.get('/')
assert resp.text.count('Total') == 0
# total of durations is not computed
cell.data_reference = 'plop:eigth'
cell.save()
resp = app.get('/api/dataviz/graph/1/')
resp = app.get('/')
assert resp.text.count('Total') == 0