-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add post enumeration modification to generate.ts #187
Changes from 25 commits
d460213
c1a99d8
435c733
e811e39
5c5488a
e9eac3d
8d3bed4
5600bcb
c9a4dd5
6e59c50
3213642
efa4898
1452dd6
fc97be4
733680b
2293acd
510255b
014bd8c
f0a9665
4e5ba6f
be8149c
34e9917
f0bfe63
682921f
c4e21f3
234e71c
ece9ee7
3f2cfb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 {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) > 10) { | ||
|
||
// 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) > 10) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. value in the config |
||
|
||
// 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) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why |
||
(schema.cardinality(colorEncQ) > 10)) { | ||
|
||
if (colorEncQ.scale === undefined) { | ||
colorEncQ.scale = {}; | ||
} | ||
|
||
if (colorEncQ.scale) { | ||
if (!(colorEncQ.scale as ScaleQuery).range) { | ||
(colorEncQ.scale as ScaleQuery).range = 'category20'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use value in the config. |
||
} | ||
} | ||
} | ||
|
||
return specM; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as Y