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 all 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
23 changes: 13 additions & 10 deletions build/vega-lite-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10391,6 +10391,18 @@
},
"type": "object"
},
"FieldRange": {
"additionalProperties": false,
"properties": {
"field": {
"type": "string"
}
},
"required": [
"field"
],
"type": "object"
},
"FieldRangePredicate": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -21069,16 +21081,7 @@
"type": "array"
},
{
"additionalProperties": false,
"properties": {
"field": {
"type": "string"
}
},
"required": [
"field"
],
"type": "object"
"$ref": "#/definitions/FieldRange"
}
],
"description": "The range of the scale. One of:\n\n- A string indicating a [pre-defined named scale range](https://vega.github.io/vega-lite/docs/scale.html#range-config) (e.g., example, `\"symbol\"`, or `\"diverging\"`).\n\n- For [continuous scales](https://vega.github.io/vega-lite/docs/scale.html#continuous), two-element array indicating minimum and maximum values, or an array with more than two entries for specifying a [piecewise scale](https://vega.github.io/vega-lite/docs/scale.html#piecewise).\n\n- For [discrete](https://vega.github.io/vega-lite/docs/scale.html#discrete) and [discretizing](https://vega.github.io/vega-lite/docs/scale.html#discretizing) scales, an array of desired output values or an object with a `field` property representing the range values. For example, if a field `color` contains CSS color names, we can set `range` to `{field: \"color\"}`.\n\n__Notes:__\n\n1) For color scales you can also specify a color [`scheme`](https://vega.github.io/vega-lite/docs/scale.html#scheme) instead of `range`.\n\n2) Any directly specified `range` for `x` and `y` channels will be ignored. Range can be customized via the view's corresponding [size](https://vega.github.io/vega-lite/docs/size.html) (`width` and `height`)."
Expand Down
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
11 changes: 10 additions & 1 deletion 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 Expand Up @@ -513,7 +522,7 @@ export interface Scale<ES extends ExprRef | SignalRef = ExprRef | SignalRef> {
*
* 2) Any directly specified `range` for `x` and `y` channels will be ignored. Range can be customized via the view's corresponding [size](https://vega.github.io/vega-lite/docs/size.html) (`width` and `height`).
*/
range?: RangeEnum | (number | string | number[] | ES)[] | {field: string};
range?: RangeEnum | (number | string | number[] | ES)[] | FieldRange;

/**
* Sets the maximum value in the scale range, overriding the `range` property or the default range. This property is only intended for use with scales having continuous ranges.
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