You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by vi...@apache.org on 2020/09/05 19:36:19 UTC
[incubator-superset] 21/26: feat(viz-plugins): add date formatting
to pivot-table (#10637)
This is an automated email from the ASF dual-hosted git repository.
villebro pushed a commit to branch 0.37
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git
commit df1deb2149a9d6b50aea9cffaf80838149de1b22
Author: Ville Brofeldt <33...@users.noreply.github.com>
AuthorDate: Wed Aug 19 23:55:59 2020 +0300
feat(viz-plugins): add date formatting to pivot-table (#10637)
* feat: make pivot table dates formattable
* Bump npm packages
---
superset-frontend/package-lock.json | 12 ++++++------
superset-frontend/package.json | 4 ++--
superset/viz.py | 17 +++++++++++++++--
3 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/superset-frontend/package-lock.json b/superset-frontend/package-lock.json
index 54a5fc6..4125e20 100644
--- a/superset-frontend/package-lock.json
+++ b/superset-frontend/package-lock.json
@@ -6024,9 +6024,9 @@
}
},
"@superset-ui/legacy-plugin-chart-pivot-table": {
- "version": "0.14.14",
- "resolved": "https://registry.npmjs.org/@superset-ui/legacy-plugin-chart-pivot-table/-/legacy-plugin-chart-pivot-table-0.14.14.tgz",
- "integrity": "sha512-+Dkzwv9yfiix6/u090RN2U2VF0Axa7sB+dB57UU5IS3zW1/jxW7e5ewEV2LsmcXQ0d87bWXvhUn+IGagkwx7Wg==",
+ "version": "0.14.21",
+ "resolved": "https://registry.npmjs.org/@superset-ui/legacy-plugin-chart-pivot-table/-/legacy-plugin-chart-pivot-table-0.14.21.tgz",
+ "integrity": "sha512-gmj3iu+ibkXwshcSna1V9Tmbh+wBCHi3HKTuy6R9KrB+0585U0dsHro3xe0o14Uamhld6PIeWbZBSl3axXK+SQ==",
"requires": {
"d3": "^3.5.17",
"datatables.net-bs": "^1.10.15",
@@ -6130,9 +6130,9 @@
}
},
"@superset-ui/legacy-preset-chart-nvd3": {
- "version": "0.14.17",
- "resolved": "https://registry.npmjs.org/@superset-ui/legacy-preset-chart-nvd3/-/legacy-preset-chart-nvd3-0.14.17.tgz",
- "integrity": "sha512-n5mkaO9bqNcN2uSpXASIq3z6WmgajVxE6NV6qtDGOCGDlTnSylwfPVNVURdBhg2mfJT+mFeuYfx54xyn7s20sg==",
+ "version": "0.14.21",
+ "resolved": "https://registry.npmjs.org/@superset-ui/legacy-preset-chart-nvd3/-/legacy-preset-chart-nvd3-0.14.21.tgz",
+ "integrity": "sha512-BbsVZnkkAL2a44XFYQtc24VNINGM5JwXAA9HbygdspumYTUu6cpH2nFVPwc06NREUeeN+EV/zF/AVW2O1IJ1tg==",
"requires": {
"@data-ui/xy-chart": "^0.0.84",
"d3": "^3.5.17",
diff --git a/superset-frontend/package.json b/superset-frontend/package.json
index 51bb284..0ef4a90 100644
--- a/superset-frontend/package.json
+++ b/superset-frontend/package.json
@@ -80,7 +80,7 @@
"@superset-ui/legacy-plugin-chart-paired-t-test": "^0.14.9",
"@superset-ui/legacy-plugin-chart-parallel-coordinates": "^0.14.9",
"@superset-ui/legacy-plugin-chart-partition": "^0.14.9",
- "@superset-ui/legacy-plugin-chart-pivot-table": "^0.14.14",
+ "@superset-ui/legacy-plugin-chart-pivot-table": "^0.14.21",
"@superset-ui/legacy-plugin-chart-rose": "^0.14.14",
"@superset-ui/legacy-plugin-chart-sankey": "^0.14.9",
"@superset-ui/legacy-plugin-chart-sankey-loop": "^0.14.9",
@@ -89,7 +89,7 @@
"@superset-ui/legacy-plugin-chart-world-map": "^0.14.16",
"@superset-ui/legacy-preset-chart-big-number": "^0.14.9",
"@superset-ui/legacy-preset-chart-deckgl": "^0.2.4",
- "@superset-ui/legacy-preset-chart-nvd3": "^0.14.17",
+ "@superset-ui/legacy-preset-chart-nvd3": "^0.14.21",
"@superset-ui/number-format": "^0.14.9",
"@superset-ui/plugin-chart-table": "^0.14.16",
"@superset-ui/plugin-chart-word-cloud": "^0.14.9",
diff --git a/superset/viz.py b/superset/viz.py
index 508d8aa..df1111a 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -72,6 +72,7 @@ from superset.utils.core import (
QueryMode,
to_adhoc,
)
+from superset.utils.dates import datetime_to_epoch
from superset.utils.hashing import md5_sha_from_str
if TYPE_CHECKING:
@@ -825,8 +826,20 @@ class PivotTableViz(BaseViz):
for metric in metrics:
aggfuncs[metric] = self.get_aggfunc(metric, df, self.form_data)
- groupby = self.form_data.get("groupby")
- columns = self.form_data.get("columns")
+ groupby = self.form_data.get("groupby") or []
+ columns = self.form_data.get("columns") or []
+
+ def _format_datetime(value: Any) -> Optional[str]:
+ if isinstance(value, str):
+ return f"__timestamp:{datetime_to_epoch(pd.Timestamp(value))}"
+ return None
+
+ for column_name in groupby + columns:
+ column = self.datasource.get_column(column_name)
+ if column and column.type in ("DATE", "DATETIME", "TIMESTAMP"):
+ ts = df[column_name].apply(_format_datetime)
+ df[column_name] = ts
+
if self.form_data.get("transpose_pivot"):
groupby, columns = columns, groupby