diff --git a/src/components/d3/renders/area.ts b/src/components/d3/renders/area.ts index 9de5819..441b764 100644 --- a/src/components/d3/renders/area.ts +++ b/src/components/d3/renders/area.ts @@ -3,6 +3,8 @@ import * as d3 from 'd3' import { ChartRegistry } from '@components/d3/types' import { makePointsOptimizator } from '@utils' +import { appendTransition } from './base' + export const renderArea = >( xAxis: (value: any) => number, yAxis: (value: any) => number, @@ -47,10 +49,8 @@ export const renderArea = >( if (chart().selectAll('path').empty()) chart().append('path') - chart().selectAll('path') - .transition() - .duration(chart.animDurationMs || 0) - .attr('d', area(data as any)) + appendTransition(chart().selectAll('path'), chart) + .attr('d', area(data)) .attr('stroke-dasharray', chart.dash ? String(chart.dash) : null) .attr('fill', chart.areaColor ?? null) diff --git a/src/components/d3/renders/base.ts b/src/components/d3/renders/base.ts new file mode 100644 index 0000000..93c1de6 --- /dev/null +++ b/src/components/d3/renders/base.ts @@ -0,0 +1,12 @@ +import * as d3 from 'd3' + +import { ChartRegistry } from '../types' + +export const appendTransition = ( + elms: d3.Selection, + chart: ChartRegistry +): d3.Selection => { + if (chart.animDurationMs && chart.animDurationMs > 0) + return elms.transition().duration(chart.animDurationMs) as any + return elms +} diff --git a/src/components/d3/renders/line.ts b/src/components/d3/renders/line.ts index abc9307..f9b0760 100644 --- a/src/components/d3/renders/line.ts +++ b/src/components/d3/renders/line.ts @@ -3,6 +3,8 @@ import * as d3 from 'd3' import { ChartRegistry } from '@components/d3/types' import { makePointsOptimizator } from '@utils' +import { appendTransition } from './base' + export const renderLine = >( xAxis: (value: any) => number, yAxis: (value: any) => number, @@ -35,11 +37,9 @@ export const renderLine = >( if (chart().selectAll('path').empty()) chart().append('path') - chart().selectAll('path') - .transition() - .duration(chart.animDurationMs ?? 0) + appendTransition(chart().selectAll('path'), chart) .attr('d', line(data)) - .attr('stroke-dasharray', String(chart.dash ?? '')) + .attr('stroke-dasharray', chart.dash ? String(chart.dash) : null) return data } diff --git a/src/components/d3/renders/needle.ts b/src/components/d3/renders/needle.ts index b3944bb..eedd5d2 100644 --- a/src/components/d3/renders/needle.ts +++ b/src/components/d3/renders/needle.ts @@ -1,5 +1,7 @@ import { ChartOffset, ChartRegistry } from '@components/d3/types' +import { appendTransition } from './base' + export const renderNeedle = >( xAxis: (value: d3.NumberValue) => number, yAxis: (value: d3.NumberValue) => number, @@ -19,10 +21,7 @@ export const renderNeedle = >( currentNeedles.exit().remove() currentNeedles.enter().append('line') - chart() - .selectAll('line') - .transition() - .duration(chart.animDurationMs ?? 0) + appendTransition(chart().selectAll('line'), chart) .attr('x1', (d) => xAxis(chart.x(d))) .attr('x2', (d) => xAxis(chart.x(d))) .attr('y1', height - offset.bottom - offset.top) diff --git a/src/components/d3/renders/points.ts b/src/components/d3/renders/points.ts index 674ac75..9788556 100644 --- a/src/components/d3/renders/points.ts +++ b/src/components/d3/renders/points.ts @@ -1,5 +1,7 @@ import { ChartRegistry, PointChartDataset } from '@components/d3/types' +import { appendTransition } from './base' + const defaultConfig: Required> = { radius: 3, shape: 'circle', @@ -10,6 +12,14 @@ const defaultConfig: Required> = { fillOpacity: 1, } +const getPointsRoot = (chart: ChartRegistry, embeded?: boolean): d3.Selection => { + const root = chart() + if (!embeded) return root + if (root.select('.points').empty()) + root.append('g').attr('class', 'points') + return root.select('.points') +} + export const renderPoint = >( xAxis: (value: any) => number, yAxis: (value: any) => number, @@ -24,19 +34,7 @@ export const renderPoint = >( config = { ...defaultConfig, ...chart } else return data - const getPointsRoot = (): d3.Selection => { - let root = chart() - if (embeded) { - if (root.select('.points').empty()) - root.append('g').attr('class', 'points') - root = root.select('.points') - } - return root - } - - getPointsRoot() - .transition() - .duration(chart.animDurationMs ?? 0) + appendTransition(getPointsRoot(chart, embeded), chart) .attr('stroke-width', config.strokeWidth) .attr('fill-opacity', config.fillOpacity) .attr('fill', config.fillColor) @@ -45,14 +43,14 @@ export const renderPoint = >( const shape = ['hline', 'vline'].includes(config.shape) ? 'line' : config.shape - const currentPoints = getPointsRoot() + const currentPoints = getPointsRoot(chart, embeded) .selectAll(shape) .data(data.filter(chart.y)) currentPoints.exit().remove() currentPoints.enter().append(shape) - const newPoints = getPointsRoot() + const newPoints = getPointsRoot(chart, embeded) .selectAll(shape) .transition() .duration(chart.animDurationMs ?? 0) diff --git a/src/components/d3/renders/rect_area.ts b/src/components/d3/renders/rect_area.ts index 762b86d..620d668 100644 --- a/src/components/d3/renders/rect_area.ts +++ b/src/components/d3/renders/rect_area.ts @@ -1,6 +1,8 @@ import { getByAccessor } from '@components/d3/functions' import { ChartRegistry } from '@components/d3/types' +import { appendTransition } from './base' + export const renderRectArea = >( xAxis: (value: d3.NumberValue) => number, yAxis: (value: d3.NumberValue) => number, @@ -28,8 +30,7 @@ export const renderRectArea = >( rects.exit().remove() rects.enter().append('rect') - const actualRects = chart() - .selectAll>('rect') + appendTransition(chart().selectAll>('rect'), chart) .attr('x1', (d) => xAxis(xMin(d))) .attr('x2', (d) => xAxis(xMax(d))) .attr('y1', (d) => yAxis(yMin(d)))