Skip to content

Commit

Permalink
fix: nvd3 bar chart sortby metric (apache#15318)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie authored and cccs-RyanS committed Dec 17, 2021
1 parent b30ea22 commit 9637652
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
8 changes: 6 additions & 2 deletions superset/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,8 +1762,12 @@ def get_data(self, df: pd.DataFrame) -> VizData:
df = df.copy()
df[filled_cols] = df[filled_cols].fillna(value=NULL_STRING)

row = df.groupby(self.groupby).sum()[metrics[0]].copy()
row.sort_values(ascending=False, inplace=True)
sortby = utils.get_metric_name(
self.form_data.get("timeseries_limit_metric") or metrics[0]
)
row = df.groupby(self.groupby).sum()[sortby].copy()
is_asc = not self.form_data.get("order_desc")
row.sort_values(ascending=is_asc, inplace=True)
pt = df.pivot_table(index=self.groupby, columns=columns, values=metrics)
if fd.get("contribution"):
pt = pt.T
Expand Down
4 changes: 3 additions & 1 deletion tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ def test_explore_json_dist_bar_order(self):
"optionName": "metric_80g1qb9b6o7_ci5vquydcbe",
},
],
"order_desc": True,
"adhoc_filters": [],
"groupby": ["name"],
"columns": [],
Expand All @@ -999,7 +1000,8 @@ def test_explore_json_dist_bar_order(self):
SELECT count(name) AS count_name, count(ds) AS count_ds
FROM birth_names
WHERE ds >= '1921-01-22 00:00:00.000000' AND ds < '2021-01-22 00:00:00.000000'
GROUP BY name ORDER BY count_name DESC, count_ds DESC
GROUP BY name
ORDER BY count_name DESC
LIMIT 10;
""",
client_id="client_id_1",
Expand Down
5 changes: 5 additions & 0 deletions tests/viz_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ def test_groupby_nulls(self):
"adhoc_filters": [],
"groupby": ["toppings"],
"columns": [],
"order_desc": True,
}
datasource = self.get_datasource_mock()
df = pd.DataFrame(
Expand All @@ -487,6 +488,7 @@ def test_groupby_nans(self):
"adhoc_filters": [],
"groupby": ["beds"],
"columns": [],
"order_desc": True,
}
datasource = self.get_datasource_mock()
df = pd.DataFrame({"beds": [0, 1, nan, 2], "count": [30, 42, 3, 29]})
Expand All @@ -508,6 +510,7 @@ def test_column_nulls(self):
"adhoc_filters": [],
"groupby": ["toppings"],
"columns": ["role"],
"order_desc": True,
}
datasource = self.get_datasource_mock()
df = pd.DataFrame(
Expand Down Expand Up @@ -537,6 +540,7 @@ def test_column_metrics_in_order(self):
"adhoc_filters": [],
"groupby": ["toppings"],
"columns": [],
"order_desc": True,
}
datasource = self.get_datasource_mock()
df = pd.DataFrame(
Expand Down Expand Up @@ -574,6 +578,7 @@ def test_column_metrics_in_order_with_breakdowns(self):
"adhoc_filters": [],
"groupby": ["toppings"],
"columns": ["role"],
"order_desc": True,
}
datasource = self.get_datasource_mock()
df = pd.DataFrame(
Expand Down

0 comments on commit 9637652

Please sign in to comment.