Initial screen starter scaffold
This commit is contained in:
commit
d136b9dddc
49 files changed
+2966
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import * as echarts from 'echarts'
|
||||
import { useEffect, useRef } from 'react'
|
||||
|
||||
type EChartProps = {
|
||||
option: echarts.EChartsOption
|
||||
loading?: boolean
|
||||
className?: string
|
||||
}
|
||||
|
||||
export function EChart({ option, loading = false, className = '' }: EChartProps) {
|
||||
const hostRef = useRef<HTMLDivElement | null>(null)
|
||||
const chartRef = useRef<echarts.ECharts | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!hostRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
chartRef.current = echarts.init(hostRef.current)
|
||||
const observer = new ResizeObserver(() => chartRef.current?.resize())
|
||||
observer.observe(hostRef.current)
|
||||
|
||||
return () => {
|
||||
observer.disconnect()
|
||||
chartRef.current?.dispose()
|
||||
chartRef.current = null
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
chartRef.current?.setOption(option, true)
|
||||
}, [option])
|
||||
|
||||
useEffect(() => {
|
||||
if (loading) {
|
||||
chartRef.current?.showLoading()
|
||||
return
|
||||
}
|
||||
|
||||
chartRef.current?.hideLoading()
|
||||
}, [loading])
|
||||
|
||||
return <div className={`chart-host ${className}`} ref={hostRef} />
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
|
||||
export const baseChartTextStyle = {
|
||||
color: '#d7e4f2',
|
||||
fontFamily: 'Inter, ui-sans-serif, system-ui'
|
||||
}
|
||||
|
||||
export const withChartDefaults = (option: EChartsOption): EChartsOption => ({
|
||||
textStyle: baseChartTextStyle,
|
||||
grid: {
|
||||
top: 32,
|
||||
right: 24,
|
||||
bottom: 32,
|
||||
left: 48,
|
||||
containLabel: true
|
||||
},
|
||||
tooltip: {
|
||||
trigger: 'axis',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: {
|
||||
color: '#d7e4f2'
|
||||
}
|
||||
},
|
||||
...option
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './EChart'
|
||||
export * from './chartTheme'
|
||||
export * from './optionFactories'
|
||||
@@ -0,0 +1,263 @@
|
||||
import type { EChartsOption } from 'echarts'
|
||||
import { withChartDefaults } from './chartTheme'
|
||||
|
||||
type DataPoint = {
|
||||
name: string
|
||||
value: number
|
||||
}
|
||||
|
||||
type AxisChartInput = {
|
||||
categories: string[]
|
||||
values: number[]
|
||||
name?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
type MultiSeriesInput = {
|
||||
categories: string[]
|
||||
series: Array<{
|
||||
name: string
|
||||
values: number[]
|
||||
color?: string
|
||||
}>
|
||||
}
|
||||
|
||||
type PieChartInput = {
|
||||
data: DataPoint[]
|
||||
radius?: string | [string, string]
|
||||
}
|
||||
|
||||
type GaugeChartInput = {
|
||||
name?: string
|
||||
value: number
|
||||
min?: number
|
||||
max?: number
|
||||
unit?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
type RadarChartInput = {
|
||||
indicators: Array<{
|
||||
name: string
|
||||
max: number
|
||||
}>
|
||||
values: number[]
|
||||
name?: string
|
||||
color?: string
|
||||
}
|
||||
|
||||
const axisLine = { lineStyle: { color: '#35506c' } }
|
||||
const axisLabel = { color: '#9fb4cc' }
|
||||
const splitLine = { lineStyle: { color: '#173047' } }
|
||||
|
||||
export function createLineOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
|
||||
const series = 'series' in input
|
||||
? input.series.map((item) => ({
|
||||
name: item.name,
|
||||
type: 'line' as const,
|
||||
smooth: true,
|
||||
data: item.values,
|
||||
areaStyle: { color: `${item.color ?? '#63a4ff'}22` },
|
||||
lineStyle: { color: item.color ?? '#63a4ff', width: 3 },
|
||||
symbolSize: 8
|
||||
}))
|
||||
: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'line' as const,
|
||||
smooth: true,
|
||||
data: input.values,
|
||||
areaStyle: { color: `${input.color ?? '#63a4ff'}22` },
|
||||
lineStyle: { color: input.color ?? '#63a4ff', width: 3 },
|
||||
symbolSize: 8
|
||||
}
|
||||
]
|
||||
|
||||
return withChartDefaults({
|
||||
legend: 'series' in input ? { top: 0, textStyle: axisLabel } : undefined,
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
axisLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
series
|
||||
})
|
||||
}
|
||||
|
||||
export function createBarOption(input: AxisChartInput | MultiSeriesInput): EChartsOption {
|
||||
const series = 'series' in input
|
||||
? input.series.map((item) => ({
|
||||
name: item.name,
|
||||
type: 'bar' as const,
|
||||
data: item.values,
|
||||
barMaxWidth: 28,
|
||||
itemStyle: { color: item.color ?? '#63a4ff', borderRadius: [4, 4, 0, 0] }
|
||||
}))
|
||||
: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'bar' as const,
|
||||
data: input.values,
|
||||
barMaxWidth: 32,
|
||||
itemStyle: { color: input.color ?? '#63a4ff', borderRadius: [4, 4, 0, 0] }
|
||||
}
|
||||
]
|
||||
|
||||
return withChartDefaults({
|
||||
legend: 'series' in input ? { top: 0, textStyle: axisLabel } : undefined,
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
axisLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
series
|
||||
})
|
||||
}
|
||||
|
||||
export function createRankBarOption(input: AxisChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
grid: {
|
||||
top: 16,
|
||||
right: 24,
|
||||
bottom: 16,
|
||||
left: 80,
|
||||
containLabel: true
|
||||
},
|
||||
xAxis: {
|
||||
type: 'value',
|
||||
splitLine,
|
||||
axisLabel
|
||||
},
|
||||
yAxis: {
|
||||
type: 'category',
|
||||
data: input.categories,
|
||||
inverse: true,
|
||||
axisLine: { show: false },
|
||||
axisTick: { show: false },
|
||||
axisLabel
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'bar',
|
||||
data: input.values,
|
||||
barMaxWidth: 18,
|
||||
label: {
|
||||
show: true,
|
||||
position: 'right',
|
||||
color: '#d7e4f2'
|
||||
},
|
||||
itemStyle: { color: input.color ?? '#63a4ff', borderRadius: [0, 4, 4, 0] }
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export function createPieOption(input: PieChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: { color: '#d7e4f2' }
|
||||
},
|
||||
legend: {
|
||||
bottom: 0,
|
||||
textStyle: axisLabel
|
||||
},
|
||||
series: [
|
||||
{
|
||||
type: 'pie',
|
||||
radius: input.radius ?? ['46%', '68%'],
|
||||
center: ['50%', '45%'],
|
||||
avoidLabelOverlap: true,
|
||||
label: { color: '#d7e4f2' },
|
||||
data: input.data
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
export function createGaugeOption(input: GaugeChartInput): EChartsOption {
|
||||
const max = input.max ?? 100
|
||||
|
||||
return {
|
||||
tooltip: {
|
||||
formatter: `{b}: {c}${input.unit ?? ''}`
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'gauge',
|
||||
min: input.min ?? 0,
|
||||
max,
|
||||
progress: {
|
||||
show: true,
|
||||
width: 14,
|
||||
itemStyle: { color: input.color ?? '#63a4ff' }
|
||||
},
|
||||
axisLine: {
|
||||
lineStyle: {
|
||||
width: 14,
|
||||
color: [[1, '#173047']]
|
||||
}
|
||||
},
|
||||
axisTick: { show: false },
|
||||
splitLine: { length: 8, lineStyle: { color: '#466886' } },
|
||||
axisLabel: { color: '#9fb4cc' },
|
||||
pointer: { width: 4 },
|
||||
detail: {
|
||||
formatter: `{value}${input.unit ?? ''}`,
|
||||
color: '#f0f7ff',
|
||||
fontSize: 32
|
||||
},
|
||||
data: [{ name: input.name, value: input.value }]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
export function createRadarOption(input: RadarChartInput): EChartsOption {
|
||||
return withChartDefaults({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: '#101722',
|
||||
borderColor: '#314154',
|
||||
textStyle: { color: '#d7e4f2' }
|
||||
},
|
||||
radar: {
|
||||
indicator: input.indicators,
|
||||
axisName: { color: '#9fb4cc' },
|
||||
splitLine: { lineStyle: { color: '#24425e' } },
|
||||
splitArea: { areaStyle: { color: ['rgba(36, 66, 94, 0.16)', 'rgba(36, 66, 94, 0.05)'] } },
|
||||
axisLine: { lineStyle: { color: '#24425e' } }
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: input.name,
|
||||
type: 'radar',
|
||||
data: [
|
||||
{
|
||||
name: input.name,
|
||||
value: input.values,
|
||||
areaStyle: { color: `${input.color ?? '#63a4ff'}33` },
|
||||
lineStyle: { color: input.color ?? '#63a4ff', width: 2 },
|
||||
itemStyle: { color: input.color ?? '#63a4ff' }
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user