-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #777 from kristw/kristw--legend
feat(legend): update scale types for vx/legend
- Loading branch information
Showing
13 changed files
with
132 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,22 @@ | ||
import React from 'react'; | ||
import { PickD3Scale } from '@vx/scale'; | ||
import Legend, { LegendProps } from './Legend'; | ||
import { ScaleLinear } from '../types'; | ||
import defaultDomain from '../util/defaultDomain'; | ||
|
||
export type LegendLinearProps<Output> = { | ||
steps?: number; | ||
} & LegendProps<number, Output, ScaleLinear<number, Output>>; | ||
|
||
export function defaultDomain<Output>({ | ||
steps = 5, | ||
scale, | ||
}: Pick<LegendLinearProps<Output>, 'steps' | 'scale'>) { | ||
const domain = scale.domain(); | ||
const start = domain[0]; | ||
const end = domain[domain.length - 1]; | ||
const step = (end - start) / (steps - 1); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type AnyLinearScale = PickD3Scale<'linear', any>; | ||
|
||
return new Array(steps).fill(1).reduce((acc, cur, i) => { | ||
acc.push(start + i * step); | ||
return acc; | ||
}, []); | ||
} | ||
export type LegendLinearProps<Scale extends AnyLinearScale> = { | ||
steps?: number; | ||
} & LegendProps<Scale>; | ||
|
||
/** Linear scales map from continuous inputs to continuous outputs. */ | ||
export default function Linear<Output>({ | ||
export default function Linear<Scale extends AnyLinearScale>({ | ||
scale, | ||
domain: inputDomain, | ||
steps = 5, | ||
...restProps | ||
}: LegendLinearProps<Output>) { | ||
}: LegendLinearProps<Scale>) { | ||
const domain = inputDomain || defaultDomain({ steps, scale }); | ||
return ( | ||
<Legend<number, Output, ScaleLinear<number, Output>> | ||
scale={scale} | ||
domain={domain} | ||
{...restProps} | ||
/> | ||
); | ||
return <Legend<Scale> scale={scale} domain={domain} {...restProps} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
import React from 'react'; | ||
import { PickD3Scale } from '@vx/scale'; | ||
import Legend, { LegendProps } from './Legend'; | ||
import { ScaleOrdinal } from '../types'; | ||
|
||
export type LegendOrdinalProps<Input extends { toString(): string }, Output> = LegendProps< | ||
string, | ||
Output, | ||
ScaleOrdinal<Input, Output> | ||
>; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type AnyOrdinalScale = PickD3Scale<'ordinal', any, any>; | ||
|
||
export type LegendOrdinalProps<Scale extends AnyOrdinalScale> = LegendProps<Scale>; | ||
|
||
/** Ordinal scales map from strings to an Output type. */ | ||
export default function Ordinal<Input extends { toString(): string }, Output>( | ||
props: LegendOrdinalProps<Input, Output>, | ||
) { | ||
return <Legend<string, Output, ScaleOrdinal<Input, Output>> {...props} />; | ||
export default function Ordinal<Scale extends AnyOrdinalScale>(props: LegendOrdinalProps<Scale>) { | ||
return <Legend<Scale> {...props} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.