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: generate unescaped boxplot calculated field aliases #9284

Merged
merged 4 commits into from
Mar 16, 2024
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
36 changes: 20 additions & 16 deletions src/compositemark/boxplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {isMarkDef, MarkDef, MarkInvalidMixins} from '../mark';
import {NormalizerParams} from '../normalize';
import {GenericUnitSpec, NormalizedLayerSpec, NormalizedUnitSpec} from '../spec';
import {AggregatedFieldDef, CalculateTransform, JoinAggregateTransform, Transform} from '../transform';
import {isEmpty, omit} from '../util';
import {isEmpty, omit, removePathFromField} from '../util';
import {CompositeMarkNormalizer} from './base';
import {
compositeMarkContinuousAxis,
Expand Down Expand Up @@ -120,6 +120,8 @@ export function normalizeBoxPlot(
customTooltipWithoutAggregatedField
} = boxParams(spec, extent, config);

const aliasedFieldName = removePathFromField(continuousAxisChannelDef.field);

const {color, size, ...encodingWithoutSizeColorAndContinuousAxis} = encodingWithoutContinuousAxis;

const makeBoxPlotPart = (sharedEncoding: Encoding<string>) => {
Expand Down Expand Up @@ -258,23 +260,23 @@ export function normalizeBoxPlot(
{
op: 'min',
field: continuousAxisChannelDef.field,
as: `lower_whisker_${continuousAxisChannelDef.field}`
as: `lower_whisker_${aliasedFieldName}`
},
{
op: 'max',
field: continuousAxisChannelDef.field,
as: `upper_whisker_${continuousAxisChannelDef.field}`
as: `upper_whisker_${aliasedFieldName}`
},
// preserve lower_box / upper_box
{
op: 'min',
field: `lower_box_${continuousAxisChannelDef.field}`,
as: `lower_box_${continuousAxisChannelDef.field}`
as: `lower_box_${aliasedFieldName}`
},
{
op: 'max',
field: `upper_box_${continuousAxisChannelDef.field}`,
as: `upper_box_${continuousAxisChannelDef.field}`
as: `upper_box_${aliasedFieldName}`
},
...aggregate
],
Expand Down Expand Up @@ -334,16 +336,17 @@ export function normalizeBoxPlot(
}

function boxParamsQuartiles(continousAxisField: string): AggregatedFieldDef[] {
const aliasedFieldName = removePathFromField(continousAxisField);
return [
{
op: 'q1',
field: continousAxisField,
as: `lower_box_${continousAxisField}`
as: `lower_box_${aliasedFieldName}`
},
{
op: 'q3',
field: continousAxisField,
as: `upper_box_${continousAxisField}`
as: `upper_box_${aliasedFieldName}`
}
];
}
Expand All @@ -356,6 +359,7 @@ function boxParams(
const orient = compositeMarkOrient(spec, BOXPLOT);
const {continuousAxisChannelDef, continuousAxis} = compositeMarkContinuousAxis(spec, orient, BOXPLOT);
const continuousFieldName: string = continuousAxisChannelDef.field;
const aliasedFieldName = removePathFromField(continuousFieldName);

const boxPlotType = getBoxPlotType(extent);

Expand All @@ -364,17 +368,17 @@ function boxParams(
{
op: 'median',
field: continuousFieldName,
as: `mid_box_${continuousFieldName}`
as: `mid_box_${aliasedFieldName}`
},
{
op: 'min',
field: continuousFieldName,
as: (boxPlotType === 'min-max' ? 'lower_whisker_' : 'min_') + continuousFieldName
as: (boxPlotType === 'min-max' ? 'lower_whisker_' : 'min_') + aliasedFieldName
},
{
op: 'max',
field: continuousFieldName,
as: (boxPlotType === 'min-max' ? 'upper_whisker_' : 'max_') + continuousFieldName
as: (boxPlotType === 'min-max' ? 'upper_whisker_' : 'max_') + aliasedFieldName
}
];

Expand All @@ -384,16 +388,16 @@ function boxParams(
: [
// This is for the original k-IQR, which we do not expose
{
calculate: `datum["upper_box_${continuousFieldName}"] - datum["lower_box_${continuousFieldName}"]`,
as: `iqr_${continuousFieldName}`
calculate: `datum["upper_box_${aliasedFieldName}"] - datum["lower_box_${aliasedFieldName}"]`,
as: `iqr_${aliasedFieldName}`
},
{
calculate: `min(datum["upper_box_${continuousFieldName}"] + datum["iqr_${continuousFieldName}"] * ${extent}, datum["max_${continuousFieldName}"])`,
as: `upper_whisker_${continuousFieldName}`
calculate: `min(datum["upper_box_${aliasedFieldName}"] + datum["iqr_${aliasedFieldName}"] * ${extent}, datum["max_${aliasedFieldName}"])`,
as: `upper_whisker_${aliasedFieldName}`
},
{
calculate: `max(datum["lower_box_${continuousFieldName}"] - datum["iqr_${continuousFieldName}"] * ${extent}, datum["min_${continuousFieldName}"])`,
as: `lower_whisker_${continuousFieldName}`
calculate: `max(datum["lower_box_${aliasedFieldName}"] - datum["iqr_${aliasedFieldName}"] * ${extent}, datum["min_${aliasedFieldName}"])`,
as: `lower_whisker_${aliasedFieldName}`
}
];

Expand Down
70 changes: 70 additions & 0 deletions test/compositemark/boxplot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,76 @@ describe('normalizeBoxMinMax', () => {
]);
});

it('should produce correct transform with values that include periods in field names', () => {
const output = normalize(
{
description: 'A box plot showing median, min, and max in the US population distribution of age groups in 2000.',
data: {
values: [
{'foo.bar': 10},
{'foo.bar': 20},
{'foo.bar': 30},
{'foo.bar': 40},
{'foo.bar': 50},
{'foo.bar': 60},
{'foo.bar': 70},
{'foo.bar': 80},
{'foo.bar': 90},
{'foo.bar': 100}
]
},
mark: {
type: 'boxplot',
extent: 'min-max',
size: 5
},
encoding: {
x: {field: 'one\\.two', type: 'quantitative'},
y: {
field: 'three\\.four',
type: 'quantitative',
axis: {title: 'Population'}
},
color: {value: 'skyblue'}
}
},
defaultConfig
);

expect(output.transform).toEqual([
{
aggregate: [
{
op: 'q1',
field: 'three\\.four',
as: 'lower_box_three.four' // aliases should be unescaped
},
{
op: 'q3',
field: 'three\\.four',
as: 'upper_box_three.four'
},
{
op: 'median',
field: 'three\\.four',
as: 'mid_box_three.four'
},
{
op: 'min',
field: 'three\\.four',
as: 'lower_whisker_three.four'
},
{
op: 'max',
field: 'three\\.four',
as: 'upper_whisker_three.four'
}
],
groupby: ['one\\.two'] // field reference should be escaped
}
]);
});

it('should produce an error if neither the x axis or y axis is specified', () => {
expect(() => {
normalize(
Expand Down