From 95a159eb7a94713fe22c67244104c80ffdb3ceaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 25 Nov 2019 16:37:55 +0100 Subject: [PATCH] dataviz: display a single decimal in percent graphs/tables (#37928) --- combo/apps/dataviz/models.py | 5 ++++- tests/test_dataviz.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py index 65cbe683..f2ab3e17 100644 --- a/combo/apps/dataviz/models.py +++ b/combo/apps/dataviz/models.py @@ -263,7 +263,7 @@ class ChartNgCell(CellBase): chart.add(label, value) chart.show_legend = True - if response.get('unit') == 'seconds': + if response.get('unit') == 'seconds' or response.get('measure') == 'duration': def format_duration(value): if value is None: return '-' @@ -286,5 +286,8 @@ class ChartNgCell(CellBase): value = _('Less than an hour') return force_text(value) chart.config.value_formatter = format_duration + elif response.get('measure') == 'percent': + percent_formatter = lambda x: '{:.1f}%'.format(x) + chart.config.value_formatter = percent_formatter return chart diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py index 21f2c27a..65fb9242 100644 --- a/tests/test_dataviz.py +++ b/tests/test_dataviz.py @@ -100,6 +100,12 @@ VISUALIZATION_JSON = [ 'name': 'nineth visualization (loop over varying dimensions)', 'slug': 'nineth', }, + { + 'data-url': 'https://bijoe.example.com/visualization/10/json/', + 'path': 'https://bijoe.example.com/visualization/10/iframe/?signature=123', + 'name': 'tenth visualization (percents)', + 'slug': 'tenth', + }, ] @@ -194,6 +200,7 @@ def bijoe_mock(url, request): 'y_labels': ['web', 'mail', 'email', 'fax'] }, 'unit': 'seconds', + 'measure': 'duration', } return {'content': json.dumps(response), 'request': request, 'status_code': 200} if url.path == '/visualization/9/json/': @@ -210,6 +217,18 @@ def bijoe_mock(url, request): } } return {'content': json.dumps(response), 'request': request, 'status_code': 200} + if url.path == '/visualization/10/json/': + response = { + 'format': '1', + 'data': [10, 20, 30, 40], + 'axis': { + 'y_labels': ['web', 'mail', 'email', 'fax'] + }, + 'unit': None, + 'measure': 'percent', + } + return {'content': json.dumps(response), 'request': request, 'status_code': 200} + def test_chartng_cell(app): @@ -396,6 +415,19 @@ def test_chartng_cell_view(app, normal_user): assert '>2 hours<' in resp.text assert '>1 day<' in resp.text + # percents + cell.data_reference = 'plop:tenth' + cell.chart_type = 'table' + cell.save() + resp = app.get('/api/dataviz/graph/1/') # get data in cache + resp = app.get('/') + assert '10.0%' in resp.text + + cell.chart_type = 'bar' + cell.save() + resp = app.get('/api/dataviz/graph/1/?width=400', status=200) + assert '>10.0%<' in resp.text + def test_chartng_cell_manager(app, admin_user): page = Page(title='One', slug='index') @@ -420,5 +452,6 @@ def test_chartng_cell_manager(app, admin_user): (u'plop:second', False, u'second visualization (Y)'), (u'plop:seventh', False, u'seventh visualization (loop/X/Y)'), (u'plop:sixth', False, u'sixth visualization (loop/Y)'), + (u'plop:tenth', False, u'third visualization (percents)'), (u'plop:third', False, u'third visualization (X/Y)'), ]