Skip to content

Commit

Permalink
facet missing data bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wirhabenzeit committed Sep 16, 2024
1 parent d5e98a8 commit 1401c7d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
30 changes: 30 additions & 0 deletions examples/specs/facet_bug.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {
"values": [
{ "first": "B", "second": "a", "value": "B/a"},
{ "first": "A", "second": "a", "value": "A/a"},
{ "first": "C", "second": "a", "value": "C/a"},
{ "first": "B", "second": "b", "value": "B/b"},
{ "first": "A", "second": "b", "value": "A/b"}
]
},
"facet": {
"row": { "field": "second" },
"column": { "field": "first", "sort": ["B","C","A"] }
},
"spec": {
"height": 50,
"width": 50,
"layer": [
{
"mark": { "type": "text" },
"encoding": {
"x": { "value": 25 },
"y": { "value": 25 },
"text": { "field": "value" }
}
}
]
}
}
17 changes: 17 additions & 0 deletions examples/specs/facet_bug_cars.vl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"data": {"url": "data/cars.json"},
"facet": {
"column": {"field": "Origin", "sort": {"field": "Acceleration", "op": "count"}},
"row": {"field": "Cylinders", "sort": {"field": "Horsepower", "op": "count"}}
},
"spec": {
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Acceleration", "type": "quantitative"},
"color": {"field": "Origin", "type": "nominal"},
"shape": {"field": "Cylinders", "type": "nominal"}
}
}
}
32 changes: 9 additions & 23 deletions src/compile/facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Config} from '../config';
import {ExprRef, replaceExprRef} from '../expr';
import * as log from '../log';
import {hasDiscreteDomain} from '../scale';
import {DEFAULT_SORT_OP, EncodingSortField, isSortField, SortOrder} from '../sort';
import {EncodingSortField, isSortField, SortOrder} from '../sort';
import {NormalizedFacetSpec} from '../spec';
import {EncodingFacetMapping, FacetFieldDef, FacetMapping, isFacetMapping} from '../spec/facet';
import {hasProperty, keys} from '../util';
Expand Down Expand Up @@ -294,34 +294,20 @@ export class FacetModel extends ModelWithField {
for (const channel of FACET_CHANNELS) {
const fieldDef = this.facet[channel];
if (fieldDef) {
groupby.push(vgField(fieldDef));

const {bin, sort} = fieldDef;

if (isBinning(bin)) {
groupby.push(vgField(fieldDef, {binSuffix: 'end'}));
}

if (isSortField(sort)) {
const {field, op = DEFAULT_SORT_OP} = sort;
const outputName = facetSortFieldName(fieldDef, sort);
if (row && column) {
// For crossed facet, use pre-calculate field as it requires a different groupby
// For each calculated field, apply max and assign them to the same name as
// all values of the same group should be the same anyway.
fields.push(outputName);
ops.push('max');
as.push(outputName);
} else {
fields.push(field);
ops.push(op);
as.push(outputName);
}
groupby.push(outputName);
} else if (isArray(sort)) {
const outputName = sortArrayIndexField(fieldDef, channel);
fields.push(outputName);
ops.push('max');
as.push(outputName);
groupby.push(outputName);
} else {
groupby.push(vgField(fieldDef));
}

if (isBinning(bin)) {
groupby.push(vgField(fieldDef, {binSuffix: 'end'}));
}
}
}
Expand Down

0 comments on commit 1401c7d

Please sign in to comment.