Browse Source

Refactor data APIs and add spec

pull/676/head
Masayuki Tanaka 10 years ago
parent
commit
ad3a3a6416
  1. 33
      c3.js
  2. 7
      c3.min.js
  3. 58
      spec/api.data-spec.js
  4. 24
      src/api.data.js
  5. 9
      src/data.js

33
c3.js

@ -1768,6 +1768,15 @@
return converted;
};
c3_chart_internal_fn.updateDataAttributes = function (name, attrs) {
var $$ = this, config = $$.config, current = config['data_' + name];
if (typeof attrs === 'undefined') { return current; }
Object.keys(attrs).forEach(function (id) {
current[id] = attrs[id];
});
$$.redraw({withLegend: true});
return current;
};
c3_chart_internal_fn.convertUrlToData = function (url, mimeType, keys, done) {
var $$ = this, type = mimeType ? mimeType : 'csv';
@ -6119,31 +6128,13 @@
return target ? target.values.map(function (d) { return d.value; }) : null;
};
c3_chart_fn.data.names = function (names) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_names; }
Object.keys(names).forEach(function (id) {
config.data_names[id] = names[id];
});
$$.redraw({withLegend: true});
return config.data_names;
return this.internal.updateDataAttributes('names', names);
};
c3_chart_fn.data.colors = function (colors) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_colors; }
Object.keys(colors).forEach(function (id) {
config.data_colors[id] = colors[id];
});
$$.redraw({withLegend: true});
return config.data_colors;
return this.internal.updateDataAttributes('colors', colors);
};
c3_chart_fn.data.axes = function (axes) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_axes; }
Object.keys(axes).forEach(function (id) {
config.data_axes[id] = axes[id];
});
$$.redraw({withLegend: true});
return config.data_axes;
return this.internal.updateDataAttributes('axes', axes);
};
c3_chart_fn.category = function (i, category) {

7
c3.min.js vendored

File diff suppressed because one or more lines are too long

58
spec/api.data-spec.js

@ -14,6 +14,14 @@ describe('c3 api data', function () {
['data1', 30, 200, 100, 400, 150, 250],
['data2', 5000, 2000, 1000, 4000, 1500, 2500]
],
names: {
data1: 'Data Name 1',
data2: 'Data Name 2'
},
colors: {
data1: '#FF0000',
data2: '#00FF00'
},
axes: {
data1: 'y',
data2: 'y2'
@ -78,6 +86,56 @@ describe('c3 api data', function () {
});
describe('data.names()', function () {
it('should return data.names specified as argument', function () {
var results = chart.data.names();
expect(results.data1).toBe('Data Name 1');
expect(results.data2).toBe('Data Name 2');
});
it('should return data.names specified as api', function () {
var results = chart.data.names({
data1: 'New Data Name 1',
data2: 'New Data Name 2'
});
expect(results.data1).toBe('New Data Name 1');
expect(results.data2).toBe('New Data Name 2');
});
it('should set data.names specified as api', function () {
expect(d3.select('.c3-legend-item-data1 text').text()).toBe("New Data Name 1");
expect(d3.select('.c3-legend-item-data2 text').text()).toBe("New Data Name 2");
});
});
describe('data.colors()', function () {
it('should return data.colors specified as argument', function () {
var results = chart.data.colors();
expect(results.data1).toBe('#FF0000');
expect(results.data2).toBe('#00FF00');
});
it('should return data.colors specified as api', function () {
var results = chart.data.colors({
data1: '#00FF00',
data2: '#FF0000'
});
expect(results.data1).toBe('#00FF00');
expect(results.data2).toBe('#FF0000');
});
it('should set data.colors specified as api', function () {
expect(d3.select('.c3-line-data1').style('stroke')).toBe("#00ff00");
expect(d3.select('.c3-line-data2').style('stroke')).toBe("#ff0000");
expect(d3.select('.c3-legend-item-data1 .c3-legend-item-tile').style('fill')).toBe("#00ff00");
expect(d3.select('.c3-legend-item-data2 .c3-legend-item-tile').style('fill')).toBe("#ff0000");
});
});
describe('data.axes()', function () {
it('should return data.axes specified as argument', function () {

24
src/api.data.js

@ -12,29 +12,11 @@ c3_chart_fn.data.values = function (targetId) {
return target ? target.values.map(function (d) { return d.value; }) : null;
};
c3_chart_fn.data.names = function (names) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_names; }
Object.keys(names).forEach(function (id) {
config.data_names[id] = names[id];
});
$$.redraw({withLegend: true});
return config.data_names;
return this.internal.updateDataAttributes('names', names);
};
c3_chart_fn.data.colors = function (colors) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_colors; }
Object.keys(colors).forEach(function (id) {
config.data_colors[id] = colors[id];
});
$$.redraw({withLegend: true});
return config.data_colors;
return this.internal.updateDataAttributes('colors', colors);
};
c3_chart_fn.data.axes = function (axes) {
var $$ = this.internal, config = $$.config;
if (!arguments.length) { return config.data_axes; }
Object.keys(axes).forEach(function (id) {
config.data_axes[id] = axes[id];
});
$$.redraw({withLegend: true});
return config.data_axes;
return this.internal.updateDataAttributes('axes', axes);
};

9
src/data.js

@ -337,3 +337,12 @@ c3_chart_internal_fn.convertValuesToStep = function (values) {
return converted;
};
c3_chart_internal_fn.updateDataAttributes = function (name, attrs) {
var $$ = this, config = $$.config, current = config['data_' + name];
if (typeof attrs === 'undefined') { return current; }
Object.keys(attrs).forEach(function (id) {
current[id] = attrs[id];
});
$$.redraw({withLegend: true});
return current;
};

Loading…
Cancel
Save