Skip to content

Commit 8bdea9f

Browse files
authored
Merge pull request #1756 from silx-kit/tick-format
Allow passing custom axis tick formatters to `VisCanvas`
2 parents 76d9679 + c3416c4 commit 8bdea9f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

apps/storybook/src/VisCanvas.stories.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ScaleType, VisCanvas } from '@h5web/lib';
22
import { type Meta, type StoryFn, type StoryObj } from '@storybook/react';
3+
import { format } from 'd3-format';
34

45
import FillHeight from './decorators/FillHeight';
56

@@ -34,6 +35,23 @@ export const NiceDomains = {
3435
},
3536
} satisfies Story;
3637

38+
export const TickFormatters = {
39+
args: {
40+
abscissaConfig: {
41+
visDomain: [-1.2, 2.8],
42+
showGrid: true,
43+
formatTick: (val) => {
44+
return Math.round(val) === val ? val.toString() : val.toFixed(3);
45+
},
46+
},
47+
ordinateConfig: {
48+
visDomain: [50, 100],
49+
showGrid: true,
50+
formatTick: format('.2e'),
51+
},
52+
},
53+
} satisfies Story;
54+
3755
export const LogScales = {
3856
args: {
3957
abscissaConfig: {

packages/lib/src/vis/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface AxisConfig {
4242
label?: string;
4343
flip?: boolean;
4444
nice?: boolean;
45+
formatTick?: (val: number) => string;
4546
}
4647

4748
export type VisScaleType = ColorScaleType | [ScaleType.Gamma, number];

packages/lib/src/vis/shared/Axis.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function Axis(props: Props) {
4949
showGrid,
5050
label,
5151
nice = false,
52+
formatTick,
5253
} = config;
5354
// Restrain ticks scales to visible domains
5455
const scale = createScale(scaleType, {
@@ -76,7 +77,9 @@ function Axis(props: Props) {
7677
>
7778
<AxisComponent
7879
scale={scale}
79-
tickFormat={getTickFormatter(domain, axisLength, scaleType)}
80+
tickFormat={
81+
formatTick || getTickFormatter(domain, axisLength, scaleType)
82+
}
8083
label={label}
8184
labelOffset={offset - (isAbscissa ? 32 : 36)}
8285
hideAxisLine={showGrid}

0 commit comments

Comments
 (0)