Skip to content

Commit 5f9d946

Browse files
authored
fix(axis): disable nice adaptive ticks (#2689)
1 parent e91f70a commit 5f9d946

4 files changed

Lines changed: 60 additions & 1 deletion

File tree

22.9 KB
Loading

packages/charts/src/chart_types/xy_chart/state/selectors/visible_ticks.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export type GetMeasuredTicks = (
5252

5353
type Projections = Map<AxisId, Projection>;
5454

55-
const adaptiveTickCount = true;
55+
const USE_ADAPTIVE_TICK_COUNT = true;
5656

5757
function axisMinMax(axisPosition: Position, chartRotation: Rotation, { width, height }: Size): [number, number] {
5858
const horizontal = isHorizontalAxis(axisPosition);
@@ -117,6 +117,7 @@ function getVisibleTicks(
117117
layer: number | undefined,
118118
detailedLayer: number,
119119
ticks: (number | string)[],
120+
adaptiveTickCount: boolean,
120121
multilayerTimeAxis: boolean = false,
121122
showGrid = true,
122123
): AxisTick[] {
@@ -200,6 +201,7 @@ function getVisibleTickSet(
200201
detailedLayer: number,
201202
ticks: (number | string)[],
202203
labelFormatter: AxisLabelFormatter,
204+
adaptiveTickCount: boolean,
203205
multilayerTimeAxis = false,
204206
showGrid = true,
205207
): AxisTick[] {
@@ -218,6 +220,7 @@ function getVisibleTickSet(
218220
layer,
219221
detailedLayer,
220222
ticks,
223+
adaptiveTickCount,
221224
multilayerTimeAxis,
222225
showGrid,
223226
);
@@ -260,6 +263,8 @@ function getVisibleTickSets(
260263
const multilayerTimeAxis = isMultilayerTimeAxis(axisSpec, scaleConfigs.x.type, chartRotation);
261264
// TODO: remove this fallback when integersOnly is removed
262265
const maximumFractionDigits = mfd ?? (integersOnly ? 0 : undefined);
266+
const isNice = (isXAxis ? scaleConfigs.x.nice : scaleConfigs.y[groupId]?.nice) ?? false;
267+
const adaptiveTickCount = !isNice && USE_ADAPTIVE_TICK_COUNT;
263268

264269
const getMeasuredTicks = (
265270
scale: ScaleBand | ScaleContinuous,
@@ -282,6 +287,7 @@ function getVisibleTickSets(
282287
detailedLayer,
283288
ticks,
284289
labelFormatter,
290+
adaptiveTickCount,
285291
multilayerTimeAxis,
286292
showGrid,
287293
),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { boolean } from '@storybook/addon-knobs';
10+
import React from 'react';
11+
12+
import { Axis, Chart, LineSeries, Position, ScaleType, Settings } from '@elastic/charts';
13+
import { getRandomNumberGenerator } from '@elastic/charts/src/mocks/utils';
14+
15+
import type { ChartsStory } from '../../types';
16+
import { useBaseTheme } from '../../use_base_theme';
17+
18+
const rng = getRandomNumberGenerator();
19+
20+
// This data is carefully picked to trigger adaptive tick placements
21+
// See https://github.com/elastic/elastic-charts/issues/2687
22+
const data = new Array(20).fill(1).map((_, i) => ({
23+
x: i === 0 ? -1e6 : (i - 1) * 13e6,
24+
y: (i === 0 ? 0 : i === 2 ? -5.2 : i === 12 ? 21 : rng(-4, 20)) * 1e5,
25+
}));
26+
27+
export const Example: ChartsStory = (_, { title, description }) => {
28+
const xNice = boolean('Nice x ticks', true);
29+
const yNice = boolean('Nice y ticks', true);
30+
31+
return (
32+
<Chart title={title} description={description}>
33+
<Settings baseTheme={useBaseTheme()} />
34+
<Axis id="x" position={Position.Bottom} />
35+
<Axis id="y" position={Position.Left} style={{ tickLabel: { rotation: -90 } }} />
36+
<LineSeries
37+
id="line-1"
38+
xScaleType={ScaleType.Linear}
39+
yScaleType={ScaleType.Linear}
40+
data={data}
41+
xNice={xNice}
42+
yNice={yNice}
43+
xAccessor="x"
44+
yAccessors={['y']}
45+
/>
46+
</Chart>
47+
);
48+
};
49+
50+
Example.parameters = {
51+
resize: true,
52+
};

storybook/stories/test_cases/test_cases.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ export { Example as startDayOfWeek } from './11_start_day_of_week.story';
2626
export { Example as logWithNegativeValues } from './12_log_with_negative_values.story';
2727
export { Example as pointStyleOverrides } from './13_point_style_overrides.story';
2828
export { Example as errorBoundary } from './14_error_boundary.story';
29+
export { Example as linearNicing } from './15_linear_nicing.story';
2930
export { Example as lensStressTest } from './33_lens_stress.story';

0 commit comments

Comments
 (0)