dataviz: display a single decimal in percent graphs/tables (#37928)

This commit is contained in:
Frédéric Péters 2019-11-25 16:37:55 +01:00
parent 9d5fc48e09
commit 95a159eb7a
2 changed files with 37 additions and 1 deletions

View File

@ -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

View File

@ -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)'),
]