| const languageShowName = getCorrectCapitalizationLanguageName(language || '') | const languageShowName = getCorrectCapitalizationLanguageName(language || '') | ||||
| const isDarkMode = theme === Theme.dark | const isDarkMode = theme === Theme.dark | ||||
| const echartsStyle = useMemo(() => ({ | |||||
| height: '350px', | |||||
| width: '100%', | |||||
| }), []) | |||||
| const echartsOpts = useMemo(() => ({ | |||||
| renderer: 'canvas', | |||||
| width: 'auto', | |||||
| }) as any, []) | |||||
| const echartsOnEvents = useMemo(() => ({ | |||||
| finished: () => { | |||||
| const instance = echartsRef.current?.getEchartsInstance?.() | |||||
| if (instance) | |||||
| instance.resize() | |||||
| }, | |||||
| }), [echartsRef]) // echartsRef is stable, so this effectively runs once. | |||||
| // Handle container resize for echarts | // Handle container resize for echarts | ||||
| useEffect(() => { | useEffect(() => { | ||||
| if (language !== 'echarts' || !echartsRef.current) return | if (language !== 'echarts' || !echartsRef.current) return | ||||
| <ReactEcharts | <ReactEcharts | ||||
| ref={echartsRef} | ref={echartsRef} | ||||
| option={finalChartOption} | option={finalChartOption} | ||||
| style={{ | |||||
| height: '350px', | |||||
| width: '100%', | |||||
| }} | |||||
| style={echartsStyle} | |||||
| theme={isDarkMode ? 'dark' : undefined} | theme={isDarkMode ? 'dark' : undefined} | ||||
| opts={{ | |||||
| renderer: 'canvas', | |||||
| width: 'auto', | |||||
| }} | |||||
| opts={echartsOpts} | |||||
| notMerge={true} | notMerge={true} | ||||
| onEvents={{ | |||||
| // Force resize when chart is finished rendering | |||||
| finished: () => { | |||||
| const instance = echartsRef.current?.getEchartsInstance?.() | |||||
| if (instance) | |||||
| instance.resize() | |||||
| }, | |||||
| }} | |||||
| onEvents={echartsOnEvents} | |||||
| /> | /> | ||||
| </ErrorBoundary> | </ErrorBoundary> | ||||
| </div> | </div> | ||||
| <ReactEcharts | <ReactEcharts | ||||
| ref={echartsRef} | ref={echartsRef} | ||||
| option={errorOption} | option={errorOption} | ||||
| style={{ | |||||
| height: '350px', | |||||
| width: '100%', | |||||
| }} | |||||
| style={echartsStyle} | |||||
| theme={isDarkMode ? 'dark' : undefined} | theme={isDarkMode ? 'dark' : undefined} | ||||
| opts={{ | |||||
| renderer: 'canvas', | |||||
| width: 'auto', | |||||
| }} | |||||
| opts={echartsOpts} | |||||
| notMerge={true} | notMerge={true} | ||||
| /> | /> | ||||
| </ErrorBoundary> | </ErrorBoundary> | ||||
| </SyntaxHighlighter> | </SyntaxHighlighter> | ||||
| ) | ) | ||||
| } | } | ||||
| }, [children, language, isSVG, finalChartOption, props, theme, match]) | |||||
| }, [children, language, isSVG, finalChartOption, props, theme, match, chartState, isDarkMode, echartsStyle, echartsOpts, echartsOnEvents]) | |||||
| if (inline || !match) | if (inline || !match) | ||||
| return <code {...props} className={className}>{children}</code> | return <code {...props} className={className}>{children}</code> |