Browse Source

Pie and donuts really handle data_order as advertised by documentation. (#1814)

pull/2152/head
Anthony Pessy 7 years ago committed by Yoshiya Hinosawa
parent
commit
64a134cc44
  1. 76
      spec/arc-spec.js
  2. 6
      src/arc.js
  3. 26
      src/data.js

76
spec/arc-spec.js

@ -97,6 +97,82 @@ describe('c3 chart arc', function () {
});
});
describe('sort pie chart', function() {
var createPie = function(order) {
return {
data: {
order: order,
columns: [
['data1', 30],
['data2', 150],
['data3', 120]
],
type: 'pie'
}
};
};
var collectArcs = function() {
return d3.selectAll('.c3-arc')
.data()
.sort(function(a, b) {
return a.startAngle - b.startAngle;
})
.map(function(item) {
return item.data.id;
});
};
it('should update data_order to desc', function () {
args = createPie('desc');
expect(true).toBeTruthy();
});
it('it should have descending ordering', function () {
expect(collectArcs()).toEqual([ 'data2', 'data3', 'data1' ]);
});
it('should update data_order to asc', function () {
args = createPie('asc');
expect(true).toBeTruthy();
});
it('it should have ascending ordering', function () {
expect(collectArcs()).toEqual([ 'data1', 'data3', 'data2' ]);
});
it('should update data_order to NULL', function () {
args = createPie(null);
expect(true).toBeTruthy();
});
it('it should have no ordering', function () {
expect(collectArcs()).toEqual([ 'data1', 'data2', 'data3' ]);
});
it('should update data_order to Array', function () {
args = createPie([ 'data3', 'data2', 'data1' ]);
expect(true).toBeTruthy();
});
it('it should have array specified ordering', function () {
expect(collectArcs()).toEqual([ 'data3', 'data2', 'data1' ]);
});
it('should update data_order to Function', function () {
var names = [ 'data2', 'data1', 'data3' ];
args = createPie(function(a, b) {
return names.indexOf(a.id) - names.indexOf(b.id);
});
expect(true).toBeTruthy();
});
it('it should have array specified ordering', function () {
expect(collectArcs()).toEqual([ 'data2', 'data1', 'data3' ]);
});
});
describe('show gauge', function () {
describe('with a 180 degree gauge', function(){

6
src/arc.js

@ -3,13 +3,11 @@ import { c3_chart_internal_fn } from './core';
import { isFunction } from './util';
c3_chart_internal_fn.initPie = function () {
var $$ = this, d3 = $$.d3, config = $$.config;
var $$ = this, d3 = $$.d3;
$$.pie = d3.layout.pie().value(function (d) {
return d.values.reduce(function (a, b) { return a + b.value; }, 0);
});
if (!config.data_order) {
$$.pie.sort(null);
}
$$.pie.sort($$.getOrderFunction() || null);
};
c3_chart_internal_fn.updateRadius = function () {

26
src/data.js

@ -236,21 +236,31 @@ c3_chart_internal_fn.isOrderAsc = function () {
var config = this.config;
return typeof(config.data_order) === 'string' && config.data_order.toLowerCase() === 'asc';
};
c3_chart_internal_fn.orderTargets = function (targets) {
c3_chart_internal_fn.getOrderFunction = function() {
var $$ = this, config = $$.config, orderAsc = $$.isOrderAsc(), orderDesc = $$.isOrderDesc();
if (orderAsc || orderDesc) {
targets.sort(function (t1, t2) {
return function (t1, t2) {
var reducer = function (p, c) { return p + Math.abs(c.value); };
var t1Sum = t1.values.reduce(reducer, 0),
t2Sum = t2.values.reduce(reducer, 0);
return orderAsc ? t2Sum - t1Sum : t1Sum - t2Sum;
});
return orderDesc ? t2Sum - t1Sum : t1Sum - t2Sum;
};
} else if (isFunction(config.data_order)) {
targets.sort(config.data_order);
return config.data_order;
} else if (isArray(config.data_order)) {
targets.sort(function (t1, t2) {
return config.data_order.indexOf(t1.id) - config.data_order.indexOf(t2.id);
});
var order = config.data_order;
return function (t1, t2) {
return order.indexOf(t1.id) - order.indexOf(t2.id);
};
}
};
c3_chart_internal_fn.orderTargets = function (targets) {
var fct = this.getOrderFunction();
if (fct) {
targets.sort(fct);
if (this.isOrderAsc() || this.isOrderDesc()) {
targets.reverse();
}
}
return targets;
};

Loading…
Cancel
Save