2022-06-29 18:08:37 +05:00
|
|
|
import { ChartOffset, ChartRegistry } from '@components/d3/types'
|
|
|
|
|
|
|
|
export const renderNeedle = <DataType extends Record<string, unknown>>(
|
|
|
|
xAxis: (value: d3.NumberValue) => number,
|
|
|
|
yAxis: (value: d3.NumberValue) => number,
|
|
|
|
chart: ChartRegistry<DataType>,
|
|
|
|
data: DataType[],
|
|
|
|
height: number,
|
|
|
|
offset: ChartOffset
|
|
|
|
): DataType[] => {
|
|
|
|
if (chart.type !== 'needle') return data
|
|
|
|
|
|
|
|
data = data.filter(chart.y)
|
|
|
|
|
|
|
|
const currentNeedles = chart()
|
|
|
|
.selectAll('line')
|
|
|
|
.data(data)
|
|
|
|
|
|
|
|
currentNeedles.exit().remove()
|
|
|
|
currentNeedles.enter().append('line')
|
|
|
|
|
|
|
|
chart()
|
2022-07-25 14:30:24 +05:00
|
|
|
.selectAll<SVGLineElement, DataType>('line')
|
2022-06-29 18:08:37 +05:00
|
|
|
.transition()
|
|
|
|
.duration(chart.animDurationMs ?? 0)
|
2022-07-25 14:30:24 +05:00
|
|
|
.attr('x1', (d) => xAxis(chart.x(d)))
|
|
|
|
.attr('x2', (d) => xAxis(chart.x(d)))
|
2022-06-29 18:08:37 +05:00
|
|
|
.attr('y1', height - offset.bottom - offset.top)
|
2022-07-25 14:30:24 +05:00
|
|
|
.attr('y2', (d) => yAxis(chart.y(d)))
|
2022-06-30 09:07:14 +05:00
|
|
|
.attr('stroke-dasharray', String(chart.dash ?? ''))
|
2022-06-29 18:08:37 +05:00
|
|
|
|
|
|
|
return data
|
|
|
|
}
|