Skip to content

[charts] improve tick rendering performance #17755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/x-charts/src/ChartsGrid/ChartsHorizontalGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function ChartsGridHorizontal(props: ChartsGridHorizontalProps) {

const { scale, tickNumber, tickInterval } = axis;

const yTicks = useTicks({ scale, tickNumber, tickInterval });
const yTicks = useTicks({ scale, tickNumber, tickInterval, direction: 'y' });

return (
<React.Fragment>
Expand Down
2 changes: 1 addition & 1 deletion packages/x-charts/src/ChartsGrid/ChartsVerticalGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function ChartsGridVertical(props: ChartsGridVerticalProps) {

const { scale, tickNumber, tickInterval } = axis;

const xTicks = useTicks({ scale, tickNumber, tickInterval });
const xTicks = useTicks({ scale, tickNumber, tickInterval, direction: 'x' });

return (
<React.Fragment>
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts/src/ChartsXAxis/ChartsXAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ function ChartsXAxis(inProps: ChartsXAxisProps) {
tickInterval,
tickPlacement,
tickLabelPlacement,
direction: 'x',
});

const visibleLabels = getVisibleLabels(xTicks, {
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts/src/ChartsYAxis/ChartsYAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function ChartsYAxis(inProps: ChartsYAxisProps) {
tickPlacement,
tickLabelPlacement,
tickInterval,
direction: 'y',
});

const positionSign = position === 'right' ? 1 : -1;
Expand Down
56 changes: 40 additions & 16 deletions packages/x-charts/src/hooks/useTicks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import * as React from 'react';
import { useChartContext } from '../context/ChartProvider';
import { AxisConfig, D3Scale } from '../models/axis';
import { isBandScale } from '../internals/isBandScale';
import { isInfinity } from '../internals/isInfinity';
Expand Down Expand Up @@ -67,6 +68,7 @@ export function useTicks(
options: {
scale: D3Scale;
valueFormatter?: AxisConfig['valueFormatter'];
direction: 'x' | 'y';
} & Pick<TickParams, 'tickNumber' | 'tickInterval' | 'tickPlacement' | 'tickLabelPlacement'>,
): TickItemType[] {
const {
Expand All @@ -76,7 +78,9 @@ export function useTicks(
tickInterval,
tickPlacement = 'extremities',
tickLabelPlacement: tickLabelPlacementProp,
direction,
} = options;
const { instance } = useChartContext();

return React.useMemo(() => {
// band scale
Expand Down Expand Up @@ -139,20 +143,40 @@ export function useTicks(
}
const tickLabelPlacement = tickLabelPlacementProp;
const ticks = typeof tickInterval === 'object' ? tickInterval : scale.ticks(tickNumber);
return ticks.map((value: any, i) => {
return {
value,
formattedValue:
valueFormatter?.(value, { location: 'tick', scale }) ??
scale.tickFormat(tickNumber)(value),
offset: scale(value),
// Allowing the label to be placed in the middle of a continuous scale is weird.
// But it is useful in some cases, like funnel categories with a linear scale.
labelOffset:
tickLabelPlacement === 'middle'
? scale(ticks[i - 1] ?? 0) - (scale(value) + scale(ticks[i - 1] ?? 0)) / 2
: 0,
};
});
}, [scale, tickInterval, tickNumber, valueFormatter, tickPlacement, tickLabelPlacementProp]);

// Ticks inside the drawing area
const visibleTicks: TickItemType[] = [];

for (let i = 0; i < ticks.length; i += 1) {
const value = ticks[i];
const offset = scale(value);

if (instance.isPointInside({ x: offset, y: offset }, { direction })) {
visibleTicks.push({
value,
formattedValue:
valueFormatter?.(value, { location: 'tick', scale }) ??
scale.tickFormat(tickNumber)(value),
offset,
// Allowing the label to be placed in the middle of a continuous scale is weird.
// But it is useful in some cases, like funnel categories with a linear scale.
labelOffset:
tickLabelPlacement === 'middle'
? scale(ticks[i - 1] ?? 0) - (offset + scale(ticks[i - 1] ?? 0)) / 2
: 0,
});
}
}

return visibleTicks;
}, [
scale,
tickLabelPlacementProp,
tickInterval,
tickNumber,
tickPlacement,
valueFormatter,
direction,
instance,
]);
}
2 changes: 1 addition & 1 deletion test/performance-charts/tests/ScatterChart.bench.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('ScatterChart', () => {
async () => {
const { findByText } = render(
<ScatterChart
xAxis={[{ data: xData, valueFormatter: (v) => v.toLocaleString('en-US') }]}
xAxis={[{ data: xData, valueFormatter: (v: number) => v.toLocaleString('en-US') }]}
series={[
{
data,
Expand Down