Skip to content

Commit

Permalink
Introduce ExtendedUnitSpec = NormalizedUnitSpec | CompositeUnitSpec
Browse files Browse the repository at this point in the history
… and separate NormalizedUnitSpec from CompositeUnitSpec
  • Loading branch information
kanitw committed Jan 27, 2019
1 parent 4d3e15d commit 36da7aa
Show file tree
Hide file tree
Showing 19 changed files with 3,902 additions and 1,559 deletions.
5,124 changes: 3,746 additions & 1,378 deletions build/vega-lite-schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"deploy:gh": "scripts/deploy-gh.sh",
"deploy:schema": "scripts/deploy-schema.sh",
"preschema": "npm run prebuild",
"schema": "node --stack-size=2800 ./node_modules/.bin/ts-json-schema-generator --no-type-check --path tsconfig.json --type TopLevelSpec > build/vega-lite-schema.json && npm run renameschema && cp build/vega-lite-schema.json _data/",
"schema": "node --stack-size=4000 ./node_modules/.bin/ts-json-schema-generator --no-type-check --path tsconfig.json --type TopLevelSpec > build/vega-lite-schema.json && npm run renameschema && cp build/vega-lite-schema.json _data/",
"renameschema": "scripts/rename-schema.sh",
"presite": "npm run prebuild && npm run data && npm run build:site && npm run build:toc && npm run build:versions && scripts/create-example-pages",
"site": "bundle exec jekyll serve --incremental",
Expand Down
4 changes: 2 additions & 2 deletions scripts/check-schema.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash

if grep 'Generic.*Spec<' ./build/vega-lite-schema.json
if grep 'Generic[^U].*Spec<' ./build/vega-lite-schema.json
then
echo "Generic*Spec in the schema have not been replaced."
echo "Non-Unit Generic Spec in the schema have not been replaced."
exit 1
elif grep 'UnitSpec<Encoding' ./build/vega-lite-schema.json
then
Expand Down
10 changes: 7 additions & 3 deletions scripts/rename-schema.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/usr/bin/env bash

perl -pi -e s,'\<EmptyObject\>','',g build/vega-lite-schema.json
perl -pi -e s,'\&EmptyObject','',g build/vega-lite-schema.json
perl -pi -e s,'<Field>','',g build/vega-lite-schema.json
perl -pi -e s,'<Field\,','<',g build/vega-lite-schema.json
perl -pi -e s,'<StandardType>','',g build/vega-lite-schema.json

perl -pi -e s,'\&FacetMapping','WithFacet',g build/vega-lite-schema.json
perl -pi -e s,'<FacetMapping>','WithFacet',g build/vega-lite-schema.json

perl -pi -e s,'FacetedCompositeUnitSpec','FacetedUnitSpec',g build/vega-lite-schema.json
perl -pi -e s,'ExtendedLayerSpec','LayerSpec',g build/vega-lite-schema.json
perl -pi -e s,'GenericLayerSpec<CompositeUnitSpec>','LayerSpec',g build/vega-lite-schema.json
perl -pi -e s,'Generic(.*)<FacetedUnitSpec\,LayerSpec>','\1',g build/vega-lite-schema.json
perl -pi -e s,'GenericLayerSpec<ExtendedUnitSpec>','LayerSpec',g build/vega-lite-schema.json
perl -pi -e s,'Generic(.*)<FacetedExtendedUnitSpec\,LayerSpec>','\1',g build/vega-lite-schema.json

perl -pi -e s,'GenericUnitSpec<EncodingWithFacet\,AnyMark>','FacetedCompositeUnitSpecAlias',g build/vega-lite-schema.json
perl -pi -e s,'GenericUnitSpec<Encoding\,AnyMark>','CompositeUnitSpecAlias',g build/vega-lite-schema.json
Expand Down
25 changes: 0 additions & 25 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export const X = Channel.X;
export const Y = Channel.Y;
export const X2 = Channel.X2;
export const Y2 = Channel.Y2;
export const XERROR = Channel.XERROR;
export const YERROR = Channel.YERROR;
export const XERROR2 = Channel.XERROR2;
export const YERROR2 = Channel.YERROR2;

