Skip to content

Commit

Permalink
Merge pull request #417 from Kitware/fix-vega-dates
Browse files Browse the repository at this point in the history
Fix date handling by using read() instead of inferAll() in axis logic
  • Loading branch information
jeffbaumes committed Sep 29, 2016
2 parents d2a5882 + 1e1ae8f commit 39db1d2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/candela/components/UpSet/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import d3 from 'd3';
import { unique } from 'datalib';
import VisComponent from '../../VisComponent';
import { inferAll } from '../../util';
import { read } from '../../util';
import * as upset from 'UpSet';
import template from './template.html';

Expand Down Expand Up @@ -124,7 +124,7 @@ export default class UpSet extends VisComponent {
// Add metadata fields.
if (this.options.metadata) {
if (!this.options.data.__types__) {
this.options.data.__types__ = inferAll(this.options.data);
read(this.options.data);
}
const upsetTypeMap = {
string: 'string',
Expand Down
11 changes: 10 additions & 1 deletion src/candela/util/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { keys, type } from 'datalib';
import { keys, type, read as dlread } from 'datalib';

export function getElementSize (el) {
const style = window.getComputedStyle(el, null);
Expand Down Expand Up @@ -34,6 +34,8 @@ export function minmax (data) {
return range;
}

// Version of datalib.type.inferAll() that handles fields
// with nested periods
export function inferAll (data) {
let fields = keys(data[0]);
let types = {};
Expand All @@ -42,3 +44,10 @@ export function inferAll (data) {
}
return types;
}

// Version of datalib.read() that uses our inferAll() to handle
// fields with nested periods
export function read (data) {
data.__types__ = inferAll(data);
dlread(data, {parse: data.__types__});
}
21 changes: 17 additions & 4 deletions src/candela/util/test/util.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import test from 'tape-catch';
import { inferAll } from '..';
import { read } from 'datalib';
import { inferAll, read } from '..';
import dl from 'datalib';

test('inferAll()', t => {
t.deepEqual(inferAll([{'a.b': 1}]), {'a.b': 'integer'}, 'should work on fields with nested dots');
t.deepEqual(inferAll([{'a': {'b': 1}}]), {'a': 'string'}, 'should only accept top-level fields');

let testData = [{a: 1, b: 'foo', c: 1.5}];
read(testData, {parse: 'auto'});
t.deepEqual(testData.__types__, inferAll(testData), 'should work identically to datalib read() when no nested dots');
t.deepEqual(dl.type.inferAll(testData), inferAll(testData), 'should work identically to datalib inferAll() when no nested dots');

t.end();
});

test('read()', t => {
let testData = [{'a.b': 1}];
read(testData);
t.deepEqual({'a.b': 'integer'}, testData.__types__, 'should work on fields with nested dots');

let testData1 = [{a: 1, b: 'foo', c: 1.5}];
let testData2 = [{a: 1, b: 'foo', c: 1.5}];
dl.read(testData1, {parse: 'auto'});
read(testData2);
t.deepEqual(testData1.__types__, testData2.__types__, 'should work identically to datalib read() when no nested dots');

t.end();
});
4 changes: 2 additions & 2 deletions src/candela/util/vega/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import d3 from 'd3';
import vg from 'vega';
import { isArray, isString } from 'datalib';
import axisTemplate from './axis.json';
import { getElementSize, inferAll } from '..';
import { getElementSize, read } from '..';

let getNestedRec = function (spec, parts) {
if (spec === undefined || parts.length === 0) {
Expand Down Expand Up @@ -214,7 +214,7 @@ let templateFunctions = {
let opt = transform(args[0], options, scope);
if (opt.data && Array.isArray(opt.data) && opt.field) {
if (!opt.data.__types__) {
opt.data.__types__ = inferAll(opt.data);
read(opt.data);
}
let type = opt.data.__types__[opt.field];
if (type === 'string') {
Expand Down

0 comments on commit 39db1d2

Please sign in to comment.