Skip to content

Commit

Permalink
remove iterating in parse and instead do for loop in each node, creat…
Browse files Browse the repository at this point in the history
…e export function for sort index field name
  • Loading branch information
sirahd committed Mar 21, 2018
1 parent 90eabb3 commit 5efd2c6
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/compiled/bar_custom_sort_partial.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/compiled/bar_custom_sort_partial.vg.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
"b": 43
},
{
"a": "D",
"a": "Z",
"b": 91
},
{
"a": "E",
"a": "Y",
"b": 81
},
{
"a": "F",
"a": "X",
"b": 53
}
]
Expand Down
2 changes: 1 addition & 1 deletion examples/specs/bar_custom_sort_partial.vl.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"data": {
"values": [
{"a": "A","b": 28}, {"a": "B","b": 55}, {"a": "C","b": 43},
{"a": "D","b": 91}, {"a": "E","b": 81}, {"a": "F","b": 53}
{"a": "Z","b": 91}, {"a": "Y","b": 81}, {"a": "X","b": 53}
]
},
"mark": "bar",
Expand Down
22 changes: 13 additions & 9 deletions src/compile/data/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {ChannelDef, isScaleFieldDef} from '../../fielddef';
import {isScaleFieldDef, ScaleFieldDef, vgField} from '../../fielddef';
import {isSortArray} from '../../sort';
import {duplicate} from '../../util';
import {VgFormulaTransform} from '../../vega.schema';
import {ModelWithField} from '../model';
import {Channel} from './../../channel';
import {SingleDefChannel} from './../../channel';
import {CalculateTransform} from './../../transform';
import {DataFlowNode} from './dataflow';

Expand All @@ -19,19 +19,18 @@ export class CalculateNode extends DataFlowNode {
super(parent);
}

public static makeAllForSortIndex(parent: DataFlowNode, model: ModelWithField): CalculateNode[] {
public static parseAllForSortIndex(parent: DataFlowNode, model: ModelWithField) {
// get all the encoding with sort fields from model
const nodes = model.reduceFieldDef((acc: CalculateNode[], fieldDef: ChannelDef<any>, channel: Channel) => {
model.forEachFieldDef((fieldDef: ScaleFieldDef<string>, channel: SingleDefChannel) => {
if (isScaleFieldDef(fieldDef) && isSortArray(fieldDef.sort)) {
const transform: CalculateTransform = {
calculate: CalculateNode.calculateExpressionFromSortField(fieldDef.field, fieldDef.sort),
as: `${channel}_${fieldDef.field}_sort_index`
as: sortArrayIndexField(model, channel)
};
acc.push(new CalculateNode(parent, transform));
parent = new CalculateNode(parent, transform);
}
return acc;
}, [] as CalculateNode[]);
return nodes;
});
return parent;
}

public static calculateExpressionFromSortField(field: string, sortFields: string[]): string {
Expand All @@ -58,3 +57,8 @@ export class CalculateNode extends DataFlowNode {
};
}
}

export function sortArrayIndexField(model: ModelWithField, channel: SingleDefChannel) {
const fieldDef = model.fieldDef(channel);
return `${channel}_${vgField(fieldDef)}_sort_index`;
}
5 changes: 1 addition & 4 deletions src/compile/data/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,7 @@ export function parseData(model: Model): DataComponent {
}

head = TimeUnitNode.makeFromEncoding(head, model) || head;

for (const calculate of CalculateNode.makeAllForSortIndex(head, model)) {
head = calculate;
}
head = CalculateNode.parseAllForSortIndex(head, model);
}

// add an output node pre aggregation
Expand Down
3 changes: 2 additions & 1 deletion src/compile/scale/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
VgUnionSortField,
} from '../../vega.schema';
import {binRequiresRange} from '../common';
import {sortArrayIndexField} from '../data/calculate';
import {FACET_SCALE_PREFIX} from '../data/optimize';
import {isFacetModel, isUnitModel, Model} from '../model';
import {SELECTION_DOMAIN} from '../selection/selection';
Expand Down Expand Up @@ -271,7 +272,7 @@ export function domainSort(model: UnitModel, channel: ScaleChannel, scaleType: S
if (isSortArray(sort)) {
return {
op: 'min',
field: `${channel}_${model.vgField(channel)}_sort_index`,
field: sortArrayIndexField(model, channel),
order: 'ascending'
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/fielddef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export interface ScaleFieldDef<F> extends FieldDef<F> {

/**
* Sort order for the encoded field.
* Supported `sort` values include `"ascending"`, `"descending"`, `array specifying the preferred order of values` and `null` (no sorting).
* Supported `sort` values include `"ascending"`, `"descending"`, `null` (no sorting), or an array specifying the preferred order of values.
* For fields with discrete domains, `sort` can also be a [sort field definition object](https://vega.github.io/vega-lite/docs/sort.html#sort-field).
* For `sort` as an [array specifying the preferred order of values](https://vega.github.io/vega-lite/docs/sort.html#sort-array), the sort order will obey the values in the array, followed by any unspecified values in their original order.
*
Expand Down
7 changes: 4 additions & 3 deletions test/compile/data/calculate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {ModelWithField} from '../../../src/compile/model';
import {parseUnitModel} from '../../util';

function assembleFromSortArray(model: ModelWithField) {
return CalculateNode.makeAllForSortIndex(null, model).map(n => n.assemble());
const node = CalculateNode.parseAllForSortIndex(null, model) as CalculateNode;
return node.assemble();
}

describe('compile/data/calculate', function () {
Expand All @@ -24,11 +25,11 @@ describe('compile/data/calculate', function () {
}
});
const nodes = assembleFromSortArray(model);
assert.deepEqual(nodes, [{
assert.deepEqual(nodes, {
type: 'formula',
expr: "datum.a === 'B' ? 0 : datum.a === 'A' ? 1 : datum.a === 'C' ? 2 : 3",
as: 'x_a_sort_index'
}]);
});
});

describe('calculateExpressionFromSortField', function () {
Expand Down

0 comments on commit 5efd2c6

Please sign in to comment.