dataviz: translate received x_labels for months (#62530)

This commit is contained in:
Valentin Deniaud 2022-03-15 17:19:50 +01:00
parent 73e345a89d
commit d54a69c423
2 changed files with 24 additions and 6 deletions

View File

@ -377,12 +377,13 @@ class ChartNgCell(CellBase):
data = response['data']
interval = self.filter_params.get('time_interval', '')
if (
data['x_labels']
and interval
and (interval == 'day' or not self.statistic.has_native_support_for_interval(interval))
):
self.aggregate_data(data, interval)
if data['x_labels'] and interval:
if interval == 'day' or not self.statistic.has_native_support_for_interval(interval):
self.aggregate_data(data, interval)
elif interval == 'month':
data['x_labels'] = [
format_date(datetime.strptime(x, '%Y-%m'), 'M Y') for x in data['x_labels']
]
chart.x_labels = data['x_labels']
chart.axis_count = min(len(data['series']), 2)

View File

@ -2124,6 +2124,23 @@ def test_chartng_cell_new_api_aggregation(new_api_statistics, app, admin_user, n
assert chart.raw_series == [([19], {'title': 'Serie 1'})]
@with_httmock(new_api_mock)
def test_chartng_cell_new_api_month_translation(app, admin_user, new_api_statistics):
page = Page.objects.create(title='One', slug='index')
cell = ChartNgCell(page=page, order=1, placeholder='content')
cell.statistic = Statistic.objects.get(slug='one-serie')
cell.save()
# populate filter params
app = login(app)
resp = app.get('/manage/pages/%s/' % page.id)
resp.form.submit()
cell.refresh_from_db()
chart = cell.get_chart()
assert chart.x_labels == ['Oct 2020', 'Nov 2020', 'Dec 2020']
@with_httmock(new_api_mock)
def test_chart_filters_cell(new_api_statistics, app, admin_user, nocache):
page = Page.objects.create(title='One', slug='index')