Skip to content

Commit 57851e5

Browse files
committed
fix: sync hover using step values
1 parent 13551ba commit 57851e5

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/components/ChartContainer.jsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ export const getComparisonData = (data1, data2, mode) => {
5353
});
5454
};
5555

56+
export const getActiveElementsAtStep = (datasets, step) => {
57+
const activeElements = [];
58+
datasets.forEach((dataset, datasetIndex) => {
59+
const index = dataset.data.findIndex(p => p.x === step);
60+
if (index !== -1) {
61+
activeElements.push({ datasetIndex, index });
62+
}
63+
});
64+
return activeElements;
65+
};
66+
5667
const ChartWrapper = ({ data, options, chartId, onRegisterChart, onSyncHover }) => {
5768
const chartRef = useRef(null);
5869

@@ -66,8 +77,10 @@ const ChartWrapper = ({ data, options, chartId, onRegisterChart, onSyncHover })
6677
const enhancedOptions = {
6778
...options,
6879
onHover: (event, activeElements) => {
69-
if (activeElements.length > 0) {
70-
const step = activeElements[0].index;
80+
if (activeElements.length > 0 && chartRef.current) {
81+
const { datasetIndex, index } = activeElements[0];
82+
const point = chartRef.current.data?.datasets?.[datasetIndex]?.data?.[index];
83+
const step = typeof point?.x === 'number' ? point.x : index;
7184
onSyncHover(step, chartId);
7285
} else {
7386
onSyncHover(null, chartId);
@@ -112,12 +125,7 @@ export default function ChartContainer({
112125
chart.tooltip.setActiveElements([]);
113126
chart.update('none');
114127
} else if (id !== sourceId) {
115-
const activeElements = [];
116-
chart.data.datasets.forEach((dataset, datasetIndex) => {
117-
if (dataset.data && dataset.data.length > step) {
118-
activeElements.push({ datasetIndex, index: step });
119-
}
120-
});
128+
const activeElements = getActiveElementsAtStep(chart.data.datasets, step);
121129
chart.setActiveElements(activeElements);
122130
chart.tooltip.setActiveElements(activeElements, { x: 0, y: 0 });
123131
chart.update('none');

src/components/__tests__/ChartContainer.test.jsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ vi.mock('chart.js', () => {
2626

2727
vi.mock('chartjs-plugin-zoom', () => ({ default: {} }));
2828

29-
import ChartContainer, { getComparisonData } from '../ChartContainer.jsx';
29+
import ChartContainer, { getComparisonData, getActiveElementsAtStep } from '../ChartContainer.jsx';
3030

3131
const sampleFile = {
3232
name: 'test.log',
@@ -94,4 +94,13 @@ describe('ChartContainer', () => {
9494
{ x: 3, y: 1 }
9595
]);
9696
});
97+
98+
it('finds active elements by step value', () => {
99+
const datasets = [
100+
{ data: [{ x: 2, y: 1 }, { x: 4, y: 2 }] },
101+
{ data: [{ x: 1, y: 3 }, { x: 2, y: 4 }, { x: 5, y: 6 }] }
102+
];
103+
const result = getActiveElementsAtStep(datasets, 2);
104+
expect(result).toEqual([{ datasetIndex: 0, index: 0 }, { datasetIndex: 1, index: 1 }]);
105+
});
97106
});

0 commit comments

Comments
 (0)