Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,356 additions
and
411 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
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
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
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ScaleConfig, ScaleConfigToD3Scale, PickScaleConfigWithoutType } from './types/ScaleConfig'; | ||
import { DefaultThresholdInput, PickD3Scale } from './types/Scale'; | ||
import { StringLike, Value } 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 extends Value, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput, | ||
Config extends ScaleConfig<Output, DiscreteInput, ThresholdInput> | ||
>(config: Config): ScaleConfigToD3Scale<Config, Output, DiscreteInput, ThresholdInput>; | ||
|
||
function createScale< | ||
Output extends Value, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
config: PickScaleConfigWithoutType<'linear', Output>, | ||
): PickD3Scale<'linear', Output, DiscreteInput, ThresholdInput>; | ||
|
||
// Actual implementation | ||
|
||
function createScale< | ||
Output extends Value, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
config: | ||
| ScaleConfig<Output, DiscreteInput, ThresholdInput> | ||
| PickScaleConfigWithoutType<'linear', Output>, | ||
) { | ||
if ('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: | ||
// @ts-ignore | ||
throw new Error(`Invalid scale type: ${config.type}`); | ||
} | ||
} | ||
|
||
// If type is not specified, fallback to linear scale | ||
return createLinearScale(config); | ||
} | ||
|
||
export default createScale; |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DefaultThresholdInput, D3Scale } from '../types/Scale'; | ||
import { StringLike } from '../types/Base'; | ||
import { ScaleConfigWithoutType } from '../types/ScaleConfig'; | ||
|
||
export default function applyAlign< | ||
Output, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
scale: D3Scale<Output, DiscreteInput, ThresholdInput>, | ||
config: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>, | ||
) { | ||
if ('align' in scale && 'align' in config && typeof config.align !== 'undefined') { | ||
scale.align(config.align); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DefaultThresholdInput, D3Scale } from '../types/Scale'; | ||
import { StringLike } from '../types/Base'; | ||
import { ScaleConfigWithoutType } from '../types/ScaleConfig'; | ||
|
||
export default function applyBase< | ||
Output, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
scale: D3Scale<Output, DiscreteInput, ThresholdInput>, | ||
config: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>, | ||
) { | ||
if ('base' in scale && 'base' in config && typeof config.base !== 'undefined') { | ||
scale.base(config.base); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DefaultThresholdInput, D3Scale } from '../types/Scale'; | ||
import { StringLike } from '../types/Base'; | ||
import { ScaleConfigWithoutType } from '../types/ScaleConfig'; | ||
|
||
export default function applyClamp< | ||
Output, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
scale: D3Scale<Output, DiscreteInput, ThresholdInput>, | ||
config: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>, | ||
) { | ||
if ('clamp' in scale && 'clamp' in config && typeof config.clamp !== 'undefined') { | ||
scale.clamp(config.clamp); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DefaultThresholdInput, D3Scale } from '../types/Scale'; | ||
import { StringLike } from '../types/Base'; | ||
import { ScaleConfigWithoutType } from '../types/ScaleConfig'; | ||
|
||
export default function applyConstant< | ||
Output, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
scale: D3Scale<Output, DiscreteInput, ThresholdInput>, | ||
config: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>, | ||
) { | ||
if ('constant' in scale && 'constant' in config && typeof config.constant !== 'undefined') { | ||
scale.constant(config.constant); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { DefaultThresholdInput, D3Scale } from '../types/Scale'; | ||
import { StringLike } from '../types/Base'; | ||
import { ScaleConfigWithoutType } from '../types/ScaleConfig'; | ||
|
||
export default function applyDomain< | ||
Output, | ||
DiscreteInput extends StringLike, | ||
ThresholdInput extends DefaultThresholdInput | ||
>( | ||
scale: D3Scale<Output, DiscreteInput, ThresholdInput>, | ||
config: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>, | ||
) { | ||
if (config.domain) { | ||
// @ts-ignore | ||
scale.domain(config.domain); | ||
} | ||
} |
Oops, something went wrong.