From 9e1bf811a03455d5b718e329559a91870da9e0d2 Mon Sep 17 00:00:00 2001 From: sirahd Date: Tue, 20 Mar 2018 23:37:55 -0700 Subject: [PATCH] remove iterating in parse and instead do for loop in each node, create export function for sort index field name --- examples/compiled/bar_custom_sort_partial.svg | 2 +- .../compiled/bar_custom_sort_partial.vg.json | 6 ++--- .../specs/bar_custom_sort_partial.vl.json | 2 +- src/compile/data/calculate.ts | 23 +++++++++++-------- src/compile/data/parse.ts | 5 +--- src/compile/scale/domain.ts | 3 ++- src/fielddef.ts | 2 +- test/compile/data/calculate.test.ts | 7 +++--- 8 files changed, 27 insertions(+), 23 deletions(-) diff --git a/examples/compiled/bar_custom_sort_partial.svg b/examples/compiled/bar_custom_sort_partial.svg index 0b6543495f9..62f4e0e4e1e 100644 --- a/examples/compiled/bar_custom_sort_partial.svg +++ b/examples/compiled/bar_custom_sort_partial.svg @@ -1 +1 @@ -BACDEFa020406080100b \ No newline at end of file +BACZYXa020406080100b \ No newline at end of file diff --git a/examples/compiled/bar_custom_sort_partial.vg.json b/examples/compiled/bar_custom_sort_partial.vg.json index 365735b84d5..5913fb897c0 100644 --- a/examples/compiled/bar_custom_sort_partial.vg.json +++ b/examples/compiled/bar_custom_sort_partial.vg.json @@ -22,15 +22,15 @@ "b": 43 }, { - "a": "D", + "a": "Z", "b": 91 }, { - "a": "E", + "a": "Y", "b": 81 }, { - "a": "F", + "a": "X", "b": 53 } ] diff --git a/examples/specs/bar_custom_sort_partial.vl.json b/examples/specs/bar_custom_sort_partial.vl.json index 2d39c527027..78c322468d9 100644 --- a/examples/specs/bar_custom_sort_partial.vl.json +++ b/examples/specs/bar_custom_sort_partial.vl.json @@ -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", diff --git a/src/compile/data/calculate.ts b/src/compile/data/calculate.ts index 317eb7b0116..06a060994ba 100644 --- a/src/compile/data/calculate.ts +++ b/src/compile/data/calculate.ts @@ -1,9 +1,10 @@ -import {ChannelDef, isScaleFieldDef} from '../../fielddef'; +import {isScaleFieldDef, 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 {ScaleFieldDef} from './../../../_site/build/src/fielddef.d'; +import {SingleDefChannel} from './../../channel'; import {CalculateTransform} from './../../transform'; import {DataFlowNode} from './dataflow'; @@ -19,19 +20,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, channel: Channel) => { + model.forEachFieldDef((fieldDef: ScaleFieldDef, 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 { @@ -58,3 +58,8 @@ export class CalculateNode extends DataFlowNode { }; } } + +export function sortArrayIndexField(model: ModelWithField, channel: SingleDefChannel) { + const fieldDef = model.fieldDef(channel); + return `${channel}_${vgField(fieldDef)}_sort_index`; +} diff --git a/src/compile/data/parse.ts b/src/compile/data/parse.ts index 090b5ac9460..4c15bf95dc6 100644 --- a/src/compile/data/parse.ts +++ b/src/compile/data/parse.ts @@ -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 diff --git a/src/compile/scale/domain.ts b/src/compile/scale/domain.ts index aa0d053c755..7eeaeb97623 100644 --- a/src/compile/scale/domain.ts +++ b/src/compile/scale/domain.ts @@ -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'; @@ -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' }; } diff --git a/src/fielddef.ts b/src/fielddef.ts index 1ffa9813a86..d3e02b396e1 100644 --- a/src/fielddef.ts +++ b/src/fielddef.ts @@ -182,7 +182,7 @@ export interface ScaleFieldDef extends FieldDef { /** * 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. * diff --git a/test/compile/data/calculate.test.ts b/test/compile/data/calculate.test.ts index 643b147e066..33578910f03 100644 --- a/test/compile/data/calculate.test.ts +++ b/test/compile/data/calculate.test.ts @@ -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 () { @@ -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 () {