Skip to content

Commit a6dcc26

Browse files
committed
refactor(custom-chart-web): route store data through typed toPlotlyData helper
1 parent d196a6a commit a6dcc26

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/pluggableWidgets/custom-chart-web/src/controllers/CustomChartControllerHost.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { computed } from "mobx";
22

3-
import { Data } from "plotly.js-dist-min";
43
import { EditableChartStore, EditableChartStoreProps } from "@mendix/shared-charts/main";
54
import { ComputedAtom, DerivedPropsGate, SetupHost } from "@mendix/widget-plugin-mobx-kit/main";
65
import { ChartPropsController } from "./ChartPropsController";
76
import { PlotlyController } from "./PlotlyController";
87
import { ResizeController } from "./ResizeController";
98
import { ControllerProps } from "./typings";
109
import { PlotlyChartProps } from "../components/PlotlyChart";
10+
import { toPlotlyData } from "../utils/toPlotlyData";
1111

1212
export class CustomChartControllerHost extends SetupHost {
1313
resizeCtrl: ResizeController;
@@ -31,7 +31,7 @@ export class CustomChartControllerHost extends SetupHost {
3131
function viewModelAtom(store: EditableChartStore, adapter: ChartPropsController): ComputedAtom<PlotlyChartProps> {
3232
return computed(() => {
3333
return {
34-
data: store.data as Data[],
34+
data: toPlotlyData(store.data),
3535
layout: store.layout,
3636
config: store.config,
3737
onClick: adapter.chartOnClick
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { toPlotlyData } from "../toPlotlyData";
2+
3+
describe("toPlotlyData", () => {
4+
it("maps stored records to Plotly traces without reshaping them", () => {
5+
const stored = [
6+
{ type: "bar", x: [1, 2], y: [3, 4] },
7+
{ type: "scatter", x: [5], y: [6] }
8+
];
9+
10+
expect(toPlotlyData(stored)).toEqual(stored);
11+
});
12+
13+
it("returns an empty array for empty data", () => {
14+
expect(toPlotlyData([])).toEqual([]);
15+
});
16+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Data } from "plotly.js-dist-min";
2+
3+
/**
4+
* Boundary between the editable store and Plotly.
5+
*
6+
* The store keeps traces as `Record<string, unknown>[]` because user JSON is validated
7+
* on write (`EditableChartStore.setDataAt` parses, type-checks and warns on bad input) but
8+
* cannot be narrowed to Plotly's large `Data` union at that point. This helper localizes the
9+
* unavoidable conversion in one named, documented place instead of scattering `as Data[]` casts.
10+
*/
11+
export function toPlotlyData(data: Array<Record<string, unknown>>): Data[] {
12+
return data as Data[];
13+
}

0 commit comments

Comments
 (0)