Skip to content
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

fix: correctly flatten nested field references for scale bindings #7159

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compile/selection/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {selectionCompilers, SelectionComponent, STORE} from '.';
import {warn} from '../../log';
import {LogicalComposition} from '../../logical';
import {BaseSelectionConfig, SelectionDef, SelectionExtent} from '../../selection';
import {Dict, duplicate, entries, logicalExpr, varName} from '../../util';
import {Dict, duplicate, entries, logicalExpr, replacePathInField, varName} from '../../util';
import {DataFlowNode, OutputNode} from '../data/dataflow';
import {FilterNode} from '../data/filter';
import {Model} from '../model';
Expand Down Expand Up @@ -120,7 +120,7 @@ export function parseSelectionBinExtent(selCmpt: SelectionComponent, extent: Sel
}
}

return `${selCmpt.name}[${stringValue(field)}]`;
return `${selCmpt.name}[${stringValue(replacePathInField(field))}]`;
}

export function materializeSelections(model: UnitModel, main: OutputNode) {
Expand Down
7 changes: 5 additions & 2 deletions src/compile/selection/scales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {isLayerModel, Model} from '../model';
import {UnitModel} from '../unit';
import {SelectionProjection} from './project';
import {SelectionCompiler} from '.';
import {replacePathInField} from '../../util';

const scaleBindings: SelectionCompiler<'interval'> = {
defined: selCmpt => {
Expand Down Expand Up @@ -55,10 +56,12 @@ const scaleBindings: SelectionCompiler<'interval'> = {
const namedSg = signals.filter(s => s.name === selCmpt.name)[0];
let update = namedSg.update;
if (update.indexOf(VL_SELECTION_RESOLVE) >= 0) {
namedSg.update = `{${bound.map(proj => `${stringValue(proj.field)}: ${proj.signals.data}`).join(', ')}}`;
namedSg.update = `{${bound
.map(proj => `${stringValue(replacePathInField(proj.field))}: ${proj.signals.data}`)
.join(', ')}}`;
} else {
for (const proj of bound) {
const mapping = `${stringValue(proj.field)}: ${proj.signals.data}`;
const mapping = `${stringValue(replacePathInField(proj.field))}: ${proj.signals.data}`;
if (!update.includes(mapping)) {
update = `${update.substring(0, update.length - 1)}, ${mapping}}`;
}
Expand Down
42 changes: 38 additions & 4 deletions test/compile/selection/scales.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ describe('Selection + Scales', () => {

let scales = assembleScalesForModel(model);
expect(scales[0]).toHaveProperty('domainRaw');
expect(scales[0].domainRaw).toEqual({signal: 'grid["nested.b"]'});
expect(scales[0].domainRaw).toEqual({signal: 'grid["nested\\\\.b"]'});
expect(scales[1]).toHaveProperty('domainRaw');
expect(scales[1].domainRaw).toEqual({signal: 'grid["nested.a"]'});
expect(scales[1].domainRaw).toEqual({signal: 'grid["nested\\\\.a"]'});

model = parseConcatModel({
vconcat: [
Expand Down Expand Up @@ -216,11 +216,11 @@ describe('Selection + Scales', () => {

scales = assembleScalesForModel(model.children[1]);
expect(scales[0]).toHaveProperty('domainRaw');
expect(scales[0].domainRaw).toEqual({signal: 'brush["nested.a"]'});
expect(scales[0].domainRaw).toEqual({signal: 'brush["nested\\\\.a"]'});

scales = assembleScalesForModel(model.children[2]);
expect(scales[0]).toHaveProperty('domainRaw');
expect(scales[0].domainRaw).toEqual({signal: 'brush["nested.a"]'});
expect(scales[0].domainRaw).toEqual({signal: 'brush["nested\\\\.a"]'});
});
});

Expand Down Expand Up @@ -325,6 +325,40 @@ describe('Selection + Scales', () => {
'{"Miles_per_Gallon": selector001_Miles_per_Gallon, "Weight_in_lbs": selector001_Weight_in_lbs, "Acceleration": selector001_Acceleration, "Horsepower": selector001_Horsepower}'
);
});

it('should correctly nest fields for top-level signals', () => {
const model = parseModel({
repeat: {
row: ['p.x', 'p.y'],
column: ['p.x', 'p.y']
},
spec: {
mark: 'point',
params: [
{
name: 'sel11',
select: 'interval',
bind: 'scales'
}
],
encoding: {
x: {field: {repeat: 'column'}, type: 'quantitative'},
y: {field: {repeat: 'row'}, type: 'quantitative'}
},
data: {
values: [{p: {x: 1, y: 1}}, {p: {x: 2, y: 1}}, {p: {x: 1, y: 2}}, {p: {x: 3, y: 3}}, {p: {x: 3, y: 2}}]
}
}
});

model.parseScale();
model.parseSelections();

const signals = assembleTopLevelSignals(model.children[2] as UnitModel, []);
const named = signals.filter(s => s.name === 'sel11') as NewSignal[];
expect(named).toHaveLength(1);
expect(named[0].update).toEqual('{"p\\\\.x": sel11_p_x, "p\\\\.y": sel11_p_y}');
});
});

it(
Expand Down