Skip to content

Commit

Permalink
Merge 5c2f2e9 into 8ede587
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Jul 24, 2020
2 parents 8ede587 + 5c2f2e9 commit 9378065
Show file tree
Hide file tree
Showing 74 changed files with 2,404 additions and 530 deletions.
6 changes: 4 additions & 2 deletions README.md
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
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
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
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
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
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
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
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
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
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
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
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
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
@@ -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<
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
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';

0 comments on commit 9378065

Please sign in to comment.