Skip to content

Commit

Permalink
fix: smoothen value gradient for var param -> point selection
Browse files Browse the repository at this point in the history
  • Loading branch information
arvind committed Feb 5, 2021
1 parent e7af254 commit 20a36c1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
43 changes: 25 additions & 18 deletions src/compile/selection/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,44 @@ const project: SelectionCompiler = {
};

const type = selCmpt.type;
const cfg = model.config.selection[type];
const init =
selDef.value !== undefined
? (array(selDef.value as any) as SelectionInitMapping[] | SelectionInitIntervalMapping[])
: null;

// If no explicit projection (either fields or encodings) is specified, set some defaults.
// If an initial value is set, try to infer projections.
// Otherwise, use the default configuration.
let {fields, encodings} = isObject(selDef.select) ? selDef.select : ({} as BaseSelectionConfig);
if (!fields && !encodings) {
const cfg = model.config.selection[type];
if (!fields && !encodings && init) {
for (const initVal of init) {
// initVal may be a scalar value to smoothen varParam -> pointSelection gradient.
if (!isObject(initVal)) {
continue;
}

if (init) {
for (const initVal of init) {
for (const key of keys(initVal)) {
if (isSingleDefUnitChannel(key)) {
(encodings || (encodings = [])).push(key as SingleDefUnitChannel);
for (const key of keys(initVal)) {
if (isSingleDefUnitChannel(key)) {
(encodings || (encodings = [])).push(key as SingleDefUnitChannel);
} else {
if (type === 'interval') {
log.warn(log.message.INTERVAL_INITIALIZED_WITH_X_Y);
encodings = cfg.encodings;
} else {
if (type === 'interval') {
log.warn(log.message.INTERVAL_INITIALIZED_WITH_X_Y);
encodings = cfg.encodings;
} else {
(fields || (fields = [])).push(key);
}
(fields || (fields = [])).push(key);
}
}
}
} else {
encodings = cfg.encodings;
fields = cfg.fields;
}
}

// We break this out as a separate if block (instead of an else condition)
// to account for unprojected point selections that have scalar initial values
if (!fields && !encodings) {
encodings = cfg.encodings;
fields = cfg.fields;
}

for (const channel of encodings ?? []) {
const fieldDef = model.fieldDef(channel);
if (fieldDef) {
Expand Down Expand Up @@ -161,7 +166,9 @@ const project: SelectionCompiler = {

if (init) {
selCmpt.init = (init as any).map((v: SelectionInitMapping | SelectionInitIntervalMapping) => {
return proj.items.map(p => (v[p.channel] !== undefined ? v[p.channel] : v[p.field]));
// Selections can be initialized either with a full object that maps projections to values
// or scalar values to smoothen the abstraction gradient from variable params to point selections.
return proj.items.map(p => (isObject(v) ? (v[p.channel] !== undefined ? v[p.channel] : v[p.field]) : v));
});
}

Expand Down
6 changes: 5 additions & 1 deletion src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ export interface SelectionParameter<T extends SelectionType = SelectionType> {
*
* __See also:__ [`init`](https://vega.github.io/vega-lite/docs/init.html) documentation.
*/
value?: T extends 'point' ? SelectionInitMapping[] : T extends 'interval' ? SelectionInitIntervalMapping : never;
value?: T extends 'point'
? SelectionInit | SelectionInitMapping[]
: T extends 'interval'
? SelectionInitIntervalMapping
: never;

/**
* When set, a selection is populated by input elements (also known as dynamic query widgets)
Expand Down
34 changes: 33 additions & 1 deletion test/compile/selection/point.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ describe('Multi Selection', () => {
fields: ['nested.a', 'nested.b'],
clear: false
}
},
// Seven ensures a smooth abstraction gradient for "value" var params -> point selections
{
name: 'seven',
value: 50,
select: {type: 'point', fields: ['Horsepower']}
},
// Eight ensures this smooth "value" gradient logic doesn't kick in on unprojected point selections
{
name: 'eight',
value: 75,
select: 'point'
}
]));

Expand Down Expand Up @@ -271,7 +283,27 @@ describe('Multi Selection', () => {
}
]
},
{name: 'six_store'}
{name: 'six_store'},
{
name: 'seven_store',
values: [
{
unit: '',
fields: [{type: 'E', field: 'Horsepower'}],
values: [50]
}
]
},
{
name: 'eight_store',
values: [
{
unit: '',
fields: [{type: 'E', field: '_vgsid_'}],
values: [75]
}
]
}
]);
});

Expand Down

0 comments on commit 20a36c1

Please sign in to comment.