-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add post enumeration modification to generate.ts (#187)
fix #176 - add nominalColorScaleForHighCardinality config: fix #101 - add smallBandSizeForFacet config: fix #93 - add smallBandSizeForHighCardinality config: fix #93 - create Stylize.ts to handle post enumeration modification
- Loading branch information
1 parent
f91dc68
commit 2f68187
Showing
6 changed files
with
390 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {Channel} from 'vega-lite/src/channel'; | ||
import {ScaleType} from 'vega-lite/src/scale'; | ||
import {Type} from 'vega-lite/src/type'; | ||
|
||
import {DEFAULT_QUERY_CONFIG, QueryConfig} from './config'; | ||
import {SpecQueryModel} from './model'; | ||
import {EncodingQuery, ScaleQuery, scaleType} from './query/encoding'; | ||
import {Schema} from './schema'; | ||
import {contains, Dict} from './util'; | ||
|
||
export function stylize(answerSet: SpecQueryModel[], schema: Schema, opt: QueryConfig): SpecQueryModel[] { | ||
answerSet = answerSet.map(function(specM) { | ||
if (opt.smallBandSizeForHighCardinalityOrFacet) { | ||
specM = smallBandSizeForHighCardinalityOrFacet(specM, schema); | ||
} | ||
|
||
if (opt.nominalColorScaleForHighCardinality) { | ||
specM = nominalColorScaleForHighCardinality(specM, schema); | ||
} | ||
return specM; | ||
}); | ||
|
||
return answerSet; | ||
} | ||
|
||
let encQIndex: Dict<EncodingQuery> = {}; | ||
|
||
export function smallBandSizeForHighCardinalityOrFacet(specM: SpecQueryModel, schema: Schema): SpecQueryModel { | ||
[Channel.ROW, Channel.Y, Channel.COLUMN, Channel.X].forEach((channel) => { | ||
encQIndex[channel] = specM.getEncodingQueryByChannel(channel); | ||
}); | ||
|
||
const yEncQ = encQIndex[Channel.Y]; | ||
if (yEncQ !== undefined) { | ||
if (encQIndex[Channel.ROW] || | ||
schema.cardinality(yEncQ) > DEFAULT_QUERY_CONFIG.smallBandSizeForHighCardinalityOrFacet.maxCardinality) { | ||
|
||
// We check for undefined rather than | ||
// yEncQ.scale = yEncQ.scale || {} to cover the case where | ||
// yEncQ.scale has been set to false/null. | ||
// This prevents us from incorrectly overriding scale and | ||
// assigning a bandSize when scale is set to false. | ||
if (yEncQ.scale === undefined) { | ||
yEncQ.scale = {}; | ||
} | ||
|
||
// We do not want to assign a bandSize if scale is set to false | ||
// and we only apply this if the scale is (or can be) an ordinal scale. | ||
if (yEncQ.scale && contains([ScaleType.ORDINAL, undefined], scaleType((yEncQ.scale as ScaleQuery).type, yEncQ.timeUnit, yEncQ.type))) { | ||
if (!(yEncQ.scale as ScaleQuery).bandSize) { | ||
(yEncQ.scale as ScaleQuery).bandSize = 12; | ||
} | ||
} | ||
} | ||
} | ||
|
||
const xEncQ = encQIndex[Channel.X]; | ||
if (xEncQ !== undefined) { | ||
if (encQIndex[Channel.COLUMN] || | ||
schema.cardinality(xEncQ) > DEFAULT_QUERY_CONFIG.smallBandSizeForHighCardinalityOrFacet.maxCardinality) { | ||
|
||
// Just like y, we don't want to do this if scale is null/false | ||
if (xEncQ.scale === undefined) { | ||
xEncQ.scale = {}; | ||
} | ||
|
||
// We do not want to assign a bandSize if scale is set to false | ||
// and we only apply this if the scale is (or can be) an ordinal scale. | ||
if (xEncQ.scale && contains([ScaleType.ORDINAL, undefined], scaleType((xEncQ.scale as ScaleQuery).type, xEncQ.timeUnit, xEncQ.type))) { | ||
if (!(xEncQ.scale as ScaleQuery).bandSize) { | ||
(xEncQ.scale as ScaleQuery).bandSize = 12; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return specM; | ||
} | ||
|
||
export function nominalColorScaleForHighCardinality(specM: SpecQueryModel, schema: Schema): SpecQueryModel { | ||
encQIndex[Channel.COLOR] = specM.getEncodingQueryByChannel(Channel.COLOR); | ||
|
||
const colorEncQ = encQIndex[Channel.COLOR]; | ||
if ((colorEncQ !== undefined) && (colorEncQ.type === Type.NOMINAL) && | ||
(schema.cardinality(colorEncQ) > DEFAULT_QUERY_CONFIG.nominalColorScaleForHighCardinality.maxCardinality)) { | ||
|
||
if (colorEncQ.scale === undefined) { | ||
colorEncQ.scale = {}; | ||
} | ||
|
||
if (colorEncQ.scale) { | ||
if (!(colorEncQ.scale as ScaleQuery).range) { | ||
(colorEncQ.scale as ScaleQuery).range = DEFAULT_QUERY_CONFIG.nominalColorScaleForHighCardinality.palette; | ||
} | ||
} | ||
} | ||
|
||
return specM; | ||
} |
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
Oops, something went wrong.