Skip to content

Commit

Permalink
fix: add rangeStep normalizer to provide backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Jul 4, 2019
1 parent 036264c commit 113be5c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/log/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ export function orientOverridden(original: string, actual: string) {
export const CANNOT_UNION_CUSTOM_DOMAIN_WITH_FIELD_DOMAIN =
'custom domain scale cannot be unioned with default field-based domain';

export const RANGE_STEP_DEPRECATED = `Scale's "rangeStep" is deprecated and will be removed in Vega-Lite 5.0. Please use "width"/"height": {"step": ...} instead. See https://vega.github.io/vega-lite/docs/size.html`;

export function cannotUseScalePropertyWithNonColor(prop: string) {
return `Cannot use the scale property "${prop}" with non-color channel.`;
}
Expand Down
4 changes: 3 additions & 1 deletion src/normalize/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {isUnitSpec, NormalizedUnitSpec} from '../spec/unit';
import {keys, omit} from '../util';
import {NonFacetUnitNormalizer, NormalizerParams} from './base';
import {PathOverlayNormalizer} from './pathoverlay';
import {RangeStepNormalizer} from './rangestep';
import {RuleForRangedLineNormalizer} from './ruleforrangedline';

export class CoreNormalizer extends SpecMapper<NormalizerParams, FacetedUnitSpec, ExtendedLayerSpec> {
Expand All @@ -23,7 +24,8 @@ export class CoreNormalizer extends SpecMapper<NormalizerParams, FacetedUnitSpec
errorBarNormalizer,
errorBandNormalizer,
new PathOverlayNormalizer(),
new RuleForRangedLineNormalizer()
new RuleForRangedLineNormalizer(),
new RangeStepNormalizer()
];

public map(spec: GenericSpec<FacetedUnitSpec, ExtendedLayerSpec>, params: NormalizerParams) {
Expand Down
62 changes: 62 additions & 0 deletions src/normalize/rangestep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {getSizeType, POSITION_SCALE_CHANNELS} from '../channel';
import {isFieldDef} from '../channeldef';
import {Encoding} from '../encoding';
import * as log from '../log';
import {Scale} from '../scale';
import {GenericSpec} from '../spec/index';
import {GenericUnitSpec, isUnitSpec} from '../spec/unit';
import {NonFacetUnitNormalizer, NormalizeLayerOrUnit, NormalizerParams} from './base';

type UnitSpecWithRangeStep = GenericUnitSpec<Encoding<string>, any>; // this is not accurate, but it's not worth making it accurate

export class RangeStepNormalizer implements NonFacetUnitNormalizer<UnitSpecWithRangeStep> {
public name = 'RangeStep';

public hasMatchingType(spec: GenericSpec<any, any>): spec is UnitSpecWithRangeStep {
if (isUnitSpec(spec)) {
for (const channel of POSITION_SCALE_CHANNELS) {
const def = spec.encoding[channel];
if (def && isFieldDef(def)) {
if (def && def.scale && def.scale['rangeStep']) {
return true;
}
}
}
}
return false;
}

public run(spec: UnitSpecWithRangeStep, params: NormalizerParams, normalize: NormalizeLayerOrUnit) {
const sizeMixins = {};
let encoding = {...spec.encoding};

for (const channel of POSITION_SCALE_CHANNELS) {
const sizeType = getSizeType(channel);
const def = spec.encoding[channel];
if (def && isFieldDef(def)) {
if (def && def.scale && def.scale['rangeStep']) {
const {rangeStep, ...scaleWithoutRangeStep} = def.scale as Scale & {rangeStep: number};
sizeMixins[sizeType] = {step: def.scale['rangeStep']};

log.warn(log.message.RANGE_STEP_DEPRECATED);

encoding = {
...encoding,
[channel]: {
...encoding[channel],
scale: scaleWithoutRangeStep
}
};
}
}
}
return normalize(
{
...sizeMixins,
...spec,
encoding
},
params
);
}
}

0 comments on commit 113be5c

Please sign in to comment.