60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
'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
|
|
|