From 1fd772460665d1af33e4106ac1b6d9b1d0a2c561 Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Wed, 8 Jan 2020 11:36:26 +0100 Subject: [PATCH] dataviz: display total in tables (#37903) --- combo/apps/dataviz/models.py | 9 ++++++ tests/test_dataviz.py | 57 +++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 4224c93..336293b 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -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('', '
') 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('Total') + if self.chart_type != 'pie': for i, serie_label in enumerate(y_labels): if chart.axis_count < 2: diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 648516c..782dbcb 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -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/': @@ -455,3 +459,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 -- 2.20.1