asb_cloud_front/src/components/d3/functions.ts

20 lines
657 B
TypeScript

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]
}