Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add onlyMainTicks in useLogScale #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: update docs
  • Loading branch information
wadjih-bencheikh18 committed Feb 10, 2022
commit baa77ccdcaa3c107fd01d40eee4c4ba2f1ea5f8b
1 change: 0 additions & 1 deletion stories/hooks/LinearPrimaryExamples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { HorizontalExample, VerticalExample } from './TestAxis';

export default {
title: 'Hooks/static/useLinearPrimaryTicks',
component: HorizontalExample || VerticalExample,
} as Meta;
interface Props {
domain: [number, number];
Expand Down
1 change: 0 additions & 1 deletion stories/hooks/LinearPrimaryTicks.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ interface Props {

export default {
title: 'Hooks/useLinearPrimaryTicks',
component: AutomaticVerticalAxis || AutomaticHorizontalAxis,
args: {
minSize: 50,
maxSize: 500,
Expand Down
2 changes: 1 addition & 1 deletion stories/hooks/LogExamples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { HorizontalExample, VerticalExample } from './TestAxis';

export default {
title: 'Hooks/static/useLogTicks',
component: HorizontalExample || VerticalExample,
} as Meta;

interface Props {
Expand All @@ -17,6 +16,7 @@ export function HorizontalCentaines(props: Props) {
HorizontalCentaines.args = {
domain: [10, 100000],
scientificNotation: false,
onlyMain: false,
};
HorizontalCentaines.storyName = 'Horizontal positive powers';

Expand Down
2 changes: 0 additions & 2 deletions stories/hooks/LogTicks.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface Props {

export default {
title: 'Hooks/useLogTicks',

component: AutomaticVerticalAxis || AutomaticHorizontalAxis,
args: {
minSize: 50,
maxSize: 500,
Expand Down
36 changes: 31 additions & 5 deletions stories/hooks/TestAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ScaleLogarithmic,
scaleTime,
} from 'd3-scale';
import React, { forwardRef, useMemo, useRef, useEffect, useState } from 'react';
import { forwardRef, useMemo, useRef, useEffect, useState } from 'react';

import { useLinearPrimaryTicks, useLogTicks, useTimeTicks } from '../../src';

Expand All @@ -17,6 +17,7 @@ interface BaseAxis {
interface ScaleAxis<T> {
scale: T;
scientificNotation?: boolean;
onlyMain?: boolean;
}
interface TickAxis {
ticks: Ticks[];
Expand Down Expand Up @@ -148,13 +149,22 @@ function LogHorizontalAxis(
props: HorizontalAxisProps<ScaleLogarithmic<number, number>> &
HorizontalOrientation,
) {
const { scale, scientificNotation, orientation = 'top', ...other } = props;
const {
scale,
scientificNotation,
orientation = 'top',
onlyMain = false,
...other
} = props;
const ref = useRef<SVGGElement>(null);
const tickFormat = useMemo(
() => (scientificNotation ? toExponential : undefined),
[scientificNotation],
);
const ticks = useLogTicks(scale, 'horizontal', ref, { tickFormat });
const ticks = useLogTicks(scale, 'horizontal', ref, {
tickFormat,
onlyMain,
});
if (orientation === 'top') {
return <HorizontalAxisTop {...other} ticks={ticks} ref={ref} />;
}
Expand Down Expand Up @@ -275,13 +285,19 @@ function LogVerticalAxis(
props: VerticalAxisProps<ScaleLogarithmic<number, number>> &
VerticalOrientation,
) {
const { scale, scientificNotation, orientation = 'left', ...other } = props;
const {
scale,
scientificNotation,
orientation = 'left',
onlyMain = false,
...other
} = props;
const ref = useRef<SVGGElement>(null);
const tickFormat = useMemo(
() => (scientificNotation ? toExponential : undefined),
[scientificNotation],
);
const ticks = useLogTicks(scale, 'vertical', ref, { tickFormat });
const ticks = useLogTicks(scale, 'vertical', ref, { tickFormat, onlyMain });
if (orientation === 'left') {
return <VerticalAxisLeft {...other} ticks={ticks} ref={ref} />;
}
Expand Down Expand Up @@ -310,6 +326,7 @@ interface ExampleProps {
domain: [number | Date, number | Date];
type: 'linear' | 'log' | 'time';
scientificNotation?: boolean;
onlyMain?: boolean;
}
interface VerticalOrientation {
orientation?: 'left' | 'right';
Expand All @@ -321,6 +338,7 @@ interface HorizontalOrientation {
export function HorizontalExample({
domain,
scientificNotation = false,
onlyMain = false,
orientation,
type,
}: ExampleProps & HorizontalOrientation) {
Expand Down Expand Up @@ -357,6 +375,7 @@ export function HorizontalExample({
y={width - 40}
scale={scale}
scientificNotation={scientificNotation}
onlyMain={onlyMain}
width={width}
type={type}
/>
Expand All @@ -370,6 +389,7 @@ export function VerticalExample({
scientificNotation,
orientation,
type,
onlyMain,
}: ExampleProps & VerticalOrientation) {
const [state, setState] = useState<VerticalState[]>([]);
const scaleType = useMemo(() => {
Expand Down Expand Up @@ -404,6 +424,7 @@ export function VerticalExample({
y={10}
scale={scale}
scientificNotation={scientificNotation}
onlyMain={onlyMain}
height={height}
type={type}
/>
Expand All @@ -421,6 +442,7 @@ interface Props {
speedAnimation: number;
type: 'linear' | 'log' | 'time';
scientificNotation?: boolean;
onlyMain?: boolean;
}
export function AutomaticHorizontalAxis({
minSize,
Expand All @@ -429,6 +451,7 @@ export function AutomaticHorizontalAxis({
maxValue,
speedAnimation,
scientificNotation = false,
onlyMain = false,
type,
}: Props) {
const [width, setWidth] = useState(minSize);
Expand Down Expand Up @@ -469,6 +492,7 @@ export function AutomaticHorizontalAxis({
scale={scale}
width={width}
scientificNotation={scientificNotation}
onlyMain={onlyMain}
type={type}
/>
</svg>
Expand All @@ -483,6 +507,7 @@ export function AutomaticVerticalAxis({
maxValue,
speedAnimation,
scientificNotation,
onlyMain = false,
type,
}: Props) {
const [height, setHeight] = useState(minSize);
Expand Down Expand Up @@ -525,6 +550,7 @@ export function AutomaticVerticalAxis({
scale={scale}
height={height}
scientificNotation={scientificNotation}
onlyMain={onlyMain}
type={type}
/>
</svg>
Expand Down
1 change: 0 additions & 1 deletion stories/hooks/TimeExamples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { HorizontalExample, VerticalExample } from './TestAxis';

export default {
title: 'Hooks/static/useTimeTicks',
component: HorizontalExample || VerticalExample,
} as Meta;

interface Props {
Expand Down
1 change: 0 additions & 1 deletion stories/hooks/TimeTicks.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface Props {

export default {
title: 'Hooks/useTimeTicks',
component: AutomaticVerticalAxis || AutomaticHorizontalAxis,
args: {
minSize: 50,
maxSize: 500,
Expand Down