Skip to content

Commit

Permalink
feat: Adds Bar chart migration logic (#28602)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-s-molina committed May 28, 2024
1 parent 8c32503 commit e17724a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
6 changes: 6 additions & 0 deletions superset/cli/viz_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

class VizType(str, Enum):
AREA = "area"
BAR = "bar"
BUBBLE = "bubble"
DIST_BAR = "dist_bar"
DUAL_LINE = "dual_line"
HEATMAP = "heatmap"
LINE = "line"
Expand Down Expand Up @@ -78,7 +80,9 @@ def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:
# pylint: disable=import-outside-toplevel
from superset.migrations.shared.migrate_viz.processors import (
MigrateAreaChart,
MigrateBarChart,
MigrateBubbleChart,
MigrateDistBarChart,
MigrateDualLine,
MigrateHeatmapChart,
MigrateLineChart,
Expand All @@ -89,7 +93,9 @@ def migrate(viz_type: VizType, is_downgrade: bool = False) -> None:

migrations = {
VizType.AREA: MigrateAreaChart,
VizType.BAR: MigrateBarChart,
VizType.BUBBLE: MigrateBubbleChart,
VizType.DIST_BAR: MigrateDistBarChart,
VizType.DUAL_LINE: MigrateDualLine,
VizType.HEATMAP: MigrateHeatmapChart,
VizType.LINE: MigrateLineChart,
Expand Down
54 changes: 51 additions & 3 deletions superset/migrations/shared/migrate_viz/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ class TimeseriesChart(MigrateViz):
"show_controls": "show_extra_controls",
"x_axis_label": "x_axis_title",
"x_axis_format": "x_axis_time_format",
"x_axis_showminmax": "truncateXAxis",
"x_ticks_layout": "xAxisLabelRotation",
"y_axis_label": "y_axis_title",
"y_axis_showminmax": "truncateYAxis",
"y_log_scale": "logAxis",
}
remove_keys = {"contribution", "show_brush", "show_markers"}
remove_keys = {
"contribution",
"line_interpolation",
"reduce_x_ticks",
"show_brush",
"show_markers",
}

def _pre_action(self) -> None:
self.data["contributionMode"] = "row" if self.data.get("contribution") else None
Expand Down Expand Up @@ -156,8 +163,6 @@ class MigrateLineChart(TimeseriesChart):
def _pre_action(self) -> None:
super()._pre_action()

self.remove_keys.add("line_interpolation")

line_interpolation = self.data.get("line_interpolation")
if line_interpolation == "cardinal":
self.target_viz_type = "echarts_timeseries_smooth"
Expand Down Expand Up @@ -190,6 +195,49 @@ def _pre_action(self) -> None:
self.data["opacity"] = 0.7


class MigrateBarChart(TimeseriesChart):
source_viz_type = "bar"
target_viz_type = "echarts_timeseries_bar"

def _pre_action(self) -> None:
super()._pre_action()

self.rename_keys["show_bar_value"] = "show_value"

self.remove_keys.add("bar_stacked")

self.data["stack"] = "Stack" if self.data.get("bar_stacked") else None


class MigrateDistBarChart(TimeseriesChart):
source_viz_type = "dist_bar"
target_viz_type = "echarts_timeseries_bar"
has_x_axis_control = False

def _pre_action(self) -> None:
super()._pre_action()

groupby = self.data.get("groupby") or []
columns = self.data.get("columns") or []
if len(groupby) > 0:
# x-axis supports only one value
self.data["x_axis"] = groupby[0]

self.data["groupby"] = []
if len(groupby) > 1:
# rest of groupby will go into dimensions
self.data["groupby"] += groupby[1:]
if len(columns) > 0:
self.data["groupby"] += columns

self.rename_keys["show_bar_value"] = "show_value"

self.remove_keys.add("columns")
self.remove_keys.add("bar_stacked")

self.data["stack"] = "Stack" if self.data.get("bar_stacked") else None


class MigrateBubbleChart(MigrateViz):
source_viz_type = "bubble"
target_viz_type = "bubble_v2"
Expand Down
64 changes: 64 additions & 0 deletions tests/unit_tests/migrations/viz/nvd3_bar_chart_to_echarts_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import Any

from superset.migrations.shared.migrate_viz import MigrateBarChart, MigrateDistBarChart
from tests.unit_tests.migrations.viz.utils import (
migrate_and_assert,
TIMESERIES_SOURCE_FORM_DATA,
TIMESERIES_TARGET_FORM_DATA,
)


def test_bar_migration() -> None:
source_form_data: dict[str, Any] = {
"viz_type": "bar",
"show_bar_value": True,
"bar_stacked": True,
}

target_form_data: dict[str, Any] = {
"form_data_bak": source_form_data,
"viz_type": "echarts_timeseries_bar",
"show_value": True,
"stack": "Stack",
}
source_form_data.update(TIMESERIES_SOURCE_FORM_DATA)
target_form_data.update(TIMESERIES_TARGET_FORM_DATA)
migrate_and_assert(MigrateBarChart, source_form_data, target_form_data)


def test_dist_bar_migration() -> None:
source_form_data: dict[str, Any] = {
"viz_type": "dist_bar",
"show_bar_value": True,
"bar_stacked": True,
"groupby": ["column_a", "column_b"],
"columns": ["column_c", "column_d"],
}

target_form_data: dict[str, Any] = {
"form_data_bak": source_form_data,
"viz_type": "echarts_timeseries_bar",
"show_value": True,
"stack": "Stack",
"x_axis": "column_a",
"groupby": ["column_b", "column_c", "column_d"],
}
source_form_data.update(TIMESERIES_SOURCE_FORM_DATA)
target_form_data.update(TIMESERIES_TARGET_FORM_DATA)
migrate_and_assert(MigrateDistBarChart, source_form_data, target_form_data)

0 comments on commit e17724a

Please sign in to comment.