Skip to content

Commit

Permalink
fix: fix #1691 (#2761)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakob Kneissl <j_the_k@web.de>
  • Loading branch information
kt3k and kelthuzad authored Feb 26, 2020
1 parent ed0e523 commit 49b7975
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
20 changes: 15 additions & 5 deletions spec/data-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,28 @@ describe('c3 chart data', function () {
"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 @@ -139,7 +142,8 @@ describe('c3 chart data', function () {
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]
};

d3.selectAll('.c3-circles-443 .c3-circle').each(function (d, i) {
Expand Down Expand Up @@ -177,6 +181,12 @@ describe('c3 chart data', function () {
expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
expect(+circle.attr('cy')).toBeCloseTo(expectedCy[556][i], 0);
});

d3.selectAll('.c3-circles-778-889 .c3-circle').each(function (d, i) {
var circle = d3.select(this);
expect(+circle.attr('cx')).toBeCloseTo(expectedCx[i], 0);
expect(+circle.attr('cy')).toBeCloseTo(expectedCy["778.889"][i], 0);
});
});
});
});
Expand Down
13 changes: 13 additions & 0 deletions src/data.convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,20 @@ ChartInternal.prototype.convertJsonToData = function (json, keys) {
}
return data;
};
/**
* Finds value from the given nested object by the given path.
* If it's not found, then this returns undefined.
* @param {Object} object the object
* @param {string} path the path
*/
ChartInternal.prototype.findValueInJson = function (object, path) {
if (path in object) {
// If object has a key that contains . or [], return the key's value
// instead of searching for an inner object.
// See https://github.com/c3js/c3/issues/1691 for details.
return object[path];
}

path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties (replace [] with .)
path = path.replace(/^\./, ''); // strip a leading dot
var pathArray = path.split('.');
Expand Down

0 comments on commit 49b7975

Please sign in to comment.