260714_01_ENH: 테스트 서버에서 기동 후 중간 테스트를 위한 커밋
This commit is contained in:
147
src/components/chart/BaseEChart.tsx
Normal file
147
src/components/chart/BaseEChart.tsx
Normal file
@@ -0,0 +1,147 @@
|
||||
'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
|
||||
59
src/components/chart/KriBarChart.tsx
Normal file
59
src/components/chart/KriBarChart.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriCartesianChartProps } from './chart.types'
|
||||
import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils'
|
||||
|
||||
export type KriBarChartProps = KriCartesianChartProps & {
|
||||
isHorizontal?: boolean
|
||||
barWidth?: number | string
|
||||
barMaxWidth?: number | string
|
||||
}
|
||||
|
||||
export function KriBarChart({
|
||||
categories,
|
||||
series,
|
||||
isHorizontal = false,
|
||||
barWidth,
|
||||
barMaxWidth,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriBarChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const categoryAxis = { type: 'category', data: categories }
|
||||
const valueAxis = { type: 'value' }
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'axis', axisPointer: { type: 'shadow' } },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
grid: grid ?? defaultGrid(series.length),
|
||||
xAxis: mergeObjectOption(isHorizontal ? valueAxis : categoryAxis, xAxis) as EChartsOption['xAxis'],
|
||||
yAxis: mergeObjectOption(isHorizontal ? categoryAxis : valueAxis, yAxis) as EChartsOption['yAxis'],
|
||||
series: series.map((item) => ({
|
||||
type: 'bar',
|
||||
name: item.name,
|
||||
data: item.data,
|
||||
...(barWidth ? { barWidth } : {}),
|
||||
...(barMaxWidth ? { barMaxWidth } : {}),
|
||||
...(item.color ? { color: item.color } : {}),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [barMaxWidth, barWidth, categories, grid, isHorizontal, legend, option, series, title, tooltip, xAxis, yAxis])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriBarChart
|
||||
|
||||
58
src/components/chart/KriLineChart.tsx
Normal file
58
src/components/chart/KriLineChart.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriCartesianChartProps } from './chart.types'
|
||||
import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils'
|
||||
|
||||
export type KriLineChartProps = KriCartesianChartProps & {
|
||||
smooth?: boolean
|
||||
area?: boolean
|
||||
showSymbol?: boolean
|
||||
}
|
||||
|
||||
export function KriLineChart({
|
||||
categories,
|
||||
series,
|
||||
smooth = false,
|
||||
area = false,
|
||||
showSymbol = true,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriLineChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'axis' },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
grid: grid ?? defaultGrid(series.length),
|
||||
xAxis: mergeObjectOption({ type: 'category', data: categories, boundaryGap: false }, xAxis) as EChartsOption['xAxis'],
|
||||
yAxis: mergeObjectOption({ type: 'value' }, yAxis) as EChartsOption['yAxis'],
|
||||
series: series.map((item) => ({
|
||||
type: 'line',
|
||||
name: item.name,
|
||||
data: item.data,
|
||||
smooth,
|
||||
showSymbol,
|
||||
...(area ? { areaStyle: {} } : {}),
|
||||
...(item.color ? { color: item.color } : {}),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [area, categories, grid, legend, option, series, showSymbol, smooth, title, tooltip, xAxis, yAxis])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriLineChart
|
||||
|
||||
50
src/components/chart/KriPieChart.tsx
Normal file
50
src/components/chart/KriPieChart.tsx
Normal file
@@ -0,0 +1,50 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriChartBaseProps, KriPieChartSeries } from './chart.types'
|
||||
import { defaultLegend, mergeChartOptions } from './chart.utils'
|
||||
|
||||
export type KriPieChartProps = KriChartBaseProps & {
|
||||
series: KriPieChartSeries[]
|
||||
}
|
||||
|
||||
const getPieCenter = (index: number, count: number) => {
|
||||
if (count === 1) return ['50%', '55%']
|
||||
|
||||
return [`${((index + 0.5) / count) * 100}%`, '55%']
|
||||
}
|
||||
|
||||
export function KriPieChart({
|
||||
series,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriPieChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'item' },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
series: series.map((item, index) => ({
|
||||
type: 'pie',
|
||||
name: item.name,
|
||||
data: item.data,
|
||||
radius: item.radius ?? (series.length > 1 ? ['32%', '52%'] : '65%'),
|
||||
center: item.center ?? getPieCenter(index, series.length),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [legend, option, series, title, tooltip])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriPieChart
|
||||
|
||||
55
src/components/chart/KriRadarChart.tsx
Normal file
55
src/components/chart/KriRadarChart.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriChartBaseProps, KriRadarChartSeries, KriRadarDataItem, KriRadarIndicator } from './chart.types'
|
||||
import { defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils'
|
||||
|
||||
export type KriRadarChartProps = KriChartBaseProps & {
|
||||
indicators: KriRadarIndicator[]
|
||||
series: KriRadarChartSeries[]
|
||||
radar?: EChartsOption['radar']
|
||||
}
|
||||
|
||||
const normalizeRadarData = (series: KriRadarChartSeries): KriRadarDataItem[] => {
|
||||
if (series.data.every((value) => typeof value === 'number')) {
|
||||
return [{ name: series.name, value: series.data as number[] }]
|
||||
}
|
||||
|
||||
return series.data as KriRadarDataItem[]
|
||||
}
|
||||
|
||||
export function KriRadarChart({
|
||||
indicators,
|
||||
series,
|
||||
radar,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriRadarChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'item' },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
radar: mergeObjectOption({ indicator: indicators }, radar) as EChartsOption['radar'],
|
||||
series: series.map((item) => ({
|
||||
type: 'radar',
|
||||
name: item.name,
|
||||
data: normalizeRadarData(item),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [indicators, legend, option, radar, series, title, tooltip])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriRadarChart
|
||||
|
||||
55
src/components/chart/KriScatterChart.tsx
Normal file
55
src/components/chart/KriScatterChart.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriChartBaseProps, KriScatterChartSeries } from './chart.types'
|
||||
import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils'
|
||||
|
||||
export type KriScatterChartProps = KriChartBaseProps & {
|
||||
series: KriScatterChartSeries[]
|
||||
grid?: EChartsOption['grid']
|
||||
xAxis?: EChartsOption['xAxis']
|
||||
yAxis?: EChartsOption['yAxis']
|
||||
symbolSize?: number | ((value: unknown, params: unknown) => number)
|
||||
}
|
||||
|
||||
export function KriScatterChart({
|
||||
series,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
symbolSize,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriScatterChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'item' },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
grid: grid ?? defaultGrid(series.length),
|
||||
xAxis: mergeObjectOption({ type: 'value' }, xAxis) as EChartsOption['xAxis'],
|
||||
yAxis: mergeObjectOption({ type: 'value' }, yAxis) as EChartsOption['yAxis'],
|
||||
series: series.map((item) => ({
|
||||
type: 'scatter',
|
||||
name: item.name,
|
||||
data: item.data,
|
||||
...(symbolSize ? { symbolSize } : {}),
|
||||
...(item.color ? { color: item.color } : {}),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [grid, legend, option, series, symbolSize, title, tooltip, xAxis, yAxis])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriScatterChart
|
||||
|
||||
62
src/components/chart/KriStackedBarChart.tsx
Normal file
62
src/components/chart/KriStackedBarChart.tsx
Normal file
@@ -0,0 +1,62 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import { BaseEChart } from './BaseEChart'
|
||||
import type { KriCartesianChartProps } from './chart.types'
|
||||
import { defaultGrid, defaultLegend, mergeChartOptions, mergeObjectOption } from './chart.utils'
|
||||
|
||||
export type KriStackedBarChartProps = KriCartesianChartProps & {
|
||||
isHorizontal?: boolean
|
||||
stackName?: string
|
||||
barWidth?: number | string
|
||||
barMaxWidth?: number | string
|
||||
}
|
||||
|
||||
export function KriStackedBarChart({
|
||||
categories,
|
||||
series,
|
||||
isHorizontal = false,
|
||||
stackName = 'total',
|
||||
barWidth,
|
||||
barMaxWidth,
|
||||
title,
|
||||
legend,
|
||||
tooltip,
|
||||
grid,
|
||||
xAxis,
|
||||
yAxis,
|
||||
option,
|
||||
...baseChartProps
|
||||
}: KriStackedBarChartProps) {
|
||||
const chartOption = useMemo<EChartsOption>(() => {
|
||||
const categoryAxis = { type: 'category', data: categories }
|
||||
const valueAxis = { type: 'value' }
|
||||
const generated: EChartsOption = {
|
||||
title,
|
||||
tooltip: tooltip ?? { trigger: 'axis', axisPointer: { type: 'shadow' } },
|
||||
legend: legend ?? defaultLegend(series.length),
|
||||
grid: grid ?? defaultGrid(series.length),
|
||||
xAxis: mergeObjectOption(isHorizontal ? valueAxis : categoryAxis, xAxis) as EChartsOption['xAxis'],
|
||||
yAxis: mergeObjectOption(isHorizontal ? categoryAxis : valueAxis, yAxis) as EChartsOption['yAxis'],
|
||||
series: series.map((item) => ({
|
||||
type: 'bar',
|
||||
name: item.name,
|
||||
data: item.data,
|
||||
stack: item.stack ?? stackName,
|
||||
...(barWidth ? { barWidth } : {}),
|
||||
...(barMaxWidth ? { barMaxWidth } : {}),
|
||||
...(item.color ? { color: item.color } : {}),
|
||||
...item.option,
|
||||
})) as EChartsOption['series'],
|
||||
}
|
||||
|
||||
return mergeChartOptions(generated, option)
|
||||
}, [barMaxWidth, barWidth, categories, grid, isHorizontal, legend, option, series, stackName, title, tooltip, xAxis, yAxis])
|
||||
|
||||
return <BaseEChart option={chartOption} {...baseChartProps} />
|
||||
}
|
||||
|
||||
export default KriStackedBarChart
|
||||
|
||||
6
src/components/chart/chart.config.ts
Normal file
6
src/components/chart/chart.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export const CHART_HEIGHTS = {
|
||||
DEFAULT: 320,
|
||||
SMALL: 240,
|
||||
LARGE: 420,
|
||||
} as const
|
||||
|
||||
71
src/components/chart/chart.types.ts
Normal file
71
src/components/chart/chart.types.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
import type { BaseEChartProps } from './BaseEChart'
|
||||
|
||||
export type KriChartBaseProps = Omit<BaseEChartProps, 'option'> & {
|
||||
option?: EChartsOption
|
||||
title?: EChartsOption['title']
|
||||
legend?: EChartsOption['legend']
|
||||
tooltip?: EChartsOption['tooltip']
|
||||
}
|
||||
|
||||
export type KriCategoryValue = string | number
|
||||
|
||||
export type KriCategoryChartSeries = {
|
||||
name: string
|
||||
data: unknown[]
|
||||
color?: string
|
||||
stack?: string
|
||||
option?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export type KriCartesianChartProps = KriChartBaseProps & {
|
||||
categories: KriCategoryValue[]
|
||||
series: KriCategoryChartSeries[]
|
||||
grid?: EChartsOption['grid']
|
||||
xAxis?: EChartsOption['xAxis']
|
||||
yAxis?: EChartsOption['yAxis']
|
||||
}
|
||||
|
||||
export type KriNamedChartData = {
|
||||
name: string
|
||||
value: unknown
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export type KriPieChartSeries = {
|
||||
name?: string
|
||||
data: KriNamedChartData[]
|
||||
radius?: string | number | Array<string | number>
|
||||
center?: Array<string | number>
|
||||
option?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export type KriRadarIndicator = {
|
||||
name: string
|
||||
max?: number
|
||||
min?: number
|
||||
color?: string
|
||||
}
|
||||
|
||||
export type KriRadarDataItem = {
|
||||
name?: string
|
||||
value: number[]
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export type KriRadarChartSeries = {
|
||||
name: string
|
||||
data: number[] | KriRadarDataItem[]
|
||||
option?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export type KriScatterDataItem = [number, number] | [string, number] | KriNamedChartData
|
||||
|
||||
export type KriScatterChartSeries = {
|
||||
name: string
|
||||
data: KriScatterDataItem[]
|
||||
color?: string
|
||||
option?: Record<string, unknown>
|
||||
}
|
||||
|
||||
34
src/components/chart/chart.utils.ts
Normal file
34
src/components/chart/chart.utils.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
export const mergeChartOptions = (generated: EChartsOption, option?: EChartsOption): EChartsOption => {
|
||||
if (!option) return generated
|
||||
|
||||
return {
|
||||
...generated,
|
||||
...option,
|
||||
series: option.series ?? generated.series,
|
||||
}
|
||||
}
|
||||
|
||||
export const mergeObjectOption = <TOption>(defaults: Record<string, unknown>, option?: TOption): TOption | Record<string, unknown> => {
|
||||
if (!option) return defaults
|
||||
if (Array.isArray(option)) return option
|
||||
if (typeof option === 'object') return { ...defaults, ...(option as Record<string, unknown>) }
|
||||
|
||||
return option
|
||||
}
|
||||
|
||||
export const defaultLegend = (seriesCount: number): EChartsOption['legend'] | undefined => {
|
||||
return seriesCount > 1 ? { top: 0 } : undefined
|
||||
}
|
||||
|
||||
export const defaultGrid = (seriesCount: number): EChartsOption['grid'] => {
|
||||
return {
|
||||
top: seriesCount > 1 ? 44 : 24,
|
||||
right: 24,
|
||||
bottom: 24,
|
||||
left: 24,
|
||||
containLabel: true,
|
||||
}
|
||||
}
|
||||
|
||||
29
src/components/chart/index.ts
Normal file
29
src/components/chart/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export { BaseEChart, default } from './BaseEChart'
|
||||
export type { BaseEChartProps } from './BaseEChart'
|
||||
export { KriBarChart } from './KriBarChart'
|
||||
export type { KriBarChartProps } from './KriBarChart'
|
||||
export { KriLineChart } from './KriLineChart'
|
||||
export type { KriLineChartProps } from './KriLineChart'
|
||||
export { KriPieChart } from './KriPieChart'
|
||||
export type { KriPieChartProps } from './KriPieChart'
|
||||
export { KriRadarChart } from './KriRadarChart'
|
||||
export type { KriRadarChartProps } from './KriRadarChart'
|
||||
export { KriScatterChart } from './KriScatterChart'
|
||||
export type { KriScatterChartProps } from './KriScatterChart'
|
||||
export { KriStackedBarChart } from './KriStackedBarChart'
|
||||
export type { KriStackedBarChartProps } from './KriStackedBarChart'
|
||||
export { CHART_HEIGHTS } from './chart.config'
|
||||
export type {
|
||||
KriCartesianChartProps,
|
||||
KriCategoryChartSeries,
|
||||
KriCategoryValue,
|
||||
KriChartBaseProps,
|
||||
KriNamedChartData,
|
||||
KriPieChartSeries,
|
||||
KriRadarChartSeries,
|
||||
KriRadarDataItem,
|
||||
KriRadarIndicator,
|
||||
KriScatterChartSeries,
|
||||
KriScatterDataItem,
|
||||
} from './chart.types'
|
||||
export { useResizeKey } from './useResizeKey'
|
||||
31
src/components/chart/useResizeKey.ts
Normal file
31
src/components/chart/useResizeKey.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export function useResizeKey(delay = 150) {
|
||||
const [resizeKey, setResizeKey] = useState(0)
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return
|
||||
|
||||
let timer: ReturnType<typeof window.setTimeout> | undefined
|
||||
|
||||
const handleResize = () => {
|
||||
if (timer) window.clearTimeout(timer)
|
||||
|
||||
timer = window.setTimeout(() => {
|
||||
setResizeKey((key) => key + 1)
|
||||
}, delay)
|
||||
}
|
||||
|
||||
window.addEventListener('resize', handleResize)
|
||||
|
||||
return () => {
|
||||
if (timer) window.clearTimeout(timer)
|
||||
window.removeEventListener('resize', handleResize)
|
||||
}
|
||||
}, [delay])
|
||||
|
||||
return resizeKey
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user