export const LATITUDE = Channel.LATITUDE;
export const LATITUDE2 = Channel.LATITUDE2;
Expand Down Expand Up @@ -111,10 +107,6 @@ const UNIT_CHANNEL_INDEX: Flag<keyof Encoding<any>> = {
y: 1,
x2: 1,
y2: 1,
xError: 1,
yError: 1,
xError2: 1,
yError2: 1,

...GEOPOSITION_CHANNEL_INDEX,

Expand Down Expand Up @@ -180,10 +172,6 @@ export type SingleDefUnitChannel =
| 'y'
| 'x2'
| 'y2'
| 'xError'
| 'yError'
| 'xError2'
| 'yError2'
| 'longitude'
| 'latitude'
| 'longitude2'
Expand Down Expand Up @@ -237,10 +225,6 @@ const {
// x2 and y2 share the same scale as x and y
x2: _x2,
y2: _y2,
xError: _xError,
yError: _yError,
xError2: _xError2,
yError2: _yError2,
latitude: _latitude,
longitude: _longitude,
latitude2: _latitude2,
Expand Down Expand Up @@ -409,11 +393,6 @@ function getSupportedMark(channel: Channel): SupportedMark {
return {point: 'always', geoshape: 'always'};
case TEXT:
return {text: 'always'};
case XERROR:
case YERROR:
case XERROR2:
case YERROR2:
return {};
}
}

Expand All @@ -429,10 +408,6 @@ export function rangeType(channel: Channel): RangeType {
// X2 and Y2 use X and Y scales, so they similarly have continuous range.
case X2:
case Y2:
case XERROR:
case YERROR:
case XERROR2:
case YERROR2:
return undefined;

case ROW:
Expand Down
4 changes: 0 additions & 4 deletions src/compile/mark/mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,6 @@ export function pathGroupingFields(mark: Mark, encoding: Encoding<string>): stri
case 'href':
case 'x2':
case 'y2':
case 'xError':
case 'yError':
case 'xError2':
case 'yError2':

case 'latitude':
case 'longitude':
Expand Down
14 changes: 7 additions & 7 deletions src/compositemark/boxplot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {isNumber, isObject} from 'vega-util';
import {Channel} from '../channel';
import {Config} from '../config';
import {Encoding, extractTransformsFromEncoding} from '../encoding';
import {PositionFieldDef} from '../fielddef';
import {Field, PositionFieldDef} from '../fielddef';
import * as log from '../log';
import {isMarkDef, MarkDef} from '../mark';
import {GenericUnitSpec, NormalizedLayerSpec, NormalizedUnitSpec} from '../spec';
Expand All @@ -13,14 +12,17 @@ import {
compositeMarkContinuousAxis,
compositeMarkOrient,
CompositeMarkTooltipSummary,
filterUnsupportedChannels,
GenericCompositeMarkDef,
getCompositeMarkTooltip,
makeCompositeAggregatePartFactory,
partLayerMixins,
PartsMixins
} from './common';

export type BoxPlotUnitSpec<
EE = {} // extra encoding parameter (for faceted composite unit spec)
> = GenericUnitSpec<BoxPlotEncoding<Field> & EE, BoxPlot | BoxPlotDef>;

export const BOXPLOT: 'boxplot' = 'boxplot';
export type BoxPlot = typeof BOXPLOT;

Expand Down Expand Up @@ -68,21 +70,19 @@ export type BoxPlotDef = GenericCompositeMarkDef<BoxPlot> &
orient?: Orient;
};

export type BoxPlotEncoding<F extends Field> = Pick<Encoding<F>, 'x' | 'y' | 'color' | 'detail' | 'opacity' | 'size'>;

export interface BoxPlotConfigMixins {
/**
* Box Config
*/
boxplot?: BoxPlotConfig;
}

const boxPlotSupportedChannels: Channel[] = ['x', 'y', 'color', 'detail', 'opacity', 'size'];

export function normalizeBoxPlot(
spec: GenericUnitSpec<Encoding<string>, BoxPlot | BoxPlotDef>,
config: Config
): NormalizedLayerSpec {
spec = filterUnsupportedChannels(spec, boxPlotSupportedChannels, BOXPLOT);

// TODO: use selection
const {mark, encoding: _encoding, selection, projection: _p, ...outerSpec} = spec;
const markDef: BoxPlotDef = isMarkDef(mark) ? mark : {type: mark};
Expand Down
25 changes: 1 addition & 24 deletions src/compositemark/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {isBoolean, isString} from 'vega-util';
import {CompositeMark, CompositeMarkDef} from '.';
import {Channel} from '../channel';
import {Encoding, fieldDefs, reduce} from '../encoding';
import {Encoding, fieldDefs} from '../encoding';
import {
Field,
FieldDefBase,
Expand Down Expand Up @@ -223,25 +222,3 @@ export function compositeMarkOrient<M extends CompositeMark>(
throw new Error('Need a valid continuous axis for ' + compositeMark + 's');
}
}

export function filterUnsupportedChannels<M extends CompositeMark, MD extends GenericCompositeMarkDef<M>>(
spec: GenericUnitSpec<Encoding<string>, M | MD>,
supportedChannels: Channel[],
compositeMark: M
): GenericUnitSpec<Encoding<string>, M | MD> {
return {
...spec,
encoding: reduce(
spec.encoding,
(newEncoding, fieldDef, channel) => {
if (supportedChannels.indexOf(channel) > -1) {
newEncoding[channel] = fieldDef;
} else {
log.warn(log.message.incompatibleChannel(channel, compositeMark));
}
return newEncoding;
},
{}
)
};
}
7 changes: 6 additions & 1 deletion src/compositemark/errorband.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {Config} from '../config';
import {Encoding} from '../encoding';
import {Field} from '../fielddef';
import * as log from '../log';
import {MarkDef} from '../mark';
import {GenericUnitSpec, NormalizedLayerSpec} from '../spec';
import {Flag, keys} from '../util';
import {Interpolate, Orient} from '../vega.schema';
import {GenericCompositeMarkDef, makeCompositeAggregatePartFactory, PartsMixins} from './common';
import {ErrorBarCenter, ErrorBarExtent, errorBarParams} from './errorbar';
import {ErrorBarCenter, ErrorBarExtent, errorBarParams, ErrorEncoding} from './errorbar';

export type ErrorBandUnitSpec<
EE = {} // extra encoding parameter (for faceted composite unit spec)
> = GenericUnitSpec<ErrorEncoding<Field> & EE, ErrorBand | ErrorBandDef>;

export const ERRORBAND: 'errorband' = 'errorband';
export type ErrorBand = typeof ERRORBAND;
Expand Down
68 changes: 40 additions & 28 deletions src/compositemark/errorbar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {AggregateOp} from 'vega';
import {Channel} from '../channel';
import {Config} from '../config';
import {Data} from '../data';
import {Encoding, extractTransformsFromEncoding} from '../encoding';
Expand All @@ -10,7 +9,8 @@ import {
isFieldDef,
PositionFieldDef,
SecondaryFieldDef,
title
title,
ValueDef
} from '../fielddef';
import * as log from '../log';
import {isMarkDef, MarkDef} from '../mark';
Expand All @@ -23,14 +23,17 @@ import {
compositeMarkContinuousAxis,
compositeMarkOrient,
CompositeMarkTooltipSummary,
filterUnsupportedChannels,
GenericCompositeMarkDef,
getCompositeMarkTooltip,
makeCompositeAggregatePartFactory,
PartsMixins
} from './common';
import {ErrorBand, ErrorBandDef} from './errorband';

export type ErrorBarUnitSpec<
EE = {} // extra encoding parameter (for faceted composite unit spec)
> = GenericUnitSpec<ErrorEncoding<Field> & EE, ErrorBar | ErrorBarDef>;

export const ERRORBAR: 'errorbar' = 'errorbar';
export type ErrorBar = typeof ERRORBAR;

Expand All @@ -46,6 +49,31 @@ const ERRORBAR_PART_INDEX: Flag<ErrorBarPart> = {
rule: 1
};

export interface ErrorEncoding<F extends Field>
extends Pick<Encoding<F>, 'x' | 'y' | 'x2' | 'y2' | 'color' | 'detail' | 'opacity'> {
/**
* Error value of x coordinates for error specified `"errorbar"` and `"errorband"`.
*/
xError?: SecondaryFieldDef<F> | ValueDef<number>;

/**
* Secondary error value of x coordinates for error specified `"errorbar"` and `"errorband"`.
*/
// `xError2` cannot have type as it should have the same type as `xError`
xError2?: SecondaryFieldDef<F> | ValueDef<number>;

/**
* Error value of y coordinates for error specified `"errorbar"` and `"errorband"`.
*/
yError?: SecondaryFieldDef<F> | ValueDef<number>;

/**
* Secondary error value of y coordinates for error specified `"errorbar"` and `"errorband"`.
*/
// `yError2` cannot have type as it should have the same type as `yError`
yError2?: SecondaryFieldDef<F> | ValueDef<number>;
}

export const ERRORBAR_PARTS = keys(ERRORBAR_PART_INDEX);

export type ErrorBarPartsMixins = PartsMixins<ErrorBarPart>;
Expand Down Expand Up @@ -91,7 +119,7 @@ export interface ErrorBarConfigMixins {
}

export function normalizeErrorBar(
spec: GenericUnitSpec<Encoding<string>, ErrorBar | ErrorBarDef>,
spec: GenericUnitSpec<ErrorEncoding<string>, ErrorBar | ErrorBarDef>,
config: Config
): NormalizedLayerSpec {
const {
Expand Down Expand Up @@ -143,7 +171,7 @@ export function normalizeErrorBar(
}

function errorBarOrientAndInputType(
spec: GenericUnitSpec<Encoding<Field>, ErrorBar | ErrorBand | ErrorBarDef | ErrorBandDef>,
spec: GenericUnitSpec<ErrorEncoding<Field>, ErrorBar | ErrorBand | ErrorBarDef | ErrorBandDef>,
compositeMark: ErrorBar | ErrorBand
): {
orient: Orient;
Expand Down Expand Up @@ -237,7 +265,7 @@ function errorBarOrientAndInputType(
}
}

function errorBarIsInputTypeRaw(encoding: Encoding<Field>): boolean {
function errorBarIsInputTypeRaw(encoding: ErrorEncoding<Field>): boolean {
return (
(isFieldDef(encoding.x) || isFieldDef(encoding.y)) &&
!isFieldDef(encoding.x2) &&
Expand All @@ -249,11 +277,11 @@ function errorBarIsInputTypeRaw(encoding: Encoding<Field>): boolean {
);
}

function errorBarIsInputTypeAggregatedUpperLower(encoding: Encoding<Field>): boolean {
function errorBarIsInputTypeAggregatedUpperLower(encoding: ErrorEncoding<Field>): boolean {
return isFieldDef(encoding.x2) || isFieldDef(encoding.y2);
}

function errorBarIsInputTypeAggregatedError(encoding: Encoding<Field>): boolean {
function errorBarIsInputTypeAggregatedError(encoding: ErrorEncoding<Field>): boolean {
return (
isFieldDef(encoding.xError) ||
isFieldDef(encoding.xError2) ||
Expand All @@ -262,33 +290,19 @@ function errorBarIsInputTypeAggregatedError(encoding: Encoding<Field>): boolean
);
}

export const errorBarSupportedChannels: Channel[] = [
'x',
'y',
'x2',
'y2',
'xError',
'yError',
'xError2',
'yError2',
'color',
'detail',
'opacity'
];

export function errorBarParams<
M extends ErrorBar | ErrorBand,
MD extends GenericCompositeMarkDef<M> & (ErrorBarDef | ErrorBandDef)
>(
spec: GenericUnitSpec<Encoding<string>, M | MD>,
spec: GenericUnitSpec<ErrorEncoding<string>, M | MD>,
compositeMark: M,
config: Config
): {
transform: Transform[];
groupby: string[];
continuousAxisChannelDef: PositionFieldDef<string>;
continuousAxis: 'x' | 'y';
encodingWithoutContinuousAxis: Encoding<string>;
encodingWithoutContinuousAxis: ErrorEncoding<string>;
ticksOrient: Orient;
markDef: MD;
outerSpec: {
Expand All @@ -300,10 +314,8 @@ export function errorBarParams<
width?: number;
height?: number;
};
tooltipEncoding: Encoding<string>;
tooltipEncoding: ErrorEncoding<string>;
} {
spec = filterUnsupportedChannels<M, MD>(spec, errorBarSupportedChannels, compositeMark);

// TODO: use selection
const {mark, encoding, selection, projection: _p, ...outerSpec} = spec;
const markDef: MD = isMarkDef(mark) ? mark : ({type: mark} as MD);
Expand Down Expand Up @@ -357,7 +369,7 @@ export function errorBarParams<
const aggregate: AggregatedFieldDef[] = [...oldAggregate, ...errorBarSpecificAggregate];
const groupby: string[] = inputType !== 'raw' ? [] : oldGroupBy;

const tooltipEncoding: Encoding<string> = getCompositeMarkTooltip(
const tooltipEncoding: ErrorEncoding<string> = getCompositeMarkTooltip(
tooltipSummary,
continuousAxisChannelDef,
encodingWithoutContinuousAxis,
Expand Down

0 comments on commit 36da7aa

Please sign in to comment.