Skip to content

Commit

Permalink
Simplify ExtendedUnitSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
kanitw committed Feb 10, 2019
1 parent deff137 commit 347a01b
Show file tree
Hide file tree
Showing 10 changed files with 1,564 additions and 3,981 deletions.
5,404 changes: 1,516 additions & 3,888 deletions build/vega-lite-schema.json

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions scripts/rename-schema.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#!/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,'CompositeEncoding','Encoding',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<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
perl -pi -e s,'GenericLayerSpec<CompositeUnitSpec>','LayerSpec',g build/vega-lite-schema.json
perl -pi -e s,'Generic(.*)<FacetedCompositeUnitSpec\,LayerSpec>','\1',g build/vega-lite-schema.json

perl -pi -e s,'FieldDefWithoutScale','FieldDef',g build/vega-lite-schema.json

Expand Down
8 changes: 1 addition & 7 deletions src/compositemark/boxplot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber, isObject} from 'vega-util';
import {Config} from '../config';
import {Encoding, extractTransformsFromEncoding} from '../encoding';
import {Field, PositionFieldDef} from '../fielddef';
import {PositionFieldDef} from '../fielddef';
import * as log from '../log';
import {isMarkDef, MarkDef} from '../mark';
import {GenericUnitSpec, NormalizedLayerSpec, NormalizedUnitSpec} from '../spec';
Expand All @@ -19,10 +19,6 @@ import {
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 @@ -70,8 +66,6 @@ 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
Expand Down
13 changes: 7 additions & 6 deletions src/compositemark/errorbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import {
} 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 @@ -49,8 +45,7 @@ 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'> {
export interface ErrorExtraEncoding<F extends Field> {
/**
* Error value of x coordinates for error specified `"errorbar"` and `"errorband"`.
*/
Expand All @@ -74,6 +69,12 @@ export interface ErrorEncoding<F extends Field>
yError2?: SecondaryFieldDef<F> | ValueDef<number>;
}

export type ErrorEncoding<F extends Field> = Pick<
Encoding<F>,
'x' | 'y' | 'x2' | 'y2' | 'color' | 'detail' | 'opacity'
> &
ErrorExtraEncoding<F>;

export const ERRORBAR_PARTS = keys(ERRORBAR_PART_INDEX);

export type ErrorBarPartsMixins = PartsMixins<ErrorBarPart>;
Expand Down
22 changes: 8 additions & 14 deletions src/compositemark/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import {Config} from '../config';
import {Encoding} from '../encoding';
import {Field} from '../fielddef';
import {AnyMark, isMarkDef} from '../mark';
import {GenericUnitSpec, NormalizedLayerSpec} from '../spec';
import {FacetMapping} from '../spec/facet';
import {keys} from '../util';
import {
BOXPLOT,
BoxPlot,
BOXPLOT_PARTS,
BoxPlotConfigMixins,
BoxPlotDef,
BoxPlotUnitSpec,
normalizeBoxPlot
} from './boxplot';
import {BOXPLOT, BoxPlot, BOXPLOT_PARTS, BoxPlotConfigMixins, BoxPlotDef, normalizeBoxPlot} from './boxplot';
import {
ERRORBAND,
ErrorBand,
ERRORBAND_PARTS,
ErrorBandConfigMixins,
ErrorBandDef,
ErrorBandUnitSpec,
normalizeErrorBand
} from './errorband';
import {
Expand All @@ -26,7 +20,7 @@ import {
ERRORBAR_PARTS,
ErrorBarConfigMixins,
ErrorBarDef,
ErrorBarUnitSpec,
ErrorExtraEncoding,
normalizeErrorBar
} from './errorbar';

Expand All @@ -53,9 +47,9 @@ export function remove(mark: string) {
delete compositeMarkRegistry[mark];
}

export type CompositeMarkUnitSpec<
EE = {} // extra encoding parameter (for faceted composite unit spec)
> = ErrorBarUnitSpec<EE> | ErrorBandUnitSpec<EE> | BoxPlotUnitSpec<EE>;
export type CompositeEncoding = Encoding<Field> & ErrorExtraEncoding<Field>;
export type FacetedCompositeEncoding = Encoding<Field> & ErrorExtraEncoding<Field> & FacetMapping<Field>;

export type CompositeMark = BoxPlot | ErrorBar | ErrorBand;

export function getAllCompositeMarks() {
Expand Down
28 changes: 14 additions & 14 deletions src/spec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ import {GenericFacetSpec, isFacetSpec} from './facet';
import {ExtendedLayerSpec, GenericLayerSpec, isLayerSpec, NormalizedLayerSpec} from './layer';
import {GenericRepeatSpec, isRepeatSpec} from './repeat';
import {TopLevel} from './toplevel';
import {
FacetedExtendedUnitSpec,
GenericUnitSpec,
isUnitSpec,
NormalizedUnitSpec,
TopLevelFacetedUnitSpec
} from './unit';
import {FacetedCompositeUnitSpec, GenericUnitSpec, isUnitSpec, NormalizedUnitSpec, TopLevelUnitSpec} from './unit';

export {normalizeTopLevelSpec as normalize} from '../normalize';
export {BaseSpec, DataMixins, LayoutSizeMixins} from './base';
Expand All @@ -39,7 +33,13 @@ export {GenericFacetSpec, isFacetSpec, NormalizedFacetSpec} from './facet';
export {ExtendedLayerSpec, GenericLayerSpec, isLayerSpec, NormalizedLayerSpec} from './layer';
export {GenericRepeatSpec, isRepeatSpec, NormalizedRepeatSpec} from './repeat';
export {TopLevel} from './toplevel';
export {ExtendedUnitSpec, FacetedExtendedUnitSpec, GenericUnitSpec, isUnitSpec, NormalizedUnitSpec} from './unit';
export {
CompositeUnitSpec as ExtendedUnitSpec,
FacetedCompositeUnitSpec as FacetedExtendedUnitSpec,
GenericUnitSpec,
isUnitSpec,
NormalizedUnitSpec
} from './unit';

/**
* Any specification in Vega-Lite.
Expand All @@ -57,20 +57,20 @@ export type GenericSpec<U extends GenericUnitSpec<any, any>, L extends GenericLa
*/
export type NormalizedSpec = GenericSpec<NormalizedUnitSpec, NormalizedLayerSpec>;

export type TopLevelFacetSpec = TopLevel<GenericFacetSpec<FacetedExtendedUnitSpec, ExtendedLayerSpec>> & DataMixins;
export type TopLevelFacetSpec = TopLevel<GenericFacetSpec<FacetedCompositeUnitSpec, ExtendedLayerSpec>> & DataMixins;

/**
* A Vega-Lite top-level specification.
* This is the root class for all Vega-Lite specifications.
* (The json schema is generated from this type.)
*/
export type TopLevelSpec =
| TopLevelFacetedUnitSpec
| TopLevelUnitSpec
| TopLevelFacetSpec
| TopLevel<ExtendedLayerSpec>
| TopLevel<GenericRepeatSpec<FacetedExtendedUnitSpec, ExtendedLayerSpec>>
| TopLevel<GenericVConcatSpec<FacetedExtendedUnitSpec, ExtendedLayerSpec>>
| TopLevel<GenericHConcatSpec<FacetedExtendedUnitSpec, ExtendedLayerSpec>>;
| TopLevel<GenericRepeatSpec<FacetedCompositeUnitSpec, ExtendedLayerSpec>>
| TopLevel<GenericVConcatSpec<FacetedCompositeUnitSpec, ExtendedLayerSpec>>
| TopLevel<GenericHConcatSpec<FacetedCompositeUnitSpec, ExtendedLayerSpec>>;

/* Custom type guards */

Expand Down Expand Up @@ -126,7 +126,7 @@ export function fieldDefs(spec: GenericSpec<any, any>): TypedFieldDef<any>[] {
return vals(fieldDefIndex(spec));
}

export function isStacked(spec: TopLevel<FacetedExtendedUnitSpec>, config?: Config): boolean {
export function isStacked(spec: TopLevel<FacetedCompositeUnitSpec>, config?: Config): boolean {
config = config || spec.config;
if (isPrimitiveMark(spec.mark)) {
return stack(spec.mark, spec.encoding, config ? config.stack : undefined) !== null;
Expand Down
9 changes: 4 additions & 5 deletions src/spec/layer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {Encoding} from '../encoding';
import {Field} from '../fielddef';
import {CompositeEncoding} from '../compositemark/index';
import {Projection} from '../projection';
import {Resolve} from '../resolve';
import {BaseSpec, LayerUnitMixins} from './base';
import {ExtendedUnitSpec, GenericUnitSpec, NormalizedUnitSpec} from './unit';
import {CompositeUnitSpec, GenericUnitSpec, NormalizedUnitSpec} from './unit';

/**
* Base interface for a layer specification.
Expand All @@ -25,11 +24,11 @@ export interface GenericLayerSpec<U extends GenericUnitSpec<any, any>> extends B
/**
* Layer Spec with `encoding` and `projection` shorthands that will be applied to underlying unit (single-view) specifications.
*/
export interface ExtendedLayerSpec extends GenericLayerSpec<ExtendedUnitSpec> {
export interface ExtendedLayerSpec extends GenericLayerSpec<CompositeUnitSpec> {
/**
* A shared key-value mapping between encoding channels and definition of fields in the underlying layers.
*/
encoding?: Encoding<Field>;
encoding?: CompositeEncoding;

/**
* An object defining properties of the geographic projection shared by underlying layers.
Expand Down
40 changes: 7 additions & 33 deletions src/spec/unit.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {BoxPlotUnitSpec} from '../compositemark/boxplot';
import {ErrorBandUnitSpec} from '../compositemark/errorband';
import {ErrorBarUnitSpec} from '../compositemark/errorbar';
import {CompositeMarkUnitSpec} from '../compositemark/index';
import {CompositeEncoding, FacetedCompositeEncoding} from '../compositemark/index';
import {Encoding} from '../encoding';
import {Field} from '../fielddef';
import {Mark, MarkDef} from '../mark';
import {AnyMark, Mark, MarkDef} from '../mark';
import {Projection} from '../projection';
import {SelectionDef} from '../selection';
import {BaseSpec, DataMixins, LayerUnitMixins} from './base';
import {FacetMapping} from './facet';
import {TopLevel} from './toplevel';

/**
Expand Down Expand Up @@ -41,42 +37,20 @@ export interface GenericUnitSpec<E extends Encoding<any>, M> extends BaseSpec, L
/**
* A unit specification without any shortcut/expansion syntax.
*/
export type NormalizedUnitSpec<
/** Extra Encoding */
EE = {}
> = GenericUnitSpec<Encoding<Field> & EE, Mark | MarkDef>;

/* tslint:disable */
// Need to declare empty object so the generated schema has a reasonable name for ExtendedUnitSpec
export interface EmptyObject {}
/* tslint:enable */
export type NormalizedUnitSpec = GenericUnitSpec<Encoding<Field>, Mark | MarkDef>;

/**
* Unit spec that can be normalized/expanded into a layer spec or another unit spec.
*/
export type ExtendedUnitSpec<
/** Extra Encoding */
EE = EmptyObject
> = NormalizedUnitSpec<EE> | CompositeMarkUnitSpec<EE>;
export type CompositeUnitSpec = GenericUnitSpec<CompositeEncoding, AnyMark>;

/**
* Unit spec that can have a composite mark and row or column channels (shorthand for a facet spec).
*/
export type FacetedExtendedUnitSpec = ExtendedUnitSpec<FacetMapping<Field>>;

// Note: The following three declarations are equivalent to:
// ```
// export type TopLevelFacetedUnitSpec = TopLevel<FacetedExtendedUnitSpec> & DataMixins;
// ```
// However, the JSON schema generator does not support the simpler syntax
export type FacetedCompositeUnitSpec = GenericUnitSpec<FacetedCompositeEncoding, AnyMark>;

export type TopLevelNormalizedUnitSpec = TopLevel<NormalizedUnitSpec<FacetMapping<Field>>> & DataMixins;
export type TopLevelCompositeMarkUnitSpec =
| (TopLevel<ErrorBarUnitSpec<FacetMapping<Field>>> & DataMixins)
| (TopLevel<ErrorBandUnitSpec<FacetMapping<Field>>> & DataMixins)
| (TopLevel<BoxPlotUnitSpec<FacetMapping<Field>>> & DataMixins);
export type TopLevelFacetedUnitSpec = TopLevelNormalizedUnitSpec | TopLevelCompositeMarkUnitSpec;
export type TopLevelUnitSpec = TopLevel<FacetedCompositeUnitSpec> & DataMixins;

export function isUnitSpec(spec: BaseSpec): spec is FacetedExtendedUnitSpec | NormalizedUnitSpec {
export function isUnitSpec(spec: BaseSpec): spec is FacetedCompositeUnitSpec | NormalizedUnitSpec {
return !!spec['mark'];
}
4 changes: 2 additions & 2 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {toSet} from 'vega-util';
import {BAR, isMarkDef} from './mark';
import {FacetedExtendedUnitSpec} from './spec';
import {FacetedCompositeUnitSpec} from './spec/unit';

// TODO: move to vl.spec.validator?
export interface RequiredChannelMap {
Expand Down Expand Up @@ -56,7 +56,7 @@ export const DEFAULT_SUPPORTED_CHANNEL_TYPE: SupportedChannelMap = {
* or null if the encoding is valid.
*/
export function getEncodingMappingError(
spec: FacetedExtendedUnitSpec,
spec: FacetedCompositeUnitSpec,
requiredChannelMap: RequiredChannelMap = DEFAULT_REQUIRED_CHANNEL_MAP,
supportedChannelMap: SupportedChannelMap = DEFAULT_SUPPORTED_CHANNEL_TYPE
) {
Expand Down
4 changes: 2 additions & 2 deletions test/compositemark/errorbar.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* tslint:disable:quotemark */
import {AggregateOp} from 'vega';
import {ErrorBarCenter, ErrorBarExtent} from '../../src/compositemark/errorbar';
import {CompositeMarkUnitSpec} from '../../src/compositemark/index';
import {defaultConfig} from '../../src/config';
import {isFieldDef} from '../../src/fielddef';
import * as log from '../../src/log';
import {isMarkDef} from '../../src/mark';
import {ExtendedLayerSpec, GenericSpec, isLayerSpec, isUnitSpec, normalize} from '../../src/spec';
import {CompositeUnitSpec} from '../../src/spec/unit';
import {isAggregate, isCalculate, Transform} from '../../src/transform';
import {some} from '../../src/util';

Expand Down Expand Up @@ -615,7 +615,7 @@ describe('normalizeErrorBar for all possible extents and centers with raw data i

for (const center of centers) {
for (const extent of extents) {
const spec: GenericSpec<CompositeMarkUnitSpec, ExtendedLayerSpec> = {
const spec: GenericSpec<CompositeUnitSpec, ExtendedLayerSpec> = {
data: {url: 'data/population.json'},
mark: {type, ...(center ? {center} : {}), ...(extent ? {extent} : {})},
encoding: {
Expand Down

0 comments on commit 347a01b

Please sign in to comment.