diff --git a/web/src/components/ui/charts/BusterChartJS/core/animations/barDelayAnimation.ts b/web/src/components/ui/charts/BusterChartJS/core/animations/barDelayAnimation.ts index 3e2e164dd..9370d66d9 100644 --- a/web/src/components/ui/charts/BusterChartJS/core/animations/barDelayAnimation.ts +++ b/web/src/components/ui/charts/BusterChartJS/core/animations/barDelayAnimation.ts @@ -1,7 +1,9 @@ import { AnimationOptions, AnimationSpec } from 'chart.js'; -export const barDelayAnimation = (props?: { dataDelay?: number; datasetDelay?: number }) => { - const { dataDelay = 200, datasetDelay = 100 } = props || {}; +const MAX_DELAY = 1200; + +export const barDelayAnimation = (props?: { maxDelay?: number }) => { + const { maxDelay = MAX_DELAY } = props || {}; let delayed = false; return { onComplete: () => { @@ -9,13 +11,20 @@ export const barDelayAnimation = (props?: { dataDelay?: number; datasetDelay?: n }, delay: (context) => { let delay = 0; + const dataIndex = context.dataIndex; + const datasetIndex = context.datasetIndex; const numberOfDatasets = context.chart.data.datasets.length; - const numberOfDataPoints = context.chart.data.datasets[context.datasetIndex].data.length; + const numberOfDataPoints = context.chart.data.datasets[datasetIndex]?.data.length || 1; if (context.type === 'data' && context.mode === 'default' && !delayed) { - delay = context.dataIndex * dataDelay + context.datasetIndex * datasetDelay; - // Ensure the maximum delay is 1000ms - delay = Math.min(delay, 1000); + // Calculate a scaling factor to distribute delays evenly, with max total delay of 1500ms + + const totalSegments = numberOfDatasets * numberOfDataPoints - 1; // -1 because first element has 0 delay + const scalingFactor = totalSegments > 0 ? maxDelay / totalSegments : 0; + + // Calculate position in overall sequence (dataset index * points per dataset + datapoint index) + const sequencePosition = datasetIndex * numberOfDataPoints + dataIndex; + delay = sequencePosition * scalingFactor; } return delay; } diff --git a/web/src/components/ui/charts/BusterChartJS/hooks/useOptions/useOptions.tsx b/web/src/components/ui/charts/BusterChartJS/hooks/useOptions/useOptions.tsx index 53b1b04b8..474fb7a06 100644 --- a/web/src/components/ui/charts/BusterChartJS/hooks/useOptions/useOptions.tsx +++ b/web/src/components/ui/charts/BusterChartJS/hooks/useOptions/useOptions.tsx @@ -23,7 +23,6 @@ import { LINE_DECIMATION_THRESHOLD, TOOLTIP_THRESHOLD } from '../../../config'; -import { barDelayAnimation } from '../../core/animations/barDelayAnimation'; import { useAnimations } from './useAnimations'; interface UseOptionsProps { @@ -202,7 +201,6 @@ export const useOptions = ({ return { indexAxis: isHorizontalBar ? 'y' : 'x', - animation, backgroundColor: colors, borderColor: colors, scales, @@ -220,10 +218,11 @@ export const useOptions = ({ samples: LINE_DECIMATION_SAMPLES } }, + animation, ...chartOptions } satisfies ChartProps['options']; }, [ - animate, + animation, colors, scales, interaction, @@ -235,8 +234,7 @@ export const useOptions = ({ isHorizontalBar, goalLinesAnnotations, trendlineAnnotations, - tooltipOptions, - animate + tooltipOptions ]); return options; diff --git a/web/src/components/ui/charts/BusterChartJS/inspiration.txt b/web/src/components/ui/charts/BusterChartJS/inspiration.txt index e1ac59e08..dfb9cb4a1 100644 --- a/web/src/components/ui/charts/BusterChartJS/inspiration.txt +++ b/web/src/components/ui/charts/BusterChartJS/inspiration.txt @@ -1 +1,2 @@ -https://andrew-dev-p.github.io/chartjs-showcase/drill-down-bar.html \ No newline at end of file +https://andrew-dev-p.github.io/chartjs-showcase/drill-down-bar.html +https://github.com/Pecacheu/chartjs-filter-plugin \ No newline at end of file