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 2021/01/25 13:12:08 UTC

[superset] 32/38: fix(multiline): return all chart data on initial request (#12660)

This is an automated email from the ASF dual-hosted git repository.

villebro pushed a commit to branch 1.0
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 0686a1c347d65f23515233e692a4bb0c13630f87
Author: Ville Brofeldt <33...@users.noreply.github.com>
AuthorDate: Sat Jan 23 09:01:50 2021 +0200

    fix(multiline): return all chart data on initial request (#12660)
    
    * fix(multiline): return chart data on data request
    
    * bump package
    
    * optimize chart retrieval and fix chart form_data
---
 superset-frontend/package-lock.json |  6 ++--
 superset-frontend/package.json      |  2 +-
 superset/viz.py                     | 71 +++++++++++++++++++++++++++++--------
 3 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/superset-frontend/package-lock.json b/superset-frontend/package-lock.json
index 2bd8b2b..779c3ac 100644
--- a/superset-frontend/package-lock.json
+++ b/superset-frontend/package-lock.json
@@ -19011,9 +19011,9 @@
       }
     },
     "@superset-ui/legacy-preset-chart-nvd3": {
-      "version": "0.16.9",
-      "resolved": "https://registry.npmjs.org/@superset-ui/legacy-preset-chart-nvd3/-/legacy-preset-chart-nvd3-0.16.9.tgz",
-      "integrity": "sha512-wfbVZOGqIk/IFUnzW0k44t9N/iHd0VoJzHT6wwM+GgGcCm5mizBDWot9Zuu4U1gf1KLfKUD7/lwEEtyCrs2zXw==",
+      "version": "0.16.10",
+      "resolved": "https://registry.npmjs.org/@superset-ui/legacy-preset-chart-nvd3/-/legacy-preset-chart-nvd3-0.16.10.tgz",
+      "integrity": "sha512-zQPybEGYfthiUpwSOV4E1YUWnSqWY6S4nGXR8rVh2FIUFzyYyr4f5ZzOr6DKmytPlQbL1oXg0m35A8boshOh0g==",
       "requires": {
         "@data-ui/xy-chart": "^0.0.84",
         "@superset-ui/chart-controls": "0.16.9",
diff --git a/superset-frontend/package.json b/superset-frontend/package.json
index c13d59d..16b83d0 100644
--- a/superset-frontend/package.json
+++ b/superset-frontend/package.json
@@ -88,7 +88,7 @@
     "@superset-ui/legacy-plugin-chart-world-map": "^0.16.9",
     "@superset-ui/legacy-preset-chart-big-number": "^0.16.9",
     "@superset-ui/legacy-preset-chart-deckgl": "^0.4.1",
-    "@superset-ui/legacy-preset-chart-nvd3": "^0.16.9",
+    "@superset-ui/legacy-preset-chart-nvd3": "^0.16.10",
     "@superset-ui/plugin-chart-echarts": "^0.16.9",
     "@superset-ui/plugin-chart-table": "^0.16.9",
     "@superset-ui/plugin-chart-word-cloud": "^0.16.9",
diff --git a/superset/viz.py b/superset/viz.py
index 21fbdf8..d8d799d 100644
--- a/superset/viz.py
+++ b/superset/viz.py
@@ -1405,21 +1405,64 @@ class MultiLineViz(NVD3Viz):
         return {}
 
     def get_data(self, df: pd.DataFrame) -> VizData:
-        fd = self.form_data
-        # Late imports to avoid circular import issues
-        from superset import db
-        from superset.models.slice import Slice
-
-        slice_ids1 = fd.get("line_charts")
-        slices1 = db.session.query(Slice).filter(Slice.id.in_(slice_ids1)).all()
-        slice_ids2 = fd.get("line_charts_2")
-        slices2 = db.session.query(Slice).filter(Slice.id.in_(slice_ids2)).all()
-        return {
-            "slices": {
-                "axis1": [slc.data for slc in slices1],
-                "axis2": [slc.data for slc in slices2],
-            }
+        multiline_fd = self.form_data
+        # Late import to avoid circular import issues
+        from superset.charts.dao import ChartDAO
+
+        axis1_chart_ids = multiline_fd.get("line_charts", [])
+        axis2_chart_ids = multiline_fd.get("line_charts_2", [])
+        all_charts = {
+            chart.id: chart
+            for chart in ChartDAO.find_by_ids(axis1_chart_ids + axis2_chart_ids)
         }
+        axis1_charts = [all_charts[chart_id] for chart_id in axis1_chart_ids]
+        axis2_charts = [all_charts[chart_id] for chart_id in axis2_chart_ids]
+
+        filters = multiline_fd.get("filters", [])
+        add_prefix = multiline_fd.get("prefix_metric_with_slice_name", False)
+        data = []
+        min_x, max_x = None, None
+
+        for chart, y_axis in [(chart, 1) for chart in axis1_charts] + [
+            (chart, 2) for chart in axis2_charts
+        ]:
+            prefix = f"{chart.chart}: " if add_prefix else ""
+            chart_fd = chart.form_data
+            chart_fd["filters"] = chart_fd.get("filters", []) + filters
+            if "extra_filters" in multiline_fd:
+                chart_fd["extra_filters"] = multiline_fd["extra_filters"]
+            if "time_range" in multiline_fd:
+                chart_fd["time_range"] = multiline_fd["time_range"]
+            viz_obj = viz_types[chart.viz_type](
+                chart.datasource,
+                form_data=chart_fd,
+                force=self.force,
+                force_cached=self.force_cached,
+            )
+            df = viz_obj.get_df_payload()["df"]
+            chart_series = viz_obj.get_data(df) or []
+            for series in chart_series:
+                x_values = [value["x"] for value in series["values"]]
+                min_x = min(x_values + ([min_x] if min_x is not None else []))
+                max_x = max(x_values + ([max_x] if max_x is not None else []))
+
+                data.append(
+                    {
+                        "key": prefix + ", ".join(series["key"]),
+                        "type": "line",
+                        "values": series["values"],
+                        "yAxis": y_axis,
+                    }
+                )
+        bounds = []
+        if min_x is not None:
+            bounds.append({"x": min_x, "y": None})
+        if max_x is not None:
+            bounds.append({"x": max_x, "y": None})
+
+        for series in data:
+            series["values"].extend(bounds)
+        return data
 
 
 class NVD3DualLineViz(NVD3Viz):