superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maximebeauche...@apache.org
Subject [incubator-superset] branch master updated: [Viz] transpose pivot table (#7325)
Date Mon, 01 Jul 2019 22:44:08 GMT
This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 459276f  [Viz] transpose pivot table (#7325)
459276f is described below

commit 459276f00d87b3368366941d781baf9ef2acf1f1
Author: Yongjie Zhao <yongjie.zhao@gmail.com>
AuthorDate: Tue Jul 2 06:44:01 2019 +0800

    [Viz] transpose pivot table (#7325)
---
 .../assets/src/explore/controlPanels/PivotTable.js |  1 +
 superset/assets/src/explore/controls.jsx           |  7 +++++++
 superset/viz.py                                    | 22 ++++++++++++++++------
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/superset/assets/src/explore/controlPanels/PivotTable.js b/superset/assets/src/explore/controlPanels/PivotTable.js
index 71e754e..38b1078 100644
--- a/superset/assets/src/explore/controlPanels/PivotTable.js
+++ b/superset/assets/src/explore/controlPanels/PivotTable.js
@@ -36,6 +36,7 @@ export default {
       controlSetRows: [
         ['pandas_aggfunc', 'pivot_margins'],
         ['number_format', 'combine_metric'],
+        ['transpose_pivot'],
       ],
     },
   ],
diff --git a/superset/assets/src/explore/controls.jsx b/superset/assets/src/explore/controls.jsx
index feba265..2ae104b 100644
--- a/superset/assets/src/explore/controls.jsx
+++ b/superset/assets/src/explore/controls.jsx
@@ -471,6 +471,13 @@ export const controls = {
     description: t('Display total row/column'),
   },
 
+  transpose_pivot: {
+    type: 'CheckboxControl',
+    label: t('Transpose Pivot'),
+    default: false,
+    description: t('Swap Groups and Columns'),
+  },
+
   show_markers: {
     type: 'CheckboxControl',
     label: t('Show Markers'),
diff --git a/superset/viz.py b/superset/viz.py
index a9a1aa0..7404a7f 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -654,15 +654,21 @@ class PivotTableViz(BaseViz):
 
     def query_obj(self):
         d = super().query_obj()
-        groupby = self.form_data.get("groupby")
-        columns = self.form_data.get("columns")
-        metrics = self.form_data.get("metrics")
+        groupby = self.form_data.get('groupby')
+        columns = self.form_data.get('columns')
+        metrics = self.form_data.get('metrics')
+        transpose = self.form_data.get('transpose_pivot')
         if not columns:
             columns = []
         if not groupby:
             groupby = []
         if not groupby:
             raise Exception(_("Please choose at least one 'Group by' field "))
+        if transpose and not columns:
+            raise Exception(_((
+                "Please choose at least one 'Columns' field when "
+                "select 'Transpose Pivot' option"
+            )))
         if not metrics:
             raise Exception(_("Please choose at least one metric"))
         if any(v in groupby for v in columns) or any(v in columns for v in groupby):
@@ -679,10 +685,14 @@ class PivotTableViz(BaseViz):
         if aggfunc == "sum":
             aggfunc = lambda x: x.sum(min_count=1)  # noqa: E731
 
+        groupby = self.form_data.get('groupby')
+        columns = self.form_data.get('columns')
+        if self.form_data.get('transpose_pivot'):
+            groupby, columns = columns, groupby
         df = df.pivot_table(
-            index=self.form_data.get("groupby"),
-            columns=self.form_data.get("columns"),
-            values=[utils.get_metric_name(m) for m in self.form_data.get("metrics")],
+            index=groupby,
+            columns=columns,
+            values=[utils.get_metric_name(m) for m in self.form_data.get('metrics')],
             aggfunc=aggfunc,
             margins=self.form_data.get("pivot_margins"),
             dropna=False,


Mime
View raw message