Commit ad3a3a64 authored by Masayuki Tanaka's avatar Masayuki Tanaka

Refactor data APIs and add spec

parent 937f3835
......@@ -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) {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -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 () {
......
......@@ -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);
};
......@@ -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;
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment