Skip to content

Commit

Permalink
Add padAngle option for pie/donut (#2669)
Browse files Browse the repository at this point in the history
* add pie.padAngle and donut.padAngle options

* add API to get/set padAngle for both pie and donut

* add pie.padAngle and donut.padAngle in reference doc

* add padAngle option in donut sample

* bind getPadAngle method to self

* use chart main instead of global D3 to select chart's elements
  • Loading branch information
panthony authored and kt3k committed Aug 9, 2019
1 parent 9ae4457 commit 20dff29
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 2 deletions.
32 changes: 32 additions & 0 deletions docs/reference.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@
= partial :reference_menu_item, locals: { id: 'pie.label.format' }
= partial :reference_menu_item, locals: { id: 'pie.label.threshold' }
= partial :reference_menu_item, locals: { id: 'pie.expand' }
= partial :reference_menu_item, locals: { id: 'pie.padAngle' }

%li Donut
= partial :reference_menu_item, locals: { id: 'donut.label.show' }
= partial :reference_menu_item, locals: { id: 'donut.label.format' }
= partial :reference_menu_item, locals: { id: 'donut.label.threshold' }
= partial :reference_menu_item, locals: { id: 'donut.expand' }
= partial :reference_menu_item, locals: { id: 'donut.padAngle' }
= partial :reference_menu_item, locals: { id: 'donut.width' }
= partial :reference_menu_item, locals: { id: 'donut.title' }

Expand Down Expand Up @@ -3335,6 +3337,21 @@
}
%hr

%section
%h3
= partial :reference_item_link, locals: { id: 'pie.padAngle' }
%p Sets the angular separation between each adjacent arc.
%h5 Default:
<code>0</code>
%h5 Format:
%div.sourcecode
%pre
%code.html.javascript
pie: {
&nbsp;&nbsp;padAngle: .1
}
%hr

%section
%h3
= partial :reference_item_link, locals: { id: 'donut.label.show' }
Expand Down Expand Up @@ -3403,6 +3420,21 @@
}
%hr

%section
%h3
= partial :reference_item_link, locals: { id: 'donut.padAngle' }
%p Sets the angular separation between each adjacent arc.
%h5 Default:
<code>0</code>
%h5 Format:
%div.sourcecode
%pre
%code.html.javascript
donut: {
&nbsp;&nbsp;padAngle: .1
}
%hr

%section
%h3
= partial :reference_item_link, locals: { id: 'donut.width' }
Expand Down
3 changes: 2 additions & 1 deletion htdocs/samples/chart_donut.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
// format: function (d, ratio) { return ""; }
},
title: "Iris Petal Width",
width: 70
width: 70,
padAngle: .1
}
});

Expand Down
37 changes: 37 additions & 0 deletions spec/api.donut-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('c3 api donut', function () {
'use strict';

var chart, args;

args = {
data: {
columns: [
['data1', 60],
['data2', 40]
],
type: 'donut'
},
donut: {
padAngle: 0.5
}
};

beforeAll(function (done) {
chart = window.initChart(chart, args, done);
});

it('can configure padAngle', function (done) {
expect(chart.donut.padAngle()).toBe(0.5);

const path = chart.internal.main.select('.c3-arc-data1').attr('d');

chart.donut.padAngle(0.2);

setTimeout(function () {
expect(chart.donut.padAngle()).toBe(0.2);
expect(chart.internal.main.select('.c3-arc-data1').attr('d')).not.toBe(path);
done();
}, 500);
});

});
37 changes: 37 additions & 0 deletions spec/api.pie-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe('c3 api pie', function () {
'use strict';

var chart, args;

args = {
data: {
columns: [
['data1', 60],
['data2', 40]
],
type: 'pie'
},
pie: {
padAngle: 0.5
}
};

beforeAll(function (done) {
chart = window.initChart(chart, args, done);
});

it('can configure padAngle', function (done) {
expect(chart.pie.padAngle()).toBe(0.5);

const path = chart.internal.main.select('.c3-arc-data1').attr('d');

chart.pie.padAngle(0.2);

setTimeout(function () {
expect(chart.pie.padAngle()).toBe(0.2);
expect(chart.internal.main.select('.c3-arc-data1').attr('d')).not.toBe(path);
done();
}, 500);
});

});
44 changes: 44 additions & 0 deletions spec/arc-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,50 @@ describe('c3 chart arc', function () {
});
});

