Skip to content

Commit 862438d

Browse files
committed
Refine y-axis tick formatting
1 parent fdac7f3 commit 862438d

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/components/ChartContainer.jsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ export default function ChartContainer({
447447
return maxDecimals;
448448
}, [parsedData]);
449449

450+
const tickStep = useMemo(() => Math.pow(10, -yDecimalPlaces), [yDecimalPlaces]);
451+
450452
const chartOptions = useMemo(() => ({
451453
responsive: true,
452454
maintainAspectRatio: false,
@@ -562,14 +564,15 @@ export default function ChartContainer({
562564
title: { display: true, text: 'Value' },
563565
bounds: 'data',
564566
ticks: {
567+
stepSize: tickStep,
565568
callback: function (value) {
566569
return Number(value.toFixed(yDecimalPlaces));
567570
}
568571
}
569572
}
570573
},
571574
elements: { point: { radius: 0 } }
572-
}), [xRange, onXRangeChange, yDecimalPlaces]);
575+
}), [xRange, onXRangeChange, yDecimalPlaces, tickStep]);
573576

574577
const buildComparisonChartData = (dataArray) => {
575578
const baselineVal =
@@ -704,11 +707,13 @@ export default function ChartContainer({
704707
const showComparison = dataArray.length >= 2;
705708

706709
const yRange = calculateYRange(dataArray);
710+
const yMin = Math.floor(yRange.min / tickStep) * tickStep;
711+
const yMax = Math.ceil(yRange.max / tickStep) * tickStep;
707712
const options = {
708713
...chartOptions,
709714
scales: {
710715
...chartOptions.scales,
711-
y: { ...chartOptions.scales.y, min: yRange.min, max: yRange.max }
716+
y: { ...chartOptions.scales.y, min: yMin, max: yMax }
712717
}
713718
};
714719

@@ -717,12 +722,14 @@ export default function ChartContainer({
717722
if (showComparison) {
718723
const compResult = buildComparisonChartData(dataArray);
719724
stats = compResult.stats.length > 0 ? compResult.stats : null;
720-
const compRange = calculateYRange(compResult.datasets);
725+
const compRange = calculateYRange(compResult.datasets);
726+
const compMin = Math.floor(compRange.min / tickStep) * tickStep;
727+
const compMax = Math.ceil(compRange.max / tickStep) * tickStep;
721728
const compOptions = {
722729
...chartOptions,
723730
scales: {
724731
...chartOptions.scales,
725-
y: { ...chartOptions.scales.y, min: compRange.min, max: compRange.max }
732+
y: { ...chartOptions.scales.y, min: compMin, max: compMax }
726733
}
727734
};
728735
const compActions = (

src/components/__tests__/ChartContainer.test.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ describe('ChartContainer', () => {
109109
expect(opts.interaction.axis).toBe('x');
110110
expect(opts.plugins.tooltip.mode).toBe('nearest');
111111
expect(opts.plugins.tooltip.axis).toBe('x');
112+
expect(opts.scales.y.ticks.stepSize).toBe(0.1);
112113

113114
// simulate hover to trigger sync
114115
const hover = __lineProps[0].options.onHover;
@@ -140,6 +141,7 @@ describe('ChartContainer', () => {
140141
{}
141142
];
142143

144+
const startIndex = __lineProps.length;
143145
render(
144146
<ChartContainer
145147
files={files}
@@ -156,8 +158,9 @@ describe('ChartContainer', () => {
156158
screen.getByText(/metric3/);
157159

158160
// data range applied (start 1 end 3 => 2 points for loss)
159-
const currentProps = __lineProps.slice(-5);
161+
const currentProps = __lineProps.slice(startIndex);
160162
expect(currentProps[0].data.datasets[0].data).toHaveLength(2);
163+
expect(currentProps[0].options.scales.y.ticks.stepSize).toBe(1);
161164

162165
// trigger container mouse leave
163166
const container = screen.getAllByTestId('line-chart')[0].parentElement;

0 commit comments

Comments
 (0)