Fix/chart widget data migration

This commit is contained in:
Vicky Bansal 2020-04-15 15:26:36 +00:00 committed by Abhinav Jha
parent f3cccd9aee
commit c0d4bbb932
2 changed files with 35 additions and 15 deletions

View File

@ -12,7 +12,7 @@ FusionCharts.options.creditLabel = false;
export interface ChartComponentProps { export interface ChartComponentProps {
chartType: ChartType; chartType: ChartType;
chartData: ChartData[] | ChartDataPoint[]; chartData: ChartData[];
xAxisName: string; xAxisName: string;
yAxisName: string; yAxisName: string;
chartName: string; chartName: string;
@ -143,29 +143,23 @@ class ChartComponent extends React.Component<ChartComponentProps> {
}; };
getChartDataSource = () => { getChartDataSource = () => {
let chartData: ChartData[];
if (!Array.isArray(this.props.chartData[0])) {
chartData = [{ data: this.props.chartData as ChartDataPoint[] }];
} else {
chartData = this.props.chartData as ChartData[];
}
if ( if (
this.props.chartData.length === 1 || this.props.chartData.length === 1 ||
this.props.chartType === "PIE_CHART" this.props.chartType === "PIE_CHART"
) { ) {
return { return {
chart: this.getChartConfig(), chart: this.getChartConfig(),
data: this.getChartData(chartData), data: this.getChartData(this.props.chartData),
}; };
} else { } else {
return { return {
chart: this.getChartConfig(), chart: this.getChartConfig(),
categories: [ categories: [
{ {
category: this.getChartCategories(chartData), category: this.getChartCategories(this.props.chartData),
}, },
], ],
dataset: this.getChartDataset(chartData), dataset: this.getChartDataset(this.props.chartData),
}; };
} }
}; };
@ -182,12 +176,10 @@ class ChartComponent extends React.Component<ChartComponentProps> {
}, },
categories: [ categories: [
{ {
category: this.getChartCategories( category: this.getChartCategories(this.props.chartData),
this.props.chartData as ChartData[],
),
}, },
], ],
dataset: this.getChartDataset(this.props.chartData as ChartData[]), dataset: this.getChartDataset(this.props.chartData),
}; };
}; };

View File

@ -18,6 +18,7 @@ import { OccupiedSpace } from "constants/editorConstants";
import { DerivedPropFactory } from "utils/DerivedPropertiesFactory"; import { DerivedPropFactory } from "utils/DerivedPropertiesFactory";
import defaultTemplate from "templates/default"; import defaultTemplate from "templates/default";
import { generateReactKey } from "./generators"; import { generateReactKey } from "./generators";
import { ChartDataPoint } from "widgets/ChartWidget";
export type WidgetOperationParams = { export type WidgetOperationParams = {
operation: WidgetOperation; operation: WidgetOperation;
@ -74,6 +75,30 @@ const updateContainers = (dsl: ContainerWidgetProps<WidgetProps>) => {
return dsl; return dsl;
}; };
//transform chart data, from old chart widget to new chart widget
//updatd chart widget has support for multiple series
const chartDataMigration = (currentDSL: ContainerWidgetProps<WidgetProps>) => {
currentDSL.children = currentDSL.children?.map((children: WidgetProps) => {
if (
children.type === WidgetTypes.CHART_WIDGET &&
children.chartData &&
children.chartData.length &&
!Array.isArray(children.chartData[0])
) {
children.chartData = [{ data: children.chartData as ChartDataPoint[] }];
} else if (
children.type === WidgetTypes.CONTAINER_WIDGET ||
children.type === WidgetTypes.FORM_WIDGET ||
children.type === WidgetTypes.CANVAS_WIDGET ||
children.type === WidgetTypes.TABS_WIDGET
) {
children = chartDataMigration(children);
}
return children;
});
return currentDSL;
};
// A rudimentary transform function which updates the DSL based on its version. // A rudimentary transform function which updates the DSL based on its version.
// A more modular approach needs to be designed. // A more modular approach needs to be designed.
const transformDSL = (currentDSL: ContainerWidgetProps<WidgetProps>) => { const transformDSL = (currentDSL: ContainerWidgetProps<WidgetProps>) => {
@ -105,7 +130,10 @@ const transformDSL = (currentDSL: ContainerWidgetProps<WidgetProps>) => {
currentDSL.children = currentDSL.children.map(updateContainers); currentDSL.children = currentDSL.children.map(updateContainers);
currentDSL.version = 2; currentDSL.version = 2;
} }
if (currentDSL.version === 2) {
currentDSL = chartDataMigration(currentDSL);
currentDSL.version = 3;
}
return currentDSL; return currentDSL;
}; };