Skip to content

Commit

Permalink
fix(api): Fix JSON data loading
Browse files Browse the repository at this point in the history
Correct the condition on loading JSON according the type (Array or Object).

Fix #398
Close #400
  • Loading branch information
netil authored May 8, 2018
1 parent feeb536 commit 9067138
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 56 deletions.
105 changes: 77 additions & 28 deletions spec/api/api.load-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,24 @@ import CLASS from "../../src/config/classes";
import util from "../assets/util";

describe("API load", function() {
describe("indexed data as column", () => {
const chart = util.generate({
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
]
},
point: {
show: true
}
});
let chart;
let args = {
data: {
columns: [
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
]
},
point: {
show: true
}
};

beforeEach(() => {
chart = util.generate(args);
});

describe("indexed data as column", () => {
it("should load additional data", done => {
const main = chart.internal.main;
const legend = chart.internal.legend;
Expand All @@ -45,23 +50,25 @@ describe("API load", function() {
});

describe("category data", () => {
const chart = util.generate({
data: {
x: "x",
columns: [
["x", "cat1", "cat2", "cat3", "cat4", "cat5", "cat6"],
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
]
},
axis: {
x: {
type: "category"
before(() => {
args = {
data: {
x: "x",
columns: [
["x", "cat1", "cat2", "cat3", "cat4", "cat5", "cat6"],
["data1", 30, 200, 100, 400, 150, 250],
["data2", 5000, 2000, 1000, 4000, 1500, 2500]
]
},
axis: {
x: {
type: "category"
}
},
point: {
show: false
}
},
point: {
show: false
}
};
});

describe("as column", () => {
Expand Down Expand Up @@ -125,4 +132,46 @@ describe("API load", function() {
});
});
});

describe("JSON data", () => {
before(() => {
args.data = {
json: [
{name: "www.site1.com", upload: 200, download: 200},
{name: "www.site2.com", upload: 100, download: 300},
{name: "www.site3.com", upload: 300, download: 200},
{name: "www.site4.com", upload: 400, download: 100}
],
keys: {
x: "name",
value: ["upload", "download"]
}
};
});

it("should load json data", done => {
const json = [
{name: "www.site5.com", upload: 300, download: 100},
{name: "www.site6.com", upload: 400, download: 200},
{name: "www.site7.com", upload: 200, download: 400},
{name: "www.site8.com", upload: 100, download: 500}
];

chart.load({json});

setTimeout(() => {
const categories = chart.categories();
const upload = chart.data.values("upload");
const download = chart.data.values("download");

json.forEach((v, i) => {
expect(v.name).to.be.equal(categories[i]);
expect(v.upload).to.be.equal(upload[i]);
expect(v.download).to.be.equal(download[i]);
});

done();
}, 1000);
});
});
});
13 changes: 8 additions & 5 deletions src/data/data.convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ extend(ChartInternal.prototype, {
}, tsv);
},

convertJsonToData(json, keys) {
convertJsonToData(json, keysParam) {
const config = this.config;
const newRows = [];
let targetKeys;
let data;

if (keys) { // when keys specified, json would be an array that includes objects
if (isArray(json)) {
const keys = keysParam || config.data_keys;

if (keys.x) {
targetKeys = keys.value.concat(keys.x);
this.config.data_x = keys.x;
config.data_x = keys.x;
} else {
targetKeys = keys.value;
}
Expand All @@ -90,18 +93,18 @@ extend(ChartInternal.prototype, {

json.forEach(o => {
const newRow = [];
let v;

for (const key of targetKeys) {
// convert undefined to null because undefined data will be removed in convertDataToTargets()
v = this.findValueInJson(o, key);
let v = this.findValueInJson(o, key);

if (isUndefined(v)) {
v = null;
}

newRow.push(v);
}

newRows.push(newRow);
});

Expand Down
47 changes: 24 additions & 23 deletions src/internals/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ function removeEvent(element, type, handler) {
/**
* Return first letter capitalized
* @param {String} str
* @return {String} capitalized string
* @private
*/
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
Expand Down Expand Up @@ -265,33 +266,33 @@ const getCssRules = styleSheets => {
};

export {
isValue,
isDefined,
isUndefined,
isBoolean,
isString,
isNumber,
addEvent,
asHalfPixel,
brushEmpty,
capitalize,
ceil10,
diffDomain,
extend,
getBrushSelection,
getCssRules,
getOption,
getPathBox,
getRectSegList,
hasValue,
isArray,
isBoolean,
isDefined,
isEmpty,
isFunction,
isNumber,
isObject,
isObjectType,
isString,
isUndefined,
isValue,
notEmpty,
ceil10,
isFunction,
asHalfPixel,
getOption,
hasValue,
sanitise,
getPathBox,
diffDomain,
getBrushSelection,
brushEmpty,
extend,
addEvent,
removeEvent,
getRectSegList,
merge,
capitalize,
toArray,
getCssRules
removeEvent,
sanitise,
toArray
};

0 comments on commit 9067138

Please sign in to comment.