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: re-type vx/scale with new functionalities #766

Merged
merged 42 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
047140d
feat: update vx scale types
kristw Jul 17, 2020
26277e6
feat: add types and factory for all scales
kristw Jul 18, 2020
7201124
fix: correctly type inputs
kristw Jul 21, 2020
cc311e1
refactor: update input types and add overload
kristw Jul 21, 2020
a2e83f0
fix: fallback to linear scale
kristw Jul 21, 2020
707e87b
fix: docs
kristw Jul 21, 2020
81408f3
fix: overload
kristw Jul 21, 2020
a7ab837
refactor: add DefaultThresholdInput
kristw Jul 21, 2020
c52affc
fix: generic type
kristw Jul 22, 2020
48e2c4a
refactor: simplify overloading
kristw Jul 22, 2020
c5a718c
fix: omit type from config and remove scale type
kristw Jul 22, 2020
21c738a
refactor: update scale
kristw Jul 22, 2020
bbebebf
feat: use operator
kristw Jul 23, 2020
b57bc94
refactor: use operator for all
kristw Jul 23, 2020
783e56a
test: fix unit test
kristw Jul 23, 2020
28a3783
fix: broken tests
kristw Jul 23, 2020
56e0a47
fix: violin type
kristw Jul 23, 2020
f5fb093
fix: symlog round
kristw Jul 23, 2020
7f45b41
docs: update comment
kristw Jul 23, 2020
41f4229
fix: update generic
kristw Jul 23, 2020
8d772a5
feat: add inferScaleType and export types
kristw Jul 23, 2020
c948e0e
fix: domain and range
kristw Jul 23, 2020
6ef370d
test: add unit tests
kristw Jul 23, 2020
bad21bb
test: mock timezone
kristw Jul 23, 2020
a0a24c2
fix: rename export and remove extends value
kristw Jul 23, 2020
deb50d6
refactor: rename value to default output
kristw Jul 23, 2020
aabe8e3
docs: update comment
kristw Jul 23, 2020
32cee04
fix: better undefined check
kristw Jul 23, 2020
dc8786f
fix: type
kristw Jul 23, 2020
653693d
fix: enforce order
kristw Jul 23, 2020
90a5fee
test: add unit test for scales
kristw Jul 23, 2020
eff7091
test: add createScale test
kristw Jul 23, 2020
820c706
test: more tests
kristw Jul 23, 2020
2c7152e
test: more tests
kristw Jul 24, 2020
d9d21f2
fix: make config optional for factories
kristw Jul 24, 2020
e9eb41c
test: more unit tests
kristw Jul 24, 2020
9e24d65
test: 100% statement coverage
kristw Jul 24, 2020
70a9a66
refactor: simplify updateScale
kristw Jul 24, 2020
5c2f2e9
test: last unit test
kristw Jul 24, 2020
fe8d0b2
test: zero test
kristw Jul 24, 2020
a13314a
docs: update warning text
kristw Jul 24, 2020
27f6836
docs: add comment
kristw Jul 24, 2020
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ const y = d => +d.frequency * 100;

