Добавлен файл с методами для D3 графиков

This commit is contained in:
goodmice 2022-07-11 12:47:43 +05:00
parent acddd0439a
commit d957ac6690
2 changed files with 46 additions and 10 deletions

View File

@ -0,0 +1,19 @@
import { range } from 'd3'
import { MinMax } from './types'
export const getChartClass = (key: string | number) => `chart-id-${key}`
export const getGroupClass = (key: string | number) => `group-id-${key}`
export const getByAccessor = <DataType extends Record<any, any>, R>(accessor: keyof DataType | ((d: DataType) => R)): ((d: DataType) => R) => {
if (typeof accessor === 'function')
return accessor
return (d) => d[accessor]
}
export const getTicks = (domain: MinMax, count: number) => {
const min = domain.min ?? 0
const max = domain.max ?? 0
const step = (max - min) / count
return [...range(min, max, step), max]
}

View File

@ -5,9 +5,11 @@ import {
D3TooltipSettings D3TooltipSettings
} from './plugins' } from './plugins'
export type AxisAccessor<DataType extends Record<string, any>> = keyof DataType | ((d: DataType) => any)
export type ChartAxis<DataType> = { export type ChartAxis<DataType> = {
type: 'linear' | 'time', type: 'linear' | 'time',
accessor: keyof DataType | ((d: DataType) => any) accessor: AxisAccessor<DataType>
unit?: ReactNode unit?: ReactNode
format?: (v: d3.NumberValue) => ReactNode format?: (v: d3.NumberValue) => ReactNode
} }
@ -47,6 +49,15 @@ export type AreaChartDataset = {
optimization?: boolean optimization?: boolean
} }
export type RectArea<DataType extends Record<string, number>> = {
type: 'rect_area'
minXAccessor?: AxisAccessor<DataType>
maxXAccessor?: AxisAccessor<DataType>
minYAccessor?: AxisAccessor<DataType>
maxYAccessor?: AxisAccessor<DataType>
data?: DataType[]
}
export type LineChartDataset = { export type LineChartDataset = {
type: 'line' type: 'line'
nullValues?: 'skip' | 'gap' | 'none' nullValues?: 'skip' | 'gap' | 'none'
@ -61,12 +72,15 @@ export type ChartDataset<DataType> = BaseChartDataset<DataType> & (
AreaChartDataset | AreaChartDataset |
LineChartDataset | LineChartDataset |
NeedleChartDataset | NeedleChartDataset |
PointChartDataset PointChartDataset |
RectArea<Record<string, any>>
) )
export type MinMax = { min?: number, max?: number }
export type ChartDomain = { export type ChartDomain = {
x: { min?: number, max?: number } x?: MinMax
y: { min?: number, max?: number } y?: MinMax
} }
export type ChartOffset = { export type ChartOffset = {
@ -76,14 +90,17 @@ export type ChartOffset = {
right: number right: number
} }
export type ChartTick<DataType> = {
visible?: boolean,
count?: number | d3.TimeInterval,
format?: (d: d3.NumberValue, idx: number, data?: DataType) => string,
color?: Property.Color
}
export type ChartTicks<DataType> = { export type ChartTicks<DataType> = {
color?: Property.Color color?: Property.Color
x?: { x?: ChartTick<DataType>
visible?: boolean, y?: ChartTick<DataType>
count?: number,
format?: (d: d3.NumberValue) => string,
}
y?: { visible?: boolean, count?: number }
} }
export type ChartRegistry<DataType> = ChartDataset<DataType> & { export type ChartRegistry<DataType> = ChartDataset<DataType> & {