Skip to content

Commit

Permalink
Merge pull request #1756 from silx-kit/tick-format
Browse files Browse the repository at this point in the history
Allow passing custom axis tick formatters to `VisCanvas`
  • Loading branch information
axelboc authored Feb 6, 2025
2 parents 76d9679 + c3416c4 commit 8bdea9f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions apps/storybook/src/VisCanvas.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ScaleType, VisCanvas } from '@h5web/lib';
import { type Meta, type StoryFn, type StoryObj } from '@storybook/react';
import { format } from 'd3-format';

import FillHeight from './decorators/FillHeight';

Expand Down Expand Up @@ -34,6 +35,23 @@ export const NiceDomains = {
},
} satisfies Story;

export const TickFormatters = {
args: {
abscissaConfig: {
visDomain: [-1.2, 2.8],
showGrid: true,
formatTick: (val) => {
return Math.round(val) === val ? val.toString() : val.toFixed(3);
},
},
ordinateConfig: {
visDomain: [50, 100],
showGrid: true,
formatTick: format('.2e'),
},
},
} satisfies Story;

export const LogScales = {
args: {
abscissaConfig: {
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/vis/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface AxisConfig {
label?: string;
flip?: boolean;
nice?: boolean;
formatTick?: (val: number) => string;
}

export type VisScaleType = ColorScaleType | [ScaleType.Gamma, number];
Expand Down
5 changes: 4 additions & 1 deletion packages/lib/src/vis/shared/Axis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function Axis(props: Props) {
showGrid,
label,
nice = false,
formatTick,
} = config;
// Restrain ticks scales to visible domains
const scale = createScale(scaleType, {
Expand Down Expand Up @@ -76,7 +77,9 @@ function Axis(props: Props) {
>
<AxisComponent
scale={scale}
tickFormat={getTickFormatter(domain, axisLength, scaleType)}
tickFormat={
formatTick || getTickFormatter(domain, axisLength, scaleType)
}
label={label}
labelOffset={offset - (isAbscissa ? 32 : 36)}
hideAxisLine={showGrid}
Expand Down

0 comments on commit 8bdea9f

Please sign in to comment.