2022-06-27 05:53:16 +05:00
|
|
|
import { memo, useCallback, useEffect, useMemo, useState } from 'react'
|
2022-06-25 16:03:08 +05:00
|
|
|
import { useElementSize } from 'usehooks-ts'
|
|
|
|
import { Property } from 'csstype'
|
2022-06-27 05:53:16 +05:00
|
|
|
import { Empty } from 'antd'
|
2022-06-25 16:03:08 +05:00
|
|
|
import * as d3 from 'd3'
|
|
|
|
|
|
|
|
import LoaderPortal from '@components/LoaderPortal'
|
2022-06-29 18:08:37 +05:00
|
|
|
import { isDev, usePartialProps } from '@utils'
|
2022-06-25 16:03:08 +05:00
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
import D3MouseZone from './D3MouseZone'
|
2022-07-11 12:53:40 +05:00
|
|
|
import { getChartClass } from './functions'
|
2022-06-29 18:08:37 +05:00
|
|
|
import {
|
2022-07-02 13:14:43 +05:00
|
|
|
renderArea,
|
2022-06-29 18:08:37 +05:00
|
|
|
renderLine,
|
|
|
|
renderPoint,
|
|
|
|
renderNeedle
|
|
|
|
} from './renders'
|
2022-06-25 16:03:08 +05:00
|
|
|
import {
|
|
|
|
BasePluginSettings,
|
|
|
|
D3ContextMenu,
|
|
|
|
D3ContextMenuSettings,
|
|
|
|
D3Cursor,
|
|
|
|
D3CursorSettings,
|
2022-06-29 18:08:37 +05:00
|
|
|
D3Legend,
|
|
|
|
D3LegendSettings,
|
2022-06-25 16:03:08 +05:00
|
|
|
D3Tooltip,
|
2022-06-27 05:53:16 +05:00
|
|
|
D3TooltipSettings,
|
2022-06-25 16:03:08 +05:00
|
|
|
} from './plugins'
|
2022-06-29 18:08:37 +05:00
|
|
|
import type {
|
|
|
|
ChartAxis,
|
|
|
|
ChartDataset,
|
|
|
|
ChartDomain,
|
|
|
|
ChartOffset,
|
|
|
|
ChartRegistry,
|
|
|
|
ChartTicks
|
|
|
|
} from './types'
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
import '@styles/d3.less'
|
|
|
|
|
2022-06-27 05:53:16 +05:00
|
|
|
const defaultOffsets: ChartOffset = {
|
|
|
|
top: 10,
|
|
|
|
bottom: 30,
|
|
|
|
left: 50,
|
|
|
|
right: 10,
|
2022-06-25 16:03:08 +05:00
|
|
|
}
|
|
|
|
|
2022-07-02 13:14:43 +05:00
|
|
|
export const getByAccessor = <DataType extends Record<any, any>, R>(accessor: keyof DataType | ((d: DataType) => R)): ((d: DataType) => R) => {
|
2022-06-27 05:53:16 +05:00
|
|
|
if (typeof accessor === 'function')
|
|
|
|
return accessor
|
2022-06-29 18:08:37 +05:00
|
|
|
return (d) => d[accessor]
|
2022-06-25 16:03:08 +05:00
|
|
|
}
|
|
|
|
|
2022-06-27 05:53:16 +05:00
|
|
|
const createAxis = <DataType,>(config: ChartAxis<DataType>) => {
|
|
|
|
if (config.type === 'time')
|
|
|
|
return d3.scaleTime()
|
|
|
|
return d3.scaleLinear()
|
2022-06-25 16:03:08 +05:00
|
|
|
}
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
export type D3ChartProps<DataType> = React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement> & {
|
2022-06-25 16:03:08 +05:00
|
|
|
xAxis: ChartAxis<DataType>
|
|
|
|
datasets: ChartDataset<DataType>[]
|
2022-06-27 08:11:49 +05:00
|
|
|
data?: DataType[] | Record<string, DataType[]>
|
2022-06-25 16:03:08 +05:00
|
|
|
domain?: Partial<ChartDomain>
|
|
|
|
width?: number | string
|
|
|
|
height?: number | string
|
|
|
|
loading?: boolean
|
|
|
|
offset?: Partial<ChartOffset>
|
|
|
|
animDurationMs?: number
|
|
|
|
backgroundColor?: Property.Color
|
2022-06-27 08:11:49 +05:00
|
|
|
ticks?: ChartTicks<DataType>
|
2022-06-25 16:03:08 +05:00
|
|
|
plugins?: {
|
|
|
|
menu?: BasePluginSettings & D3ContextMenuSettings
|
|
|
|
tooltip?: BasePluginSettings & D3TooltipSettings<DataType>
|
|
|
|
cursor?: BasePluginSettings & D3CursorSettings
|
2022-06-29 18:08:37 +05:00
|
|
|
legend?: BasePluginSettings & D3LegendSettings
|
2022-06-25 16:03:08 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
const getDefaultXAxisConfig = <DataType,>(): ChartAxis<DataType> => ({
|
|
|
|
type: 'time',
|
|
|
|
accessor: (d: any) => new Date(d.date)
|
|
|
|
})
|
|
|
|
|
|
|
|
const _D3Chart = <DataType extends Record<string, unknown>>({
|
2022-06-25 16:03:08 +05:00
|
|
|
className = '',
|
|
|
|
xAxis: _xAxisConfig,
|
|
|
|
datasets,
|
|
|
|
data,
|
|
|
|
domain,
|
|
|
|
width: givenWidth = '100%',
|
|
|
|
height: givenHeight = '100%',
|
|
|
|
loading,
|
|
|
|
offset: _offset,
|
|
|
|
animDurationMs = 200,
|
|
|
|
backgroundColor = 'transparent',
|
|
|
|
ticks,
|
|
|
|
plugins,
|
|
|
|
...other
|
2022-06-29 18:08:37 +05:00
|
|
|
}: D3ChartProps<DataType>) => {
|
|
|
|
const xAxisConfig = usePartialProps<ChartAxis<DataType>>(_xAxisConfig, getDefaultXAxisConfig)
|
2022-06-25 16:03:08 +05:00
|
|
|
const offset = usePartialProps(_offset, defaultOffsets)
|
|
|
|
|
|
|
|
const [svgRef, setSvgRef] = useState<SVGSVGElement | null>(null)
|
|
|
|
const [xAxisRef, setXAxisRef] = useState<SVGGElement | null>(null)
|
|
|
|
const [yAxisRef, setYAxisRef] = useState<SVGGElement | null>(null)
|
|
|
|
const [chartAreaRef, setChartAreaRef] = useState<SVGGElement | null>(null)
|
|
|
|
|
|
|
|
const xAxisArea = useCallback(() => d3.select(xAxisRef), [xAxisRef])
|
|
|
|
const yAxisArea = useCallback(() => d3.select(yAxisRef), [yAxisRef])
|
|
|
|
const chartArea = useCallback(() => d3.select(chartAreaRef), [chartAreaRef])
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
const [charts, setCharts] = useState<ChartRegistry<DataType>[]>([])
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
const [rootRef, { width, height }] = useElementSize()
|
|
|
|
|
|
|
|
const xAxis = useMemo(() => {
|
|
|
|
if (!data) return
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
const getX = getByAccessor(xAxisConfig.accessor)
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
const xAxis = createAxis(xAxisConfig)
|
|
|
|
xAxis.range([0, width - offset.left - offset.right])
|
|
|
|
|
2022-06-27 08:11:49 +05:00
|
|
|
if (domain?.x?.min && domain?.x?.max) {
|
|
|
|
xAxis.domain([domain.x.min, domain.x.max])
|
|
|
|
return xAxis
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Array.isArray(data)) {
|
|
|
|
const [minX, maxX] = d3.extent(data, getX)
|
|
|
|
xAxis.domain([domain?.x?.min ?? minX, domain?.x?.max ?? maxX])
|
|
|
|
} else {
|
|
|
|
let [minX, maxX] = [Infinity, -Infinity]
|
|
|
|
for (const key in data) {
|
|
|
|
const [min, max] = d3.extent(data[key], getX)
|
|
|
|
if (min < minX) minX = min
|
|
|
|
if (max > maxX) maxX = max
|
|
|
|
}
|
|
|
|
xAxis.domain([domain?.x?.min ?? minX, domain?.x?.max ?? maxX])
|
|
|
|
}
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
return xAxis
|
2022-06-29 18:08:37 +05:00
|
|
|
}, [xAxisConfig, data, domain, width, offset])
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
const yAxis = useMemo(() => {
|
|
|
|
if (!data) return
|
|
|
|
|
|
|
|
const yAxis = d3.scaleLinear()
|
|
|
|
|
|
|
|
if (domain?.y) {
|
|
|
|
const { min, max } = domain.y
|
|
|
|
if (min && max && Number.isFinite(min + max)) {
|
|
|
|
yAxis.domain([min, max])
|
|
|
|
return yAxis
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let minY = Infinity
|
|
|
|
let maxY = -Infinity
|
2022-06-27 08:11:49 +05:00
|
|
|
if (Array.isArray(data)) {
|
|
|
|
charts.forEach(({ y }) => {
|
|
|
|
const [min, max] = d3.extent(data, y)
|
|
|
|
if (min && min < minY) minY = min
|
|
|
|
if (max && max > maxY) maxY = max
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
for (const key in data) {
|
|
|
|
const chart = charts.find((chart) => chart.key === key)
|
|
|
|
if (!chart) continue
|
|
|
|
const [min, max] = d3.extent(data[key], chart.y)
|
|
|
|
if (min && min < minY) minY = min
|
|
|
|
if (max && max > maxY) maxY = max
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
yAxis.domain([
|
|
|
|
domain?.y?.min ?? minY,
|
|
|
|
domain?.y?.max ?? maxY,
|
|
|
|
])
|
|
|
|
|
|
|
|
yAxis.range([height - offset.top - offset.bottom, 0])
|
|
|
|
|
|
|
|
return yAxis
|
|
|
|
}, [charts, data, domain, height, offset])
|
|
|
|
|
2022-07-11 12:53:40 +05:00
|
|
|
const nTicks = {
|
|
|
|
color: 'lightgray',
|
|
|
|
...ticks,
|
|
|
|
x: {
|
|
|
|
visible: false,
|
|
|
|
format: (d: any) => String(d),
|
|
|
|
count: 10,
|
|
|
|
...ticks?.x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
useEffect(() => { // Рисуем ось X
|
|
|
|
if (!xAxis) return
|
|
|
|
xAxisArea().transition()
|
|
|
|
.duration(animDurationMs)
|
|
|
|
.call(d3.axisBottom(xAxis)
|
2022-07-11 12:53:40 +05:00
|
|
|
.tickSize(nTicks.x.visible ? -height + offset.bottom : 0)
|
|
|
|
.tickFormat((d, i) => nTicks.x.format(d, i))
|
|
|
|
.ticks(nTicks.x.count) as any // TODO: Исправить тип
|
2022-06-25 16:03:08 +05:00
|
|
|
)
|
|
|
|
|
2022-07-11 12:53:40 +05:00
|
|
|
xAxisArea().selectAll('.tick line').attr('stroke', nTicks.x.color || nTicks.color)
|
2022-06-25 16:03:08 +05:00
|
|
|
}, [xAxisArea, xAxis, animDurationMs, height, offset, ticks])
|
|
|
|
|
|
|
|
useEffect(() => { // Рисуем ось Y
|
|
|
|
if (!yAxis) return
|
2022-07-11 12:53:40 +05:00
|
|
|
|
|
|
|
const nTicks = {
|
|
|
|
color: 'lightgray',
|
|
|
|
...ticks,
|
|
|
|
y: {
|
|
|
|
visible: false,
|
|
|
|
format: (d: any) => String(d),
|
|
|
|
count: 10,
|
|
|
|
...ticks?.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
yAxisArea().transition()
|
|
|
|
.duration(animDurationMs)
|
|
|
|
.call(d3.axisLeft(yAxis)
|
2022-07-11 12:53:40 +05:00
|
|
|
.tickSize(nTicks.y.visible ? -width + offset.left + offset.right : 0)
|
|
|
|
.tickFormat((d, i) => nTicks.y.format(d, i))
|
|
|
|
.ticks(nTicks.y.count) as any // TODO: Исправить тип
|
2022-06-25 16:03:08 +05:00
|
|
|
)
|
|
|
|
|
2022-07-11 12:53:40 +05:00
|
|
|
yAxisArea().selectAll('.tick line').attr('stroke', nTicks.y.color || nTicks.color)
|
2022-06-25 16:03:08 +05:00
|
|
|
}, [yAxisArea, yAxis, animDurationMs, width, offset, ticks])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-26 17:16:12 +05:00
|
|
|
if (isDev())
|
|
|
|
for (let i = 0; i < datasets.length - 1; i++)
|
|
|
|
for (let j = i + 1; j < datasets.length; j++)
|
|
|
|
if (datasets[i].key === datasets[j].key)
|
|
|
|
console.warn(`Ключ датасета "${datasets[i].key}" неуникален (индексы ${i} и ${j})!`)
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
setCharts((oldCharts) => {
|
2022-06-29 18:08:37 +05:00
|
|
|
const charts: ChartRegistry<DataType>[] = []
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
for (const chart of oldCharts) { // Удаляем ненужные графики
|
|
|
|
if (datasets.find(({ key }) => key === chart.key))
|
|
|
|
charts.push(chart)
|
|
|
|
else
|
|
|
|
chart().remove()
|
|
|
|
}
|
|
|
|
|
|
|
|
datasets.forEach((dataset) => { // Добавляем новые
|
|
|
|
let chartIdx = charts.findIndex(({ key }) => key === dataset.key)
|
|
|
|
if (chartIdx < 0)
|
|
|
|
chartIdx = charts.length
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
const newChart: ChartRegistry<DataType> = Object.assign(
|
2022-07-11 12:53:40 +05:00
|
|
|
() => chartArea().select('.' + getChartClass(dataset.key)) as d3.Selection<SVGGElement, DataType, any, unknown>,
|
2022-06-25 16:03:08 +05:00
|
|
|
{
|
2022-06-29 18:08:37 +05:00
|
|
|
width: 1,
|
|
|
|
opacity: 1,
|
|
|
|
label: dataset.key,
|
|
|
|
color: 'gray',
|
|
|
|
animDurationMs,
|
2022-06-25 16:03:08 +05:00
|
|
|
...dataset,
|
2022-06-27 05:53:16 +05:00
|
|
|
xAxis: dataset.xAxis ?? xAxisConfig,
|
|
|
|
y: getByAccessor(dataset.yAxis.accessor),
|
|
|
|
x: getByAccessor(dataset.xAxis?.accessor ?? xAxisConfig.accessor),
|
2022-06-25 16:03:08 +05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
if (newChart.type === 'line')
|
|
|
|
newChart.optimization = false
|
|
|
|
|
2022-06-25 16:03:08 +05:00
|
|
|
if (!newChart().node())
|
|
|
|
chartArea()
|
|
|
|
.append('g')
|
2022-07-11 12:53:40 +05:00
|
|
|
.attr('class', getChartClass(newChart.key))
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
charts[chartIdx] = newChart
|
|
|
|
})
|
|
|
|
|
|
|
|
return charts
|
|
|
|
})
|
2022-06-29 18:08:37 +05:00
|
|
|
}, [xAxisConfig, chartArea, datasets, animDurationMs])
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
const redrawCharts = useCallback(() => {
|
|
|
|
if (!data || !xAxis || !yAxis) return
|
|
|
|
|
|
|
|
charts.forEach((chart) => {
|
|
|
|
chart()
|
2022-06-29 18:08:37 +05:00
|
|
|
.attr('color', chart.color || null)
|
2022-06-27 05:53:16 +05:00
|
|
|
.attr('stroke', 'currentColor')
|
2022-06-29 18:08:37 +05:00
|
|
|
.attr('stroke-width', chart.width ?? null)
|
|
|
|
.attr('opacity', chart.opacity ?? null)
|
2022-06-25 16:03:08 +05:00
|
|
|
.attr('fill', 'none')
|
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
let chartData = Array.isArray(data) ? data : data[String(chart.key).split(':')[0]]
|
|
|
|
if (!chartData) return
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
switch (chart.type) {
|
2022-06-26 17:16:12 +05:00
|
|
|
case 'needle':
|
2022-06-29 18:08:37 +05:00
|
|
|
chartData = renderNeedle<DataType>(xAxis, yAxis, chart, chartData, height, offset)
|
2022-06-25 16:03:08 +05:00
|
|
|
break
|
2022-06-29 18:08:37 +05:00
|
|
|
case 'line':
|
|
|
|
chartData = renderLine<DataType>(xAxis, yAxis, chart, chartData)
|
|
|
|
break
|
|
|
|
case 'point':
|
|
|
|
chartData = renderPoint<DataType>(xAxis, yAxis, chart, chartData)
|
2022-06-25 16:03:08 +05:00
|
|
|
break
|
2022-07-02 13:14:43 +05:00
|
|
|
case 'area':
|
|
|
|
chartData = renderArea<DataType>(xAxis, yAxis, chart, chartData)
|
|
|
|
break
|
2022-06-25 16:03:08 +05:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-06-26 17:16:12 +05:00
|
|
|
|
2022-06-29 18:08:37 +05:00
|
|
|
if (chart.point)
|
2022-07-03 19:30:50 +05:00
|
|
|
renderPoint<DataType>(xAxis, yAxis, chart, chartData, true)
|
2022-06-29 18:08:37 +05:00
|
|
|
|
2022-06-27 08:11:49 +05:00
|
|
|
chart.afterDraw?.(chart)
|
2022-06-25 16:03:08 +05:00
|
|
|
})
|
2022-06-29 18:08:37 +05:00
|
|
|
}, [charts, data, xAxis, yAxis, height, offset])
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
redrawCharts()
|
|
|
|
}, [redrawCharts])
|
|
|
|
|
|
|
|
return (
|
2022-06-27 08:11:49 +05:00
|
|
|
<LoaderPortal
|
|
|
|
show={loading}
|
|
|
|
style={{
|
|
|
|
width: givenWidth,
|
|
|
|
height: givenHeight,
|
|
|
|
}}
|
|
|
|
>
|
2022-06-25 16:03:08 +05:00
|
|
|
<div
|
|
|
|
{...other}
|
|
|
|
ref={rootRef}
|
|
|
|
className={`asb-d3-chart ${className}`}
|
|
|
|
>
|
|
|
|
{data ? (
|
|
|
|
<D3ContextMenu {...plugins?.menu} svg={svgRef}>
|
|
|
|
<svg ref={setSvgRef} width={'100%'} height={'100%'}>
|
|
|
|
<g ref={setXAxisRef} className={'axis x'} transform={`translate(${offset.left}, ${height - offset.bottom})`} />
|
|
|
|
<g ref={setYAxisRef} className={'axis y'} transform={`translate(${offset.left}, ${offset.top})`} />
|
|
|
|
<g ref={setChartAreaRef} className={'chart-area'} transform={`translate(${offset.left}, ${offset.top})`}>
|
2022-06-29 18:08:37 +05:00
|
|
|
<rect
|
|
|
|
width={Math.max(width - offset.left - offset.right, 0)}
|
|
|
|
height={Math.max(height - offset.top - offset.bottom, 0)}
|
|
|
|
fill={backgroundColor}
|
|
|
|
/>
|
2022-06-25 16:03:08 +05:00
|
|
|
</g>
|
|
|
|
<D3MouseZone width={width} height={height} offset={offset}>
|
2022-06-29 18:08:37 +05:00
|
|
|
<D3Cursor {...plugins?.cursor} />
|
|
|
|
<D3Legend<DataType> charts={charts} {...plugins?.legend} />
|
|
|
|
<D3Tooltip<DataType> charts={charts} {...plugins?.tooltip} />
|
2022-06-25 16:03:08 +05:00
|
|
|
</D3MouseZone>
|
|
|
|
</svg>
|
|
|
|
</D3ContextMenu>
|
|
|
|
) : (
|
|
|
|
<div className={'chart-empty'}>
|
|
|
|
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</LoaderPortal>
|
|
|
|
)
|
2022-06-29 18:08:37 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
export const D3Chart = memo(_D3Chart) as typeof _D3Chart
|
2022-06-25 16:03:08 +05:00
|
|
|
|
|
|
|
export default D3Chart
|