Expand visual kit and chart factories

This commit is contained in:
zhougf committed 2026-05-30 23:18:02 +08:00
1 parent 19f79ef8ef
commit 9e27c7bd34
21 files changed
+810 -52

No files matched your search

+170
View File
@@ -46,10 +46,61 @@ type RadarChartInput = {
color?: string
}
type DualAxisChartInput = {
categories: string[]
leftName?: string
rightName?: string
bar: {
name: string
values: number[]
color?: string
}
line: {
name: string
values: number[]
color?: string
}
}
type RoseChartInput = {
data: DataPoint[]
radius?: string | [string, string]
}
const axisLine = { lineStyle: { color: '#35506c' } }
const axisLabel = { color: '#9fb4cc' }
const splitLine = { lineStyle: { color: '#173047' } }
function deepMerge<T extends Record<string, unknown>>(base: T, patch?: Partial<T>): T {
if (!patch) {
return base
}
const output: Record<string, unknown> = { ...base }
for (const [key, value] of Object.entries(patch)) {
const existing = output[key]
if (
value &&
existing &&
typeof value === 'object' &&
typeof existing === 'object' &&
!Array.isArray(value) &&
!Array.isArray(existing)
) {
output[key] = deepMerge(existing as Record<string, unknown>, value as Record<string, unknown>)
} else {
output[key] = value
}
}
return output as T
}
export function mergeChartOption(base: EChartsOption, patch?: EChartsOption): EChartsOption {
return deepMerge(base as Record<string, unknown>, patch as Record<string, unknown> | undefined) as EChartsOption
}
export function createLineOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
const series = 'series' in input
? input.series.map((item) => ({
@@ -126,6 +177,100 @@ export function createBarOption(input: AxisChartInput | MultiSeriesInput): EChar
})
}
export function createStackedBarOption(input: MultiSeriesInput): EChartsOption {
return withChartDefaults({
legend: { top: 0, textStyle: axisLabel },
xAxis: {
type: 'category',
data: input.categories,
axisLine,
axisLabel
},
yAxis: {
type: 'value',
splitLine,
axisLabel
},
series: input.series.map((item) => ({
name: item.name,
type: 'bar',
stack: 'total',
data: item.values,
barMaxWidth: 32,
itemStyle: { color: item.color ?? '#63a4ff', borderRadius: [4, 4, 0, 0] }
}))
})
}
export function createAreaTrendOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
return mergeChartOption(createLineOption(input), {
grid: {
top: '14%',
right: 24,
bottom: 28,
left: 40,
containLabel: true
},
series: ('series' in input ? input.series : [input]).map((item) => ({
name: item.name,
type: 'line',
smooth: true,
data: item.values,
showSymbol: false,
areaStyle: {
opacity: 0.24,
color: item.color ?? '#63a4ff'
},
lineStyle: { color: item.color ?? '#63a4ff', width: 3 }
}))
})
}
export function createDualAxisOption(input: DualAxisChartInput): EChartsOption {
return withChartDefaults({
legend: { top: 0, textStyle: axisLabel },
xAxis: {
type: 'category',
data: input.categories,
axisLine,
axisLabel
},
yAxis: [
{
type: 'value',
name: input.leftName,
splitLine,
axisLabel
},
{
type: 'value',
name: input.rightName,
splitLine: { show: false },
axisLabel
}
],
series: [
{
name: input.bar.name,
type: 'bar',
data: input.bar.values,
barMaxWidth: 28,
itemStyle: { color: input.bar.color ?? '#56d08a', borderRadius: [4, 4, 0, 0] }
},
{
name: input.line.name,
type: 'line',
yAxisIndex: 1,
smooth: true,
data: input.line.values,
symbolSize: 8,
lineStyle: { color: input.line.color ?? '#d8a13e', width: 3 },
itemStyle: { color: input.line.color ?? '#d8a13e' }
}
]
})
}
export function createRankBarOption(input: AxisChartInput): EChartsOption {
return withChartDefaults({
grid: {
@@ -190,6 +335,31 @@ export function createPieOption(input: PieChartInput): EChartsOption {
})
}
export function createRosePieOption(input: RoseChartInput): EChartsOption {
return withChartDefaults({
tooltip: {
trigger: 'item',
backgroundColor: '#101722',
borderColor: '#314154',
textStyle: { color: '#d7e4f2' }
},
legend: {
bottom: 0,
textStyle: axisLabel
},
series: [
{
type: 'pie',
roseType: 'area',
radius: input.radius ?? ['22%', '70%'],
center: ['50%', '44%'],
label: { color: '#d7e4f2' },
data: input.data
}
]
})
}
export function createGaugeOption(input: GaugeChartInput): EChartsOption {
const max = input.max ?? 100