describe('config donut chart', function() {
beforeAll(function () {
args = {
data: {
columns: [
['data1', 30],
['data2', 150],
['data3', 120]
],
type: 'donut'
},
donut: {
padAngle: 0.05
}
};
});

it('can configure padAngle', function () {
expect(chart.internal.pie.padAngle()()).toBe(0.05);
});
});

describe('config pie chart', function() {
beforeAll(function () {
args = {
data: {
columns: [
['data1', 30],
['data2', 150],
['data3', 120]
],
type: 'pie'
},
pie: {
padAngle: 0.05
}
};
});

it('can configure padAngle', function () {
expect(chart.internal.pie.padAngle()()).toBe(0.05);
});
});

describe('show gauge', function () {

describe('with a 180 degree gauge', function(){
Expand Down
11 changes: 11 additions & 0 deletions src/api.donut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Chart } from './core';

Chart.prototype.donut = function() {};

Chart.prototype.donut.padAngle = function (padAngle) {
if (padAngle === undefined) {
return this.internal.config.donut_padAngle;
}
this.internal.config.donut_padAngle = padAngle;
this.flush();
};
11 changes: 11 additions & 0 deletions src/api.pie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Chart } from './core';

Chart.prototype.pie = function() {};

Chart.prototype.pie.padAngle = function (padAngle) {
if (padAngle === undefined) {
return this.internal.config.pie_padAngle;
}
this.internal.config.pie_padAngle = padAngle;
this.flush();
};
12 changes: 11 additions & 1 deletion src/arc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isFunction } from './util';

ChartInternal.prototype.initPie = function () {
var $$ = this, d3 = $$.d3;
$$.pie = d3.pie().value(function (d) {
$$.pie = d3.pie().padAngle(this.getPadAngle.bind(this)).value(function (d) {
return d.values.reduce(function (a, b) { return a + b.value; }, 0);
});

Expand All @@ -30,6 +30,16 @@ ChartInternal.prototype.updateRadius = function () {
$$.gaugeArcWidth = w ? w : (gaugeArcWidth <= $$.radius - $$.innerRadius ? $$.radius - $$.innerRadius : (gaugeArcWidth <= $$.radius ? gaugeArcWidth : $$.radius));
};

ChartInternal.prototype.getPadAngle = function() {
if (this.hasType('pie')) {
return this.config.pie_padAngle || 0;
} else if (this.hasType('donut')) {
return this.config.donut_padAngle || 0;
} else {
return 0;
}
};

ChartInternal.prototype.updateArc = function () {
var $$ = this;
$$.svgArc = $$.getSvgArc();
Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ ChartInternal.prototype.getDefaultConfig = function () {
pie_label_ratio: undefined,
pie_expand: {},
pie_expand_duration: 50,
pie_padAngle: 0,
// gauge
gauge_fullCircle: false,
gauge_label_show: true,
Expand All @@ -209,6 +210,7 @@ ChartInternal.prototype.getDefaultConfig = function () {
donut_title: "",
donut_expand: {},
donut_expand_duration: 50,
donut_padAngle: 0,
// spline
spline_interpolation_type: 'cardinal',
// stanford
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import './api.category';
import './api.chart';
import './api.color';
import './api.data';
import './api.donut';
import './api.flow';
import './api.focus';
import './api.grid';
import './api.group';
import './api.legend';
import './api.load';
import './api.pie';
import './api.region';
import './api.selection';
import './api.show';
Expand Down

0 comments on commit 20dff29

Please sign in to comment.