From acc36b8cf78adfcedb112569235af751c84bf9c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Thu, 4 Aug 2022 10:32:13 +0200
Subject: [PATCH] dataviz: add table style to invert rows/columns (#67937)

---
 .../dataviz/migrations/0011_auto_20200813_1100.py  |  1 +
 combo/apps/dataviz/models.py                       |  4 +++-
 .../apps/dataviz/templates/combo/chartngcell.html  |  2 +-
 .../dataviz/templates/combo/chartngcell_form.html  |  2 +-
 combo/apps/dataviz/views.py                        | 10 +++++++---
 tests/test_dataviz.py                              | 14 ++++++++++++++
 6 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/combo/apps/dataviz/migrations/0011_auto_20200813_1100.py b/combo/apps/dataviz/migrations/0011_auto_20200813_1100.py
index 160bf9b7..8c231527 100644
--- a/combo/apps/dataviz/migrations/0011_auto_20200813_1100.py
+++ b/combo/apps/dataviz/migrations/0011_auto_20200813_1100.py
@@ -48,6 +48,7 @@ class Migration(migrations.Migration):
                     ('pie', 'Pie'),
                     ('dot', 'Dot'),
                     ('table', 'Table'),
+                    ('table-inverted', 'Table (inverted)'),
                 ],
                 default='bar',
                 max_length=20,
diff --git a/combo/apps/dataviz/models.py b/combo/apps/dataviz/models.py
index a4f36dcc..32f3bb7e 100644
--- a/combo/apps/dataviz/models.py
+++ b/combo/apps/dataviz/models.py
@@ -238,6 +238,7 @@ class ChartNgCell(CellBase):
             ('pie', _('Pie')),
             ('dot', _('Dot')),
             ('table', _('Table')),
+            ('table-inverted', _('Table (inverted)')),
         ),
     )
 
@@ -341,6 +342,7 @@ class ChartNgCell(CellBase):
             'pie': pygal.Pie,
             'dot': pygal.Dot,
             'table': pygal.Bar,
+            'table-inverted': pygal.Bar,
         }[self.chart_type](config=pygal.Config(style=copy.copy(style), order_min=0.1, max_scale=5))
 
         if self.statistic.service_slug == 'bijoe':
@@ -560,7 +562,7 @@ class ChartNgCell(CellBase):
             data = self.hide_values(chart, data)
         if data and self.sort_order != 'none':
             data = self.sort_values(chart, data)
-        if getattr(chart, 'compute_sum', True) and self.chart_type == 'table':
+        if getattr(chart, 'compute_sum', True) and self.chart_type in ('table', 'table-inverted'):
             data = self.add_total_to_line_table(chart, data)
         return data
 
diff --git a/combo/apps/dataviz/templates/combo/chartngcell.html b/combo/apps/dataviz/templates/combo/chartngcell.html
index 97373f11..10e47303 100644
--- a/combo/apps/dataviz/templates/combo/chartngcell.html
+++ b/combo/apps/dataviz/templates/combo/chartngcell.html
@@ -1,6 +1,6 @@
 {% load i18n %}
 {% if cell.title %}<h2>{{cell.title}}</h2>{% endif %}
-{% if cell.chart_type == "table" %}
+{% if cell.chart_type == "table" or cell.chart_type == "table-inverted" %}
 <div id="chart-{{cell.id}}"></div>
 <script>
 $(function() {
diff --git a/combo/apps/dataviz/templates/combo/chartngcell_form.html b/combo/apps/dataviz/templates/combo/chartngcell_form.html
index 34cc8fc4..6dbdb886 100644
--- a/combo/apps/dataviz/templates/combo/chartngcell_form.html
+++ b/combo/apps/dataviz/templates/combo/chartngcell_form.html
@@ -1,6 +1,6 @@
 <div style="position: relative">
 {{ form.as_p }}
-{% if cell.statistic and cell.chart_type != "table" %}
+{% if cell.statistic and cell.chart_type != "table" and cell.chart_type != "table-inverted" %}
 <div style="position: absolute; right: 0; top: 0; width: 300px; height: 150px">
   <embed type="image/svg+xml" src="{% url 'combo-dataviz-graph' cell=cell.id %}?width=300&height=150"/>
 </div>
diff --git a/combo/apps/dataviz/views.py b/combo/apps/dataviz/views.py
index 1b9292d8..8af901bc 100644
--- a/combo/apps/dataviz/views.py
+++ b/combo/apps/dataviz/views.py
@@ -92,12 +92,16 @@ class DatavizGraphView(DetailView):
         if self.filters_cell_id and self.cell.statistic.service_slug != 'bijoe':
             self.update_subfilters_cache(form.instance)
 
-        if self.cell.chart_type == 'table':
+        if self.cell.chart_type in ('table', 'table-inverted'):
             if not chart.raw_series:
                 return self.error(_('No data'))
 
+            transpose = bool(chart.axis_count == 2)  # default behaviour
+            if self.cell.chart_type == 'table-inverted':
+                transpose = not transpose
+
             rendered = chart.render_table(
-                transpose=bool(chart.axis_count == 2) and self.cell.statistic.service_slug == 'bijoe',
+                transpose=transpose,
                 total=getattr(chart, 'compute_sum', True),
             )
             rendered = rendered.replace('<table>', '<table class="main">')
@@ -106,7 +110,7 @@ class DatavizGraphView(DetailView):
         return HttpResponse(chart.render(), content_type='image/svg+xml')
 
     def error(self, error_text):
-        if self.cell.chart_type == 'table':
+        if self.cell.chart_type in ('table', 'table-inverted'):
             return HttpResponse('<p>%s</p>' % error_text)
 
         context = {
diff --git a/tests/test_dataviz.py b/tests/test_dataviz.py
index a32d5949..8ef5b5ca 100644
--- a/tests/test_dataviz.py
+++ b/tests/test_dataviz.py
@@ -1213,6 +1213,11 @@ def test_chartng_cell_view(app, normal_user, statistics):
     resp = app.get(location, status=200)
     assert '<td>222</td>' in resp.text
 
+    cell.chart_type = 'table-inverted'
+    cell.save()
+    resp = app.get(location, status=200)
+    assert '<td>222</td>' in resp.text
+
     # unsupported dataset
     cell.statistic = Statistic.objects.get(slug='seventh')
     cell.save()
@@ -1278,6 +1283,15 @@ def test_chartng_cell_view_new_api(app, normal_user, new_api_statistics):
     cell.save()
     resp = app.get(location, status=200)
     assert '<td>18</td>' in resp.text
+    assert '<th>Serie 1</th>' in resp.text
+    assert '<td>2020-10</td>' in resp.text
+
+    cell.chart_type = 'table-inverted'
+    cell.save()
+    resp = app.get(location, status=200)
+    assert '<td>18</td>' in resp.text
+    assert '<td>Serie 1</td>' in resp.text
+    assert '<th>2020-10</th>' in resp.text
 
     # deleted visualization
     cell.statistic = Statistic.objects.get(slug='not-found')
-- 
2.36.1

