({type='line', id, options, data, redraw=false, plugins={}, ...props})
| 83 | }; |
| 84 | |
| 85 | export default function BaseChart({type='line', id, options, data, redraw=false, plugins={}, ...props}) { |
| 86 | const chartRef = React.useRef(); |
| 87 | const chartObj = React.useRef(); |
| 88 | const theme = useTheme(); |
| 89 | |
| 90 | const initChart = function() { |
| 91 | Chart.register(...registerables); |
| 92 | // Register for Zoom Plugin |
| 93 | Chart.register(zoomPlugin); |
| 94 | let chartContext = chartRef.current.getContext('2d'); |
| 95 | chartObj.current = new Chart(chartContext, { |
| 96 | type: type, |
| 97 | data: data, |
| 98 | plugins: [plugins], |
| 99 | options: options, |
| 100 | }); |
| 101 | props.onInit?.(chartObj.current); |
| 102 | }; |
| 103 | |
| 104 | const destroyChart = function() { |
| 105 | chartObj.current?.resetZoom?.(); |
| 106 | chartObj.current?.destroy(); |
| 107 | }; |
| 108 | |
| 109 | useEffect(()=>{ |
| 110 | initChart(); |
| 111 | return destroyChart; |
| 112 | }, []); |
| 113 | |
| 114 | useEffect(()=>{ |
| 115 | let scaleColors = { |
| 116 | scales: { |
| 117 | x: { |
| 118 | ticks: { |
| 119 | color: theme.palette.text.primary, |
| 120 | }, |
| 121 | }, |
| 122 | y: { |
| 123 | ticks: { |
| 124 | color: theme.palette.text.primary, |
| 125 | }, |
| 126 | grid: { |
| 127 | zeroLineColor: theme.otherVars.borderColor, |
| 128 | color: theme.otherVars.borderColor |
| 129 | }, |
| 130 | }, |
| 131 | }, |
| 132 | }; |
| 133 | let merged = options; |
| 134 | // Override only if scales are actually defined. |
| 135 | if(options.scales && Object.keys(options.scales).length > 0) { |
| 136 | merged = _.merge(options, scaleColors); |
| 137 | } |
| 138 | chartObj.current.options = merged; |
| 139 | chartObj.current.update(props.updateOptions || {}); |
| 140 | },[theme]); |
| 141 | |
| 142 | useEffect(()=>{ |
nothing calls this directly
no test coverage detected