148 lines
3.8 KiB
TypeScript
148 lines
3.8 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
|
import type { ComponentType, CSSProperties } from 'react'
|
|
import type { EChartsOption } from 'echarts'
|
|
|
|
import { CHART_HEIGHTS } from './chart.config'
|
|
import { useResizeKey } from './useResizeKey'
|
|
|
|
type EChartsSeries = NonNullable<EChartsOption['series']>
|
|
type ReactEChartsProps = {
|
|
option: EChartsOption
|
|
className?: string
|
|
style?: CSSProperties
|
|
notMerge?: boolean
|
|
lazyUpdate?: boolean
|
|
onEvents?: Record<string, (params: unknown) => void>
|
|
}
|
|
|
|
export type BaseEChartProps = {
|
|
option: EChartsOption
|
|
height?: number | string
|
|
className?: string
|
|
contentClassName?: string
|
|
chartClassName?: string
|
|
style?: CSSProperties
|
|
contentStyle?: CSSProperties
|
|
chartStyle?: CSSProperties
|
|
notMerge?: boolean
|
|
lazyUpdate?: boolean
|
|
onEvents?: Record<string, (params: unknown) => void>
|
|
resizeDebounceMs?: number
|
|
normalizeValues?: boolean
|
|
decimalPlaces?: number
|
|
}
|
|
|
|
const roundNumber = (value: number, decimalPlaces: number) => {
|
|
const factor = 10 ** decimalPlaces
|
|
return Math.round((value + Number.EPSILON) * factor) / factor
|
|
}
|
|
|
|
const normalizeDataValue = (value: unknown, decimalPlaces: number): unknown => {
|
|
if (typeof value === 'number') {
|
|
return roundNumber(value, decimalPlaces)
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
return value.map((item) => normalizeDataValue(item, decimalPlaces))
|
|
}
|
|
|
|
if (value && typeof value === 'object' && 'value' in value) {
|
|
const dataItem = value as Record<string, unknown>
|
|
|
|
return {
|
|
...dataItem,
|
|
value: normalizeDataValue(dataItem.value, decimalPlaces),
|
|
}
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
const normalizeSeries = (
|
|
series: EChartsOption['series'] | undefined,
|
|
decimalPlaces: number,
|
|
): EChartsOption['series'] => {
|
|
if (!series) return series
|
|
|
|
const normalizeOne = (seriesOption: EChartsSeries): EChartsSeries => {
|
|
const candidate = seriesOption as Record<string, unknown>
|
|
const data = candidate.data
|
|
|
|
if (!Array.isArray(data)) {
|
|
return seriesOption
|
|
}
|
|
|
|
return {
|
|
...candidate,
|
|
data: data.map((value) => normalizeDataValue(value, decimalPlaces)),
|
|
} as EChartsSeries
|
|
}
|
|
|
|
return (Array.isArray(series) ? series.map((item) => normalizeOne(item as EChartsSeries)) : normalizeOne(series)) as EChartsOption['series']
|
|
}
|
|
|
|
export function BaseEChart({
|
|
option,
|
|
height = CHART_HEIGHTS.DEFAULT,
|
|
className,
|
|
contentClassName,
|
|
chartClassName,
|
|
style,
|
|
contentStyle,
|
|
chartStyle,
|
|
notMerge = true,
|
|
lazyUpdate = false,
|
|
onEvents,
|
|
resizeDebounceMs = 150,
|
|
normalizeValues = true,
|
|
decimalPlaces = 2,
|
|
}: BaseEChartProps) {
|
|
const resizeKey = useResizeKey(resizeDebounceMs)
|
|
const [ReactECharts, setReactECharts] = useState<ComponentType<ReactEChartsProps> | null>(null)
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
|
|
void import('echarts-for-react').then((module) => {
|
|
if (!cancelled) {
|
|
setReactECharts(() => module.default as ComponentType<ReactEChartsProps>)
|
|
}
|
|
})
|
|
|
|
return () => {
|
|
cancelled = true
|
|
}
|
|
}, [])
|
|
|
|
const normalizedOption = useMemo<EChartsOption>(() => {
|
|
if (!normalizeValues) return option
|
|
|
|
return {
|
|
...option,
|
|
series: normalizeSeries(option.series, decimalPlaces),
|
|
}
|
|
}, [decimalPlaces, normalizeValues, option])
|
|
|
|
return (
|
|
<div className={className} style={{ display: 'flex', flexDirection: 'column', height, ...style }}>
|
|
<div className={contentClassName} style={{ flex: 1, minHeight: 0, ...contentStyle }}>
|
|
{ReactECharts ? (
|
|
<ReactECharts
|
|
key={resizeKey}
|
|
option={normalizedOption}
|
|
className={chartClassName}
|
|
style={{ width: '100%', height: '100%', ...chartStyle }}
|
|
notMerge={notMerge}
|
|
lazyUpdate={lazyUpdate}
|
|
onEvents={onEvents}
|
|
/>
|
|
) : null}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default BaseEChart
|