summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2019-11-25 15:37:55 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2019-11-29 10:58:11 (GMT)
commit95a159eb7a94713fe22c67244104c80ffdb3ceaa (patch)
tree35caa5fbb4047f0571308ae3d35b3455e9aa6999
parent9d5fc48e09379afced2f2cb41e8533c88a61c7e1 (diff)
downloadcombo-95a159eb7a94713fe22c67244104c80ffdb3ceaa.zip
combo-95a159eb7a94713fe22c67244104c80ffdb3ceaa.tar.gz
combo-95a159eb7a94713fe22c67244104c80ffdb3ceaa.tar.bz2
dataviz: display a single decimal in percent graphs/tables (#37928)
-rw-r--r--combo/apps/dataviz/models.py5
-rw-r--r--tests/test_dataviz.py33
2 files changed, 37 insertions, 1 deletions
diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py
index 65cbe68..f2ab3e1 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 21f2c27..65fb924 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 '<td>10.0%</td>' 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)'),
]