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: add field from scale-range to aggregate groupby #7576

Merged
merged 7 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/compile/data/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
isGeoPositionChannel,
isScaleChannel
} from '../../channel';
import {binRequiresRange, FieldDef, hasBandEnd, isTypedFieldDef, vgField} from '../../channeldef';
import {binRequiresRange, FieldDef, hasBandEnd, isScaleFieldDef, isTypedFieldDef, vgField} from '../../channeldef';
import * as log from '../../log';
import {isFieldRange} from '../../scale';
import {AggregateTransform} from '../../transform';
import {Dict, duplicate, hash, keys, replacePathInField, setEqual} from '../../util';
import {isUnitModel, ModelWithField} from '../model';
Expand Down Expand Up @@ -37,6 +38,9 @@ function addDimension(dims: Set<string>, channel: Channel, fieldDef: FieldDef<st
} else {
dims.add(vgField(fieldDef));
}
if (isScaleFieldDef(fieldDef) && isFieldRange(fieldDef.scale?.range)) {
dims.add(fieldDef.scale.range.field);
}
return dims;
}

Expand Down
9 changes: 9 additions & 0 deletions src/scale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
isObject,
RangeEnum,
ScaleBins,
ScaleInterpolateEnum,
Expand Down Expand Up @@ -439,6 +440,14 @@ export function isDomainUnionWith(domain: Domain): domain is DomainUnionWith {
return domain && domain['unionWith'];
}

export interface FieldRange {
field: string;
}

export function isFieldRange(range: any): range is FieldRange {
return isObject(range) && 'field' in range;
}

export interface Scale<ES extends ExprRef | SignalRef = ExprRef | SignalRef> {
/**
* The type of scale. Vega-Lite supports the following categories of scale types:
Expand Down
20 changes: 20 additions & 0 deletions test/compile/data/aggregate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,26 @@ describe('compile/data/aggregate', () => {
});
});

it('should add correct dimension when field is in scale range', () => {
const model = parseUnitModel({
mark: 'point',
encoding: {
x: {timeUnit: 'year', field: 'date'},
y: {aggregate: 'mean', field: 'Displacement', type: 'quantitative'},
color: {field: 'symbol', type: 'nominal', scale: {range: {field: 'color'}}}
}
});

const agg = AggregateNode.makeFromEncoding(null, model);
expect(agg.assemble()).toEqual({
type: 'aggregate',
groupby: ['year_date', 'symbol', 'color'],
ops: ['mean'],
fields: ['Displacement'],
as: ['mean_Displacement']
});
});

it('adds correct measure for argmin/max', () => {
const model = parseUnitModel({
mark: 'point',
Expand Down