Skip to content

Instantly share code, notes, and snippets.

@hoangtrung99
Last active January 5, 2023 04:18
Show Gist options
  • Save hoangtrung99/e706614ccbb4dbfc560ff9ad8d5e13b2 to your computer and use it in GitHub Desktop.
Save hoangtrung99/e706614ccbb4dbfc560ff9ad8d5e13b2 to your computer and use it in GitHub Desktop.
React component for react
import type { ECharts, SetOptionOpts } from 'echarts/core'
import { getInstanceByDom, init } from 'echarts/core'
import { ECBasicOption } from 'echarts/types/dist/shared'
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react'
// Redeclare forwardRef
declare module 'react' {
function forwardRef<T, P = Record<string, unknown>>(
render: (props: P, ref: React.Ref<T>) => React.ReactElement | null
): (props: P & React.RefAttributes<T>) => React.ReactElement | null
}
export type CoreChartProps<T> = {
options?: T
loading?: boolean
settings?: SetOptionOpts
}
function CoreChartInner<T extends ECBasicOption>(
{ options, loading, settings }: CoreChartProps<T>,
ref: React.ForwardedRef<ECharts>
) {
const chartRef = useRef<HTMLDivElement>(null)
useImperativeHandle(ref, () => chartRef.current as unknown as ECharts, [])
useEffect(() => {
// Initialize chart
let chart: ECharts | undefined
if (chartRef.current !== null) {
chart = init(chartRef.current)
}
function resizeChart() {
chart && chart.resize()
}
window.addEventListener('resize', resizeChart)
// Return cleanup function
return () => {
chart && chart.dispose()
window.removeEventListener('resize', resizeChart)
}
}, [])
useEffect(() => {
// Update chart
if (chartRef.current !== null) {
const chart = getInstanceByDom(chartRef.current)
chart && options && chart.setOption(options, settings)
}
}, [options, settings])
useEffect(() => {
// Update chart
if (chartRef.current !== null) {
const chart = getInstanceByDom(chartRef.current)
if (chart) {
loading ? chart.showLoading() : chart.hideLoading()
}
}
}, [loading])
return <div ref={chartRef} style={{ width: '100%', height: '100%' }} />
}
export const CoreChart = forwardRef(CoreChartInner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment