Browse Source

Add data.shown API and modify data API interface - #626

pull/676/head
Masayuki Tanaka 10 years ago
parent
commit
6433f4a132
  1. 11
      c3.js
  2. 2
      c3.min.js
  3. 72
      spec/api.data-spec.js
  4. 11
      src/api.data.js

11
c3.js

@ -6105,9 +6105,14 @@
return config.regions;
};
c3_chart_fn.data = function (targetId) {
var targets = this.internal.data.targets.filter(function (t) { return t.id === targetId; });
return targets.length > 0 ? targets[0] : null;
c3_chart_fn.data = function (targetIds) {
var targets = this.internal.data.targets;
return typeof targetIds === 'undefined' ? targets : targets.filter(function (t) {
return [].concat(targetIds).indexOf(t.id) >= 0;
});
};
c3_chart_fn.data.shown = function (targetId) {
return this.internal.filterTargetsToShow(this.data(targetId));
};
c3_chart_fn.data.values = function (targetId) {
var target = this.data(targetId);

2
c3.min.js vendored

File diff suppressed because one or more lines are too long

72
spec/api.data-spec.js

@ -0,0 +1,72 @@
var describe = window.describe,
expect = window.expect,
it = window.it,
beforeEach = window.beforeEach;
describe('c3 api data', function () {
'use strict';
var chart, d3;
var args = {
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
]
}
};
beforeEach(function (done) {
if (typeof chart === 'undefined') {
window.initDom();
}
chart = window.c3.generate(args);
d3 = chart.internal.d3;
chart.internal.d3.select('.jasmine_html-reporter')
.style('position', 'absolute')
.style('right', 0);
window.setTimeout(function () {
done();
}, 10);
});
describe('data()', function () {
it('should return all of data if no argument given', function () {
var results = chart.data(),
expected = ['data1', 'data2'];
results.forEach(function (result, i) {
expect(result.id).toBe(expected[i]);
});
});
it('should return specifid data if string argument given', function () {
var results = chart.data('data1');
expect(results.length).toBe(1);
expect(results[0].id).toBe('data1');
});
it('should return specifid data if array argument given', function () {
var results = chart.data(['data1', 'data2']);
expect(results.length).toBe(2);
expect(results[0].id).toBe('data1');
expect(results[1].id).toBe('data2');
});
});
describe('data.shown()', function () {
it('should return only shown targets', function () {
var results;
chart.hide('data1');
results = chart.data.shown();
expect(results.length).toBe(1);
expect(results[0].id).toBe('data2');
});
});
});

11
src/api.data.js

@ -1,6 +1,11 @@
c3_chart_fn.data = function (targetId) {
var targets = this.internal.data.targets.filter(function (t) { return t.id === targetId; });
return targets.length > 0 ? targets[0] : null;
c3_chart_fn.data = function (targetIds) {
var targets = this.internal.data.targets;
return typeof targetIds === 'undefined' ? targets : targets.filter(function (t) {
return [].concat(targetIds).indexOf(t.id) >= 0;
});
};
c3_chart_fn.data.shown = function (targetId) {
return this.internal.filterTargetsToShow(this.data(targetId));
};
c3_chart_fn.data.values = function (targetId) {
var target = this.data(targetId);

Loading…
Cancel
Save