Skip to content

Commit

Permalink
add post enumeration modification to generate.ts
Browse files Browse the repository at this point in the history
- add nominalScaleForHighCardinality config
- add smallBandSizeForFacet config
- add smallBandSizeForHighCardinality config
  • Loading branch information
espressoroaster committed Aug 7, 2016
1 parent f91dc68 commit d460213
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export interface QueryConfig {
preferredNominalAxis?: Channel;
preferredFacet?: Channel;

// Not sure where these belong..
smallBandSizeForHighCardinality?: {maxCardinality: number, bandSize: number};
smallBandSizeForFacet?: {maxCardinality: number, bandSize: number};
nominalScaleForHighCardinality?: {maxCardinality: number};


// Encoding Constraints

maxCardinalityForCategoricalColor?: number;
Expand Down Expand Up @@ -150,6 +156,11 @@ export const DEFAULT_QUERY_CONFIG: QueryConfig = {
preferredNominalAxis: Channel.Y, // nominal on y makes it easier to read.
preferredFacet: Channel.ROW, // row make it easier to scroll than column

// what should their default values be?
smallBandSizeForHighCardinality: {maxCardinality: 10, bandSize: 12},
smallBandSizeForFacet: {maxCardinality: 10, bandSize: 12},
nominalScaleForHighCardinality: {maxCardinality: 10},

// Encoding Constraints -- See description inside src/constraints/encoding.ts
maxCardinalityForCategoricalColor: 20,
maxCardinalityForFacet: 10,
Expand Down
102 changes: 102 additions & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import {Channel} from 'vega-lite/src/channel';
import {Type} from 'vega-lite/src/type';

import {ENUMERATOR_INDEX} from '../src/enumerator';

import {QueryConfig, DEFAULT_QUERY_CONFIG} from './config';
import {SpecQueryModel} from './model';
import {Property} from './property';
import {EncodingQuery} from './query/encoding';
import {SpecQuery} from './query/spec';
import {Schema} from './schema';
import {Dict} from './util';

export function generate(specQ: SpecQuery, schema: Schema, opt: QueryConfig = DEFAULT_QUERY_CONFIG) {
// 1. Build a SpecQueryModel, which also contains enumSpecIndex
Expand All @@ -22,5 +28,101 @@ export function generate(specQ: SpecQuery, schema: Schema, opt: QueryConfig = DE
}
});

let ENCODING_QUERY_INDEX: Dict<EncodingQuery> = {};

if (opt.smallBandSizeForHighCardinality || opt.smallBandSizeForFacet) {
answerSet = answerSet.map(function(specQM) {

[Channel.ROW, Channel.Y, Channel.COLUMN, Channel.X].forEach((channel) => {
ENCODING_QUERY_INDEX[channel] = specQM.getEncodingQueryByChannel(channel);
});

const yEncQ = ENCODING_QUERY_INDEX[Channel.Y];
if (yEncQ !== undefined) {
if (ENCODING_QUERY_INDEX[Channel.ROW] ||
schema.cardinality(yEncQ) > 10) {

if (yEncQ.scale === undefined) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.Y),
Property.SCALE,
true,
{name: 'scale', values: [true, false]} // not sure what this EnumSpec should be
);
}

if (yEncQ.scale) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.Y),
Property.SCALE_BANDSIZE,
12,
{name: 'scaleBandSize', values: [12]} // not sure what this EnumSpec should be
);
}
}
}

const xEncQ = ENCODING_QUERY_INDEX[Channel.X];
if (xEncQ !== undefined) {
if (ENCODING_QUERY_INDEX[Channel.COLUMN] ||
schema.cardinality(xEncQ) > 10) {

if (xEncQ.scale === undefined) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.X),
Property.SCALE,
true,
{name: 'scale', values: [true, false]} // not sure what this EnumSpec should be
);
}

if (xEncQ.scale) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.X),
Property.SCALE_BANDSIZE,
12,
{name: 'scaleBandSize', values: [12]} // not sure what this EnumSpec should be
);
}
}
}

return specQM;
});
}

if (opt.nominalScaleForHighCardinality) {
answerSet = answerSet.map(function(specQM) {
ENCODING_QUERY_INDEX[Channel.COLOR] = specQM.getEncodingQueryByChannel(Channel.COLOR);

const colorEncQ = ENCODING_QUERY_INDEX[Channel.COLOR];
if ((colorEncQ !== undefined) && (colorEncQ.type === Type.NOMINAL) &&
(schema.cardinality(colorEncQ) > 10)) {

if (colorEncQ.scale === undefined) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.COLOR),
Property.SCALE,
true,
{name: 'scale', values: [true, false]} // not sure what this EnumSpec should be
);
}

if (colorEncQ.scale) {
specQM.setEncodingProperty(
specQM.getEncodingQueryIndexByChannel(Channel.COLOR),
Property.SCALE_RANGE,
'category20',
{name: 'scaleRange', values: [undefined]} // not sure what this EnumSpec should be
);
}
}

return specQM;
});
}

return answerSet;
}


9 changes: 9 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ export class SpecQueryModel {
return undefined;
}

public getEncodingQueryIndexByChannel(channel: Channel) {
for (let i = 0; i < this._spec.encodings.length; i++) {
if (this._spec.encodings[i].channel === channel) {
return i;
}
}
return undefined;
}

public getEncodingQueryByIndex(i: number) {
return this._spec.encodings[i];
}
Expand Down

0 comments on commit d460213

Please sign in to comment.