Skip to content

Commit

Permalink
fix: generate ticks with the correct domain
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Nov 1, 2022
1 parent a5d7ad4 commit 745edc1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
8 changes: 2 additions & 6 deletions src/hooks/useLinearPrimaryTicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ export function useLinearPrimaryTicks<
ref: MutableRefObject<SVGGElement | null>,
options: Options = {},
): PrimaryLinearTicks[] {
const [ticks, setTicks] = useState<number[]>([]);
const [ticks, setTicks] = useState<PrimaryLinearTicks[]>([]);
const { tickFormat = String } = options;
useTicks<number, ScaleContinuousNumeric<number, number>>(
scale,
direction,
ref,
{ ...options, tickFormat, setTicks },
);
return ticks.map((value) => ({
label: tickFormat(value),
position: scale(value),
value,
}));
return ticks;
}
12 changes: 10 additions & 2 deletions src/hooks/useTick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export interface Ticks<T> {

interface Options<T> {
tickFormat: (d: T) => string;
setTicks: Dispatch<SetStateAction<T[]>>;
setTicks: Dispatch<
SetStateAction<Array<{ label: string; position: number; value: T }>>
>;
minSpace?: number;
}
const TEST_HEIGHT = '+1234567890';
Expand Down Expand Up @@ -80,7 +82,13 @@ export function useTicks<
}
}

setTicks(ticks);
setTicks(
ticks.map((value) => ({
label: tickFormat(value),
position: scale(value),
value,
})),
);
}
}, [axisLength, direction, minSpace, ref, scale, setTicks, tickFormat]);
}
8 changes: 2 additions & 6 deletions src/hooks/useTimeTicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ export function useTimeTicks<Scale extends ScaleTime<number, number>>(
options: Options,
): TimeTicks[] {
const { tickFormat = scale.tickFormat() } = options;
const [ticks, setTicks] = useState<Date[]>([]);
const [ticks, setTicks] = useState<TimeTicks[]>([]);
useTicks<Date, ScaleTime<number, number>>(scale, direction, ref, {
...options,
setTicks,
tickFormat,
});
return ticks.map((value) => ({
label: tickFormat(value),
position: scale(value),
value,
}));
return ticks;
}
20 changes: 20 additions & 0 deletions stories/hooks/LinearPrimaryExamples.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Meta } from '@storybook/react';
import { useState } from 'react';

import { HorizontalExample, VerticalExample } from './TestAxis';

Expand Down Expand Up @@ -56,6 +57,25 @@ export function HorizontalScientificDecimals() {
}
HorizontalScientificDecimals.storyName = 'Horizontal scientific decimals';

export function HorizontalToggleDomain() {
const domain1 = [-1, 1] as const;
const domain2 = [0, 0] as const;
const [which, setWhich] = useState(1);

return (
<div>
<HorizontalExample
domain={which === 1 ? domain1 : domain2}
type="linear"
/>
<button type="button" onClick={() => setWhich((w) => (w === 1 ? 2 : 1))}>
Toggle domain
</button>
</div>
);
}
HorizontalToggleDomain.storyName = 'Horizontal toggle domain';

export function VerticalCentaines() {
return (
<VerticalExample
Expand Down
10 changes: 5 additions & 5 deletions stories/hooks/TestAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const HorizontalAxisTop = forwardRef<SVGGElement | null, HorizontalRenderProps>(
<g ref={ref} transform={`translate(${x}, ${y})`}>
<line x2={width} y1={15} y2={15} stroke="black" />
{ticks.map(({ label, position }) => (
<g key={`${label}${position}`}>
<g key={position}>
<line x1={position} x2={position} y1={10} y2={15} stroke="black" />
<text x={position} dominantBaseline="middle" textAnchor="middle">
{label}
Expand All @@ -69,7 +69,7 @@ const HorizontalAxisBottom = forwardRef<
<g ref={ref} transform={`translate(${x}, ${y})`}>
<line x2={width} y1={-15} y2={-15} stroke="black" />
{ticks.map(({ label, position }) => (
<g key={`${label}${position}`}>
<g key={position}>
<line x1={position} x2={position} y1={-10} y2={-15} stroke="black" />
<text x={position} dominantBaseline="middle" textAnchor="middle">
{label}
Expand Down Expand Up @@ -169,7 +169,7 @@ const VerticalAxisLeft = forwardRef<SVGGElement | null, VerticalRenderProps>(
<g ref={ref} transform={`translate(${x}, ${y})`}>
<line y2={height} x1={15} x2={15} stroke="black" />
{ticks.map(({ label, position }) => (
<g key={`${label}${position}`}>
<g key={position}>
<line y1={position} y2={position} x1={10} x2={15} stroke="black" />
<text y={position} dominantBaseline="middle" textAnchor="end">
{label}
Expand All @@ -185,7 +185,7 @@ const VerticalAxisRight = forwardRef<SVGGElement | null, VerticalRenderProps>(
<line y2={height} x1={-15} x2={-15} stroke="black" />
{ticks.map(({ label, position }) => {
return (
<g key={`${label}${position}`}>
<g key={position}>
<line
y1={position}
y2={position}
Expand Down Expand Up @@ -307,7 +307,7 @@ interface VerticalState {
height: number;
}
interface ExampleProps {
domain: [number | Date, number | Date];
domain: readonly [number | Date, number | Date];
type: 'linear' | 'log' | 'time';
scientificNotation?: boolean;
}
Expand Down

0 comments on commit 745edc1

Please sign in to comment.