Skip to content

Commit 9de0d08

Browse files
committed
Use the correct scale with multiple axes
1 parent 2705a28 commit 9de0d08

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

packages/x-charts/src/ChartsAxisHighlight/ChartsXAxisHighlight.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client';
22
import * as React from 'react';
3-
import { getValueToPositionMapper, useXScale } from '../hooks/useScale';
3+
import { getValueToPositionMapper } from '../hooks/useScale';
44
import { isBandScale } from '../internals/isBandScale';
55
import { useSelector } from '../internals/store/useSelector';
66
import { useStore } from '../internals/store/useStore';
77
import {
8+
selectorChartsHighlightXAxisScale,
89
selectorChartsHighlightXAxisValue,
910
UseChartCartesianAxisSignature,
1011
} from '../internals/plugins/featurePlugins/useChartCartesianAxis';
@@ -24,10 +25,9 @@ export default function ChartsXHighlight(props: {
2425

2526
const { top, height } = useDrawingArea();
2627

27-
const xScale = useXScale();
28-
2928
const store = useStore<[UseChartCartesianAxisSignature]>();
3029
const axisXValue = useSelector(store, selectorChartsHighlightXAxisValue);
30+
const xScale = useSelector(store, selectorChartsHighlightXAxisScale);
3131

3232
const getXPosition = getValueToPositionMapper(xScale);
3333

packages/x-charts/src/ChartsAxisHighlight/ChartsYAxisHighlight.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use client';
22
import * as React from 'react';
3-
import { getValueToPositionMapper, useYScale } from '../hooks/useScale';
3+
import { getValueToPositionMapper } from '../hooks/useScale';
44
import { isBandScale } from '../internals/isBandScale';
55
import { useSelector } from '../internals/store/useSelector';
66
import { useStore } from '../internals/store/useStore';
77
import {
8+
selectorChartsHighlightYAxisScale,
89
selectorChartsHighlightYAxisValue,
910
UseChartCartesianAxisSignature,
1011
} from '../internals/plugins/featurePlugins/useChartCartesianAxis';
@@ -24,10 +25,9 @@ export default function ChartsYHighlight(props: {
2425

2526
const { left, width } = useDrawingArea();
2627

27-
const yScale = useYScale();
28-
2928
const store = useStore<[UseChartCartesianAxisSignature]>();
3029
const axisYValue = useSelector(store, selectorChartsHighlightYAxisValue);
30+
const yScale = useSelector(store, selectorChartsHighlightYAxisScale);
3131

3232
const getYPosition = getValueToPositionMapper(yScale);
3333

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianHighlight.selectors.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSelector } from '../../utils/selectors';
2-
import { CartesianAxisItemIdentifier } from '../../../../models/axis';
2+
import { CartesianAxisItemIdentifier, ChartsAxisProps } from '../../../../models/axis';
33
import { selectorChartXAxis, selectorChartYAxis } from './useChartCartesianAxisRendering.selectors';
44

55
import {
@@ -10,6 +10,7 @@ import {
1010
} from './useChartCartesianInteraction.selectors';
1111
import { ChartState } from '../../models/chart';
1212
import { UseChartCartesianAxisSignature } from './useChartCartesianAxis.types';
13+
import { ComputeResult } from './computeAxisValue';
1314

1415
const selectorChartControlledCartesianAxisHighlight = (
1516
state: ChartState<[], [UseChartCartesianAxisSignature]>,
@@ -79,3 +80,32 @@ export const selectorChartsHighlightYAxisValue = createSelector(
7980
return null;
8081
},
8182
);
83+
84+
/**
85+
* Get the scale of the axis with highlight if controlled. The default axis otherwise.
86+
* @param controlledItem The controlled value of highlightedAxis
87+
* @param axis The axis stats after all the processing
88+
* @returns axis scale
89+
*/
90+
const selectScale = (
91+
controlledItem: CartesianAxisItemIdentifier | null | undefined,
92+
axis: ComputeResult<ChartsAxisProps>,
93+
) => {
94+
if (controlledItem === undefined) {
95+
return axis.axis[axis.axisIds[0]].scale;
96+
}
97+
if (controlledItem !== null) {
98+
return axis.axis[controlledItem.axisId]?.scale;
99+
}
100+
return axis.axis[axis.axisIds[0]].scale;
101+
};
102+
103+
export const selectorChartsHighlightXAxisScale = createSelector(
104+
[selectorChartsControlledXAxisHighlight, selectorChartXAxis],
105+
selectScale,
106+
);
107+
108+
export const selectorChartsHighlightYAxisScale = createSelector(
109+
[selectorChartsControlledYAxisHighlight, selectorChartYAxis],
110+
selectScale,
111+
);

0 commit comments

Comments
 (0)