Browse Source

Fix for issue http://github.com/c3js/c3/issues/1691

If an object has key that contains . or [], it is now correctly found
instead of searching for an inner object or array that does not exist.
Example:
var data = [{"key.withdot":"1"}]; // did not work before, works now
var data2 = [{"key":{"innerkey":"1"}}]; // still works

Updated test with new bugfix.
pull/1694/head
Jakob Kneissl 9 years ago
parent
commit
e34fb48021
  1. 16
      c3.js
  2. 12
      c3.min.js
  3. 30
      spec/data-spec.js
  4. 8
      src/data.convert.js

16
c3.js

@ -1188,7 +1188,7 @@
axis_y_label: {},
axis_y_tick_format: undefined,
axis_y_tick_outer: true,
axis_y_tick_values: null,
axis_y_tick_values: null,
axis_y_tick_rotate: 0,
axis_y_tick_count: undefined,
axis_y_tick_time_value: undefined,
@ -2100,9 +2100,17 @@
return data;
};
c3_chart_internal_fn.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
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('.');
// search for any inner objects or arrays denoted by the path
for (var i = 0; i < pathArray.length; ++i) {
var k = pathArray[i];
if (k in object) {
@ -2694,7 +2702,7 @@
c3_chart_internal_fn.getCurrentHeight = function () {
var $$ = this, config = $$.config,
h = config.size_height ? config.size_height : $$.getParentHeight();
return h > 0 ? h : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1);
return h > 0 ? h : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1);
};
c3_chart_internal_fn.getCurrentPaddingTop = function () {
var $$ = this,
@ -2784,8 +2792,8 @@
var $$ = this, config = $$.config, h = 30;
if (axisId === 'x' && !config.axis_x_show) { return 8; }
if (axisId === 'x' && config.axis_x_height) { return config.axis_x_height; }
if (axisId === 'y' && !config.axis_y_show) {
return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1;
if (axisId === 'y' && !config.axis_y_show) {
return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1;
}
if (axisId === 'y2' && !config.axis_y2_show) { return $$.rotated_padding_top; }
// Calculate x axis height when tick rotated

12
c3.min.js vendored

File diff suppressed because one or more lines are too long

30
spec/data-spec.js

@ -85,13 +85,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"},
@ -99,11 +101,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: {
@ -118,12 +121,13 @@ describe('c3 chart data', function () {
it('should draw nested JSON correctly', function () {
var expectedCx = [98, 294, 490],
expectedCy = {
443: [181, 326, 36],
995: [362, 398, 326],
112: [354, 347, 340],
223: [391, 383, 376],
334: [376, 398, 362],
556: [326, 253, 181]
"443": [181, 326, 36],
"995": [362, 398, 326],
"112": [354, 347, 340],
"223": [391, 383, 376],
"334": [376, 398, 362],
"556": [326, 253, 181],
"778.889": [347, 376, 340]
};
d3.selectAll('.c3-circles-443 .c3-circle').each(function (d, i) {
@ -161,6 +165,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);
});
});
});

8
src/data.convert.js

@ -72,9 +72,17 @@ c3_chart_internal_fn.convertJsonToData = function (json, keys) {
return data;
};
c3_chart_internal_fn.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
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('.');
// search for any inner objects or arrays denoted by the path
for (var i = 0; i < pathArray.length; ++i) {
var k = pathArray[i];
if (k in object) {

Loading…
Cancel
Save