diff --git a/spec/data-spec.js b/spec/data-spec.js index 31b9e1ded..526a70ee0 100644 --- a/spec/data-spec.js +++ b/spec/data-spec.js @@ -102,13 +102,15 @@ 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"}, @@ -116,11 +118,12 @@ describe('c3 chart data', function () { "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: { @@ -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) { @@ -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); + }); }); }); }); diff --git a/src/data.convert.js b/src/data.convert.js index fcf170d4e..e20cf3996 100644 --- a/src/data.convert.js +++ b/src/data.convert.js @@ -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('.');