|
1 | 1 | import { debounce } from "@mendix/widget-plugin-platform/utils/debounce";
|
2 |
| -import { useEffect, useMemo, useRef, useState, type RefObject } from "react"; |
| 2 | +import { useEffect, useMemo, useRef, useState, type RefObject, CSSProperties } from "react"; |
3 | 3 | import { CustomChartContainerProps } from "../../typings/CustomChartProps";
|
4 | 4 | import { PlotlyChart, ChartProps } from "../components/PlotlyChart";
|
5 |
| -import { ChartDataProcessor } from "../utils/ChartDataProcessor"; |
| 5 | +import { parseData, parseLayout, parseConfig } from "../utils/utils"; |
6 | 6 |
|
7 | 7 | interface UseCustomChartReturn {
|
8 | 8 | chartRef: RefObject<HTMLDivElement>;
|
9 |
| - containerStyle: { |
10 |
| - width?: string; |
11 |
| - height?: string; |
12 |
| - }; |
| 9 | + containerStyle: CSSProperties; |
13 | 10 | }
|
14 | 11 |
|
15 | 12 | export function useCustomChart(props: CustomChartContainerProps): UseCustomChartReturn {
|
16 | 13 | const chartRef = useRef<HTMLDivElement>(null);
|
17 | 14 | const [chart, setChart] = useState<PlotlyChart | null>(null);
|
18 | 15 | const [containerDimensions, setContainerDimensions] = useState<{ width?: number; height?: number }>({});
|
19 |
| - const dataProcessor = useRef(new ChartDataProcessor()); |
20 | 16 |
|
21 | 17 | const [setContainerDimensionsDebounced, abortDimensionsDebounce] = useMemo(
|
22 | 18 | () =>
|
@@ -63,74 +59,65 @@ export function useCustomChart(props: CustomChartContainerProps): UseCustomChart
|
63 | 59 | return;
|
64 | 60 | }
|
65 | 61 |
|
66 |
| - const data = dataProcessor.current.parseData(props.dataStatic, props.dataAttribute?.value, props.sampleData); |
67 |
| - |
68 |
| - const layout = dataProcessor.current.parseLayout( |
69 |
| - props.layoutStatic, |
70 |
| - props.layoutAttribute?.value, |
71 |
| - props.sampleLayout |
72 |
| - ); |
| 62 | + const data = parseData(props.dataStatic, props.dataAttribute?.value, props.sampleData); |
73 | 63 |
|
74 |
| - const dimensions = dataProcessor.current.calculateDimensions( |
75 |
| - props.widthUnit, |
76 |
| - props.width, |
77 |
| - props.heightUnit, |
78 |
| - props.height, |
79 |
| - containerDimensions.width, |
80 |
| - containerDimensions.height |
81 |
| - ); |
| 64 | + const layout = parseLayout(props.layoutStatic, props.layoutAttribute?.value, props.sampleLayout); |
82 | 65 |
|
83 |
| - const { width, height } = dimensions; |
| 66 | + const dimensions = { |
| 67 | + width: containerDimensions.width ?? 0, |
| 68 | + height: containerDimensions.height ?? 0 |
| 69 | + }; |
84 | 70 |
|
85 | 71 | const updateData: ChartProps = {
|
86 | 72 | data,
|
87 | 73 | layout: {
|
88 | 74 | ...layout,
|
89 |
| - width, |
90 |
| - height, |
| 75 | + width: dimensions.width, |
| 76 | + height: dimensions.height, |
91 | 77 | autosize: true,
|
92 | 78 | font: {
|
93 | 79 | family: "Open Sans, sans-serif",
|
94 |
| - size: Math.max(12 * (width / 1000), 8) |
| 80 | + size: Math.max(12 * (dimensions.width / 1000), 8) |
95 | 81 | },
|
96 | 82 | legend: {
|
97 | 83 | ...layout.legend,
|
98 | 84 | font: {
|
99 | 85 | ...layout.legend?.font,
|
100 |
| - size: Math.max(10 * (width / 1000), 7) |
| 86 | + size: Math.max(10 * (dimensions.width / 1000), 7) |
101 | 87 | },
|
102 |
| - itemwidth: Math.max(10 * (width / 1000), 3) |
| 88 | + itemwidth: Math.max(10 * (dimensions.width / 1000), 3), |
| 89 | + itemsizing: "constant" |
103 | 90 | },
|
104 | 91 | xaxis: {
|
105 | 92 | ...layout.xaxis,
|
106 | 93 | tickfont: {
|
107 | 94 | ...layout.xaxis?.tickfont,
|
108 |
| - size: Math.max(10 * (width / 1000), 7) |
| 95 | + size: Math.max(10 * (dimensions.width / 1000), 7) |
109 | 96 | }
|
110 | 97 | },
|
111 | 98 | yaxis: {
|
112 | 99 | ...layout.yaxis,
|
113 | 100 | tickfont: {
|
114 | 101 | ...layout.yaxis?.tickfont,
|
115 |
| - size: Math.max(10 * (width / 1000), 7) |
| 102 | + size: Math.max(10 * (dimensions.width / 1000), 7) |
116 | 103 | }
|
117 | 104 | },
|
118 | 105 | margin: {
|
119 | 106 | ...layout.margin,
|
120 |
| - l: Math.max(50 * (width / 1000), 30), |
121 |
| - r: Math.max(50 * (width / 1000), 30), |
122 |
| - t: Math.max(50 * (width / 1000), 30), |
123 |
| - b: Math.max(50 * (width / 1000), 30), |
124 |
| - pad: Math.max(4 * (width / 1000), 2) |
| 107 | + l: Math.max(50 * (dimensions.width / 1000), 30), |
| 108 | + r: Math.max(50 * (dimensions.width / 1000), 30), |
| 109 | + t: Math.max(50 * (dimensions.width / 1000), 30), |
| 110 | + b: Math.max(50 * (dimensions.width / 1000), 30), |
| 111 | + pad: Math.max(4 * (dimensions.width / 1000), 2) |
125 | 112 | }
|
126 | 113 | },
|
127 | 114 | config: {
|
128 |
| - ...dataProcessor.current.parseConfig(props.configurationOptions), |
| 115 | + ...parseConfig(props.configurationOptions), |
129 | 116 | displayModeBar: props.devMode === "developer",
|
130 | 117 | responsive: true
|
131 | 118 | },
|
132 |
| - width, |
133 |
| - height |
| 119 | + width: dimensions.width, |
| 120 | + height: dimensions.height |
134 | 121 | };
|
135 | 122 |
|
136 | 123 | updateChartDebounced(chart, updateData);
|
|
0 commit comments