Skip to content

Commit

Permalink
fix(data.convert): allow dots in json properties
Browse files Browse the repository at this point in the history
Current function findValueInJson does not allow dots in properties as it always processes them as path for inner objects.
Fix checks first if property exists in json object and only then processes the name as path

Close #14
  • Loading branch information
rattuscz authored and netil committed Jun 15, 2017
1 parent 318d140 commit 7ff837a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
21 changes: 16 additions & 5 deletions spec/data-spec.js
Expand Up @@ -102,25 +102,28 @@ describe("Data", () => {
"112": ["600"],
"223": [{"224": "100"}],
"334": [[], [{"335": "300"}]],
"556": {"557": {"558": ["1000"]}}
"556": {"557": {"558": ["1000"]}},
"778.889" : "700"
}, {
"date": "2014-06-04",
"443": "1000",
"112": ["700"],
"223": [{"224": "200"}],
"556": {"557": {"558": ["2000"]}}
"556": {"557": {"558": ["2000"]}},
"778.889" : "300"
}, {
"date": "2014-06-05",
"995": {"996": "1000"},
"112": ["800"],
"223": [{"224": "300"}],
"443": "5000",
"334": [[], [{"335": "500"}]],
"556": {"557": {"558": ["3000"]}}
"556": {"557": {"558": ["3000"]}},
"778.889" : "800"
}],
keys: {
x: "date",
value: ["443", "995.996", "112[0]", "223[0].224", "334[1][0].335", "556.557.558[0]"]
value: ["443", "995.996", "112[0]", "223[0].224", "334[1][0].335", "556.557.558[0]", "778.889"]
}
},
axis: {
Expand All @@ -140,7 +143,8 @@ describe("Data", () => {
112: [354, 347, 340],
223: [391, 383, 376],
334: [376, 398, 362],
556: [326, 253, 181]
556: [326, 253, 181],
"778.889": [347, 376, 340]
};

d3SelectAll(".bb-circles-443 .bb-circle").each(function(d, i) {
Expand Down Expand Up @@ -185,6 +189,13 @@ describe("Data", () => {
expect(+circle.attr("cx")).to.be.closeTo(expectedCx[i], 0);
expect(+circle.attr("cy")).to.be.closeTo(expectedCy[556][i], 1);
});

d3SelectAll(".bb-circles-778-889 .bb-circle").each(function (d, i) {
const circle = d3Select(this);

expect(+circle.attr("cx")).to.be.closeTo(expectedCx[i], 0);
expect(+circle.attr("cy")).to.be.closeTo(expectedCy["778.889"][i], 1);
});
});
});

Expand Down
4 changes: 4 additions & 0 deletions src/data/data.convert.js
Expand Up @@ -115,6 +115,10 @@ extend(ChartInternal.prototype, {
},

findValueInJson(object, path) {
if (object[path] !== undefined) {
return object[path];
}

const convertedPath = path.replace(/\[(\w+)\]/g, ".$1"); // convert indexes to properties (replace [] with .)
const pathArray = convertedPath.replace(/^\./, "").split("."); // strip a leading dot
let target = object;
Expand Down

0 comments on commit 7ff837a

Please sign in to comment.