1
1
import { debounce } from "@mendix/widget-plugin-platform/utils/debounce" ;
2
2
import { useEffect , useMemo , useRef , useState , type RefObject } from "react" ;
3
3
import { CustomChartContainerProps } from "../../typings/CustomChartProps" ;
4
- import { PlotlyChart } from "../components/PlotlyChart" ;
4
+ import { PlotlyChart , ChartProps } from "../components/PlotlyChart" ;
5
5
import { ChartDataProcessor } from "../utils/ChartDataProcessor" ;
6
6
7
7
interface UseCustomChartReturn {
@@ -28,26 +28,14 @@ export function useCustomChart(props: CustomChartContainerProps): UseCustomChart
28
28
29
29
const [ updateChartDebounced , abortChartUpdate ] = useMemo (
30
30
( ) =>
31
- debounce (
32
- (
33
- chartInstance : PlotlyChart | null ,
34
- updateData : {
35
- data : any ;
36
- layout : any ;
37
- config : any ;
38
- width : number ;
39
- height : number ;
40
- }
41
- ) => {
42
- if ( ! chartInstance ) {
43
- const newChart = new PlotlyChart ( chartRef . current ! , updateData ) ;
44
- setChart ( newChart ) ;
45
- } else {
46
- chartInstance . update ( updateData ) ;
47
- }
48
- } ,
49
- 100
50
- ) ,
31
+ debounce ( ( chartInstance : PlotlyChart | null , updateData : ChartProps ) => {
32
+ if ( ! chartInstance ) {
33
+ const newChart = new PlotlyChart ( chartRef . current ! , updateData ) ;
34
+ setChart ( newChart ) ;
35
+ } else {
36
+ chartInstance . update ( updateData ) ;
37
+ }
38
+ } , 100 ) ,
51
39
[ ]
52
40
) ;
53
41
@@ -83,19 +71,18 @@ export function useCustomChart(props: CustomChartContainerProps): UseCustomChart
83
71
props . sampleLayout
84
72
) ;
85
73
86
- const { width, height } = {
87
- width : containerDimensions . width ,
88
- height : dataProcessor . current . calculateDimensions (
89
- props . widthUnit ,
90
- props . width ,
91
- props . heightUnit ,
92
- props . height ,
93
- containerDimensions . width ,
94
- containerDimensions . height
95
- ) . height
96
- } ;
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
+ ) ;
97
82
98
- const updateData = {
83
+ const { width, height } = dimensions ;
84
+
85
+ const updateData : ChartProps = {
99
86
data,
100
87
layout : {
101
88
...layout ,
@@ -104,14 +91,43 @@ export function useCustomChart(props: CustomChartContainerProps): UseCustomChart
104
91
autosize : true ,
105
92
font : {
106
93
family : "Open Sans, sans-serif" ,
107
- size : 12
94
+ size : Math . max ( 12 * ( width / 1000 ) , 8 )
95
+ } ,
96
+ legend : {
97
+ ...layout . legend ,
98
+ font : {
99
+ ...layout . legend ?. font ,
100
+ size : Math . max ( 10 * ( width / 1000 ) , 7 )
101
+ } ,
102
+ itemwidth : Math . max ( 10 * ( width / 1000 ) , 3 )
103
+ } ,
104
+ xaxis : {
105
+ ...layout . xaxis ,
106
+ tickfont : {
107
+ ...layout . xaxis ?. tickfont ,
108
+ size : Math . max ( 10 * ( width / 1000 ) , 7 )
109
+ }
110
+ } ,
111
+ yaxis : {
112
+ ...layout . yaxis ,
113
+ tickfont : {
114
+ ...layout . yaxis ?. tickfont ,
115
+ size : Math . max ( 10 * ( width / 1000 ) , 7 )
116
+ }
117
+ } ,
118
+ margin : {
119
+ ...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 )
108
125
}
109
126
} ,
110
127
config : {
111
128
...dataProcessor . current . parseConfig ( props . configurationOptions ) ,
112
129
displayModeBar : props . devMode === "developer" ,
113
- responsive : true ,
114
- staticPlot : false
130
+ responsive : true
115
131
} ,
116
132
width,
117
133
height
0 commit comments