// And then scale the graph by our data
const xScale = scaleBand({
rangeRound: [0, xMax],
range: [0, xMax],
round: true,
domain: data.map(x),
padding: 0.4,
});
const yScale = scaleLinear({
rangeRound: [yMax, 0],
range: [yMax, 0],
round: true,
domain: [0, Math.max(...data.map(y))],
});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@
"enzyme-to-json": "^3.4.0",
"fs-jetpack": "^1.3.0",
"husky": "^3.0.0",
"jest-mock-console": "^1.0.1",
"lerna": "^3.15.0",
"marked": "^0.7.0",
"raf": "^3.4.0",
"react": "^15.0.0-0 || ^16.0.0-0",
"react-dom": "^15.0.0-0 || ^16.0.0-0",
"react-test-renderer": "^16.8.6",
"regenerator-runtime": "^0.10.5"
"regenerator-runtime": "^0.10.5",
"timezone-mock": "^1.1.0"
},
"workspaces": [
"./packages/*"
Expand Down
6 changes: 4 additions & 2 deletions packages/vx-axis/test/Axis.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { GenericScale } from '../src/types';
const axisProps = {
orientation: 'left' as const,
scale: scaleLinear({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
label: 'test axis',
Expand Down Expand Up @@ -199,7 +200,8 @@ describe('<Axis />', () => {
const overrideAxisProps = {
orientation: 'bottom' as const,
scale: scaleBand({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: ['a', 'b'],
}),
};
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-axis/test/AxisBottom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { GenericScale } from '../src/types';

const axisProps = {
scale: scaleLinear({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
};
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-axis/test/AxisLeft.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { GenericScale } from '../src/types';

const axisProps = {
scale: scaleLinear({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
};
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-axis/test/AxisRight.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { GenericScale } from '../src/types';

const axisProps = {
scale: scaleLinear({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
};
Expand Down
3 changes: 2 additions & 1 deletion packages/vx-axis/test/AxisTop.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { GenericScale } from '../src/types';

const axisProps = {
scale: scaleLinear({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
};
Expand Down
12 changes: 8 additions & 4 deletions packages/vx-axis/test/scales.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('Axis scales', () => {
expect(
setup(
scaleBand({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: ['a', 'b', 'c'],
}) as GenericScale<string>,
),
Expand All @@ -42,7 +43,8 @@ describe('Axis scales', () => {
expect(
setup(
scaleLinear<number>({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}) as GenericScale<number>,
),
Expand All @@ -53,7 +55,8 @@ describe('Axis scales', () => {
expect(
setup(
scaleLog<number>({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [1, 10, 100, 1000],
}) as GenericScale<number>,
),
Expand All @@ -75,7 +78,8 @@ describe('Axis scales', () => {
expect(
setup(
scalePoint<string>({
rangeRound: [0, 10],
range: [0, 10],
round: true,
domain: ['a', 'b', 'c'],
}) as GenericScale<string>,
),
Expand Down
6 changes: 4 additions & 2 deletions packages/vx-demo/src/sandboxes/vx-bars/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default function Example({ width, height, events = false }: BarsProps) {
const xScale = useMemo(
() =>
scaleBand<string>({
rangeRound: [0, xMax],
range: [0, xMax],
round: true,
domain: data.map(getLetter),
padding: 0.4,
}),
Expand All @@ -36,7 +37,8 @@ export default function Example({ width, height, events = false }: BarsProps) {
const yScale = useMemo(
() =>
scaleLinear<number>({
rangeRound: [yMax, 0],
range: [yMax, 0],
round: true,
domain: [0, Math.max(...data.map(getLetterFrequency))],
}),
[yMax],
Expand Down
6 changes: 4 additions & 2 deletions packages/vx-demo/src/sandboxes/vx-stats/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default withTooltip<StatsPlotProps, TooltipData>(

// scales
const xScale = scaleBand<string>({
rangeRound: [0, xMax],
range: [0, xMax],
round: true,
domain: data.map(x),
padding: 0.4,
});
Expand All @@ -63,7 +64,8 @@ export default withTooltip<StatsPlotProps, TooltipData>(
const maxYValue = Math.max(...values);

const yScale = scaleLinear<number>({
rangeRound: [yMax, 0],
range: [yMax, 0],
round: true,
domain: [minYValue, maxYValue],
});

Expand Down
3 changes: 2 additions & 1 deletion packages/vx-legend/test/Legend.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { Legend, LegendLabel } from '../src';

const defaultProps = {
scale: scaleLinear<number>({
rangeRound: [10, 0],
range: [10, 0],
round: true,
domain: [0, 10],
}),
};
Expand Down
6 changes: 4 additions & 2 deletions packages/vx-scale/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ const [minY, maxY] = getYMinAndMax();

const xScale = Scale.scaleLinear({
domain: [minX, maxX], // x-coordinate data values
rangeRound: [0, graphWidth], // svg x-coordinates, svg x-coordinates increase left to right
range: [0, graphWidth], // svg x-coordinates, svg x-coordinates increase left to right
round: true,
});

const yScale = Scale.scaleLinear({
domain: [minY, maxY], // y-coordinate data values
// svg y-coordinates, these increase from top to bottom so we reverse the order
// so that minY in data space maps to graphHeight in svg y-coordinate space
rangeRound: [graphHeight, 0],
range: [graphHeight, 0],
round: true,
});

// ...
Expand Down
8 changes: 6 additions & 2 deletions packages/vx-scale/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@
"visualizations",
"charts"
],
"author": "@hshoff",
"authors": ["@hshoff", "@kristw"],
"license": "MIT",
"bugs": {
"url": "https://github.com/hshoff/vx/issues"
},
"homepage": "https://github.com/hshoff/vx#readme",
"dependencies": {
"@types/d3-interpolate": "^1.3.1",
"@types/d3-scale": "^2.1.1",
"d3-scale": "^2.2.2"
"@types/d3-time": "^1.0.10",
"d3-interpolate": "^1.4.0",
"d3-scale": "^3.0.1",
"d3-time": "^1.1.0"
},
"publishConfig": {
"access": "public"
Expand Down
157 changes: 157 additions & 0 deletions packages/vx-scale/src/createScale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import { ScaleConfig, PickScaleConfigWithoutType, PickScaleConfig } from './types/ScaleConfig';
import { DefaultThresholdInput, PickD3Scale } from './types/Scale';
import { StringLike, DefaultOutput } from './types/Base';
import createLinearScale from './scales/linear';
import createLogScale from './scales/log';
import createPowScale from './scales/power';
import createSqrtScale from './scales/squareRoot';
import createSymlogScale from './scales/symlog';
import createTimeScale from './scales/time';
import createUtcScale from './scales/utc';
import createQuantileScale from './scales/quantile';
import createQuantizeScale from './scales/quantize';
import createThresholdScale from './scales/threshold';
import createOrdinalScale from './scales/ordinal';
import createPointScale from './scales/point';
import createBandScale from './scales/band';

// Overload function for more strict typing, e.g.,
// If the config is a linear config then a ScaleLinear will be returned
// instead of a union type of all scales.

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(
config?: PickScaleConfig<'linear', Output> | PickScaleConfigWithoutType<'linear', Output>,
): PickD3Scale<'linear', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'log', Output>): PickD3Scale<'log', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'pow', Output>): PickD3Scale<'pow', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'sqrt', Output>): PickD3Scale<'sqrt', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'symlog', Output>): PickD3Scale<'symlog', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'time', Output>): PickD3Scale<'time', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'utc', Output>): PickD3Scale<'utc', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'quantile', Output>): PickD3Scale<'quantile', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(config: PickScaleConfig<'quantize', Output>): PickD3Scale<'quantize', Output>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(
config: PickScaleConfig<'threshold', Output, StringLike, ThresholdInput>,
): PickD3Scale<'threshold', Output, StringLike, ThresholdInput>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(
config: PickScaleConfig<'ordinal', Output, DiscreteInput>,
): PickD3Scale<'ordinal', Output, DiscreteInput>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(
config: PickScaleConfig<'point', Output, DiscreteInput>,
): PickD3Scale<'point', Output, DiscreteInput>;

function createScale<
Output = DefaultOutput,
DiscreteInput extends StringLike = StringLike,
ThresholdInput extends DefaultThresholdInput = DefaultThresholdInput
>(
config: PickScaleConfig<'band', Output, DiscreteInput>,
): PickD3Scale<'band', Output, DiscreteInput>;

// Actual implementation

function createScale<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be amazing for XYChart 🤩

Output,
DiscreteInput extends StringLike,
ThresholdInput extends DefaultThresholdInput
>(
config?:
| ScaleConfig<Output, DiscreteInput, ThresholdInput>
| PickScaleConfigWithoutType<'linear', Output>,
) {
if (typeof config !== 'undefined' && 'type' in config) {
switch (config.type) {
case 'linear':
return createLinearScale(config);
case 'log':
return createLogScale(config);
case 'pow':
return createPowScale(config);
case 'sqrt':
return createSqrtScale(config);
case 'symlog':
return createSymlogScale(config);
case 'time':
return createTimeScale(config);
case 'utc':
return createUtcScale(config);
case 'quantile':
return createQuantileScale(config);
case 'quantize':
return createQuantizeScale(config);
case 'threshold':
return createThresholdScale(config);
case 'ordinal':
return createOrdinalScale(config);
case 'point':
return createPointScale(config);
case 'band':
return createBandScale(config);
default:
}
}

// If type is not specified, fallback to linear scale
return createLinearScale(config);
}

export default createScale;
12 changes: 11 additions & 1 deletion packages/vx-scale/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,15 @@ export { default as scaleQuantize } from './scales/quantize';
export { default as scaleQuantile } from './scales/quantile';
export { default as scaleSymlog } from './scales/symlog';
export { default as scaleThreshold } from './scales/threshold';
export { default as updateScale } from './util/updateScale';
export { default as scaleSqrt } from './scales/squareRoot';

export { default as createScale } from './createScale';
export { default as updateScale } from './updateScale';
export { default as inferScaleType } from './utils/inferScaleType';

// export types
export * from './types/Base';
export * from './types/Nice';
export * from './types/Scale';
export * from './types/ScaleConfig';
export * from './types/ScaleInterpolate';
Loading