Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 870b994

Browse files
slinhartetimberg
authored andcommitted
Added 'angle' option to Polar Charts (chartjs#5279)
* added 'angle' option to polar charts. image comparison test is work in progress; not currently passing * removed unnecessary variable assignment * code cleanup based on PR; for 'angle' option on polarCharts * Made polar chart image comparison test pass by removing debug flag. Also explicitly marked _computeAngle as private. * Removed visibleCount computation in polar chart * split out code related to updating the radius in polar chart's update function, into it's own 'updateRadius' function * made updateRadius method private * fix linting error * updated polar charts to read custom angles from "chart.options.elements.arc.angle" instead of "chart.options.angle"
1 parent ae889d5 commit 870b994

File tree

6 files changed

+117
-24
lines changed

6 files changed

+117
-24
lines changed

src/controllers/controller.polarArea.js

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,25 +117,47 @@ module.exports = function(Chart) {
117117
linkScales: helpers.noop,
118118

119119
update: function(reset) {
120+
var me = this;
121+
var dataset = me.getDataset();
122+
var meta = me.getMeta();
123+
var start = me.chart.options.startAngle || 0;
124+
var starts = me._starts = [];
125+
var angles = me._angles = [];
126+
var i, ilen, angle;
127+
128+
me._updateRadius();
129+
130+
meta.count = me.countVisibleElements();
131+
132+
for (i = 0, ilen = dataset.data.length; i < ilen; i++) {
133+
starts[i] = start;
134+
angle = me._computeAngle(i);
135+
angles[i] = angle;
136+
start += angle;
137+
}
138+
139+
helpers.each(meta.data, function(arc, index) {
140+
me.updateElement(arc, index, reset);
141+
});
142+
},
143+
144+
/**
145+
* @private
146+
*/
147+
_updateRadius: function() {
120148
var me = this;
121149
var chart = me.chart;
122150
var chartArea = chart.chartArea;
123-
var meta = me.getMeta();
124151
var opts = chart.options;
125152
var arcOpts = opts.elements.arc;
126153
var minSize = Math.min(chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
154+
127155
chart.outerRadius = Math.max((minSize - arcOpts.borderWidth / 2) / 2, 0);
128156
chart.innerRadius = Math.max(opts.cutoutPercentage ? (chart.outerRadius / 100) * (opts.cutoutPercentage) : 1, 0);
129157
chart.radiusLength = (chart.outerRadius - chart.innerRadius) / chart.getVisibleDatasetCount();
130158

131159
me.outerRadius = chart.outerRadius - (chart.radiusLength * me.index);
132160
me.innerRadius = me.outerRadius - chart.radiusLength;
133-
134-
meta.count = me.countVisibleElements();
135-
136-
helpers.each(meta.data, function(arc, index) {
137-
me.updateElement(arc, index, reset);
138-
});
139161
},
140162

141163
updateElement: function(arc, index, reset) {
@@ -147,25 +169,14 @@ module.exports = function(Chart) {
147169
var scale = chart.scale;
148170
var labels = chart.data.labels;
149171

150-
var circumference = me.calculateCircumference(dataset.data[index]);
151172
var centerX = scale.xCenter;
152173
var centerY = scale.yCenter;
153174

154-
// If there is NaN data before us, we need to calculate the starting angle correctly.
155-
// We could be way more efficient here, but its unlikely that the polar area chart will have a lot of data
156-
var visibleCount = 0;
157-
var meta = me.getMeta();
158-
for (var i = 0; i < index; ++i) {
159-
if (!isNaN(dataset.data[i]) && !meta.data[i].hidden) {
160-
++visibleCount;
161-
}
162-
}
163-
164175
// var negHalfPI = -0.5 * Math.PI;
165176
var datasetStartAngle = opts.startAngle;
166177
var distance = arc.hidden ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]);
167-
var startAngle = datasetStartAngle + (circumference * visibleCount);
168-
var endAngle = startAngle + (arc.hidden ? 0 : circumference);
178+
var startAngle = me._starts[index];
179+
var endAngle = startAngle + (arc.hidden ? 0 : me._angles[index]);
169180

170181
var resetRadius = animationOpts.animateScale ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]);
171182

@@ -211,12 +222,31 @@ module.exports = function(Chart) {
211222
return count;
212223
},
213224

214-
calculateCircumference: function(value) {
225+
/**
226+
* @private
227+
*/
228+
_computeAngle: function(index) {
229+
var me = this;
215230
var count = this.getMeta().count;
216-
if (count > 0 && !isNaN(value)) {
217-
return (2 * Math.PI) / count;
231+
var dataset = me.getDataset();
232+
var meta = me.getMeta();
233+
234+
if (isNaN(dataset.data[index]) || meta.data[index].hidden) {
235+
return 0;
218236
}
219-
return 0;
237+
238+
// Scriptable options
239+
var context = {
240+
chart: me.chart,
241+
dataIndex: index,
242+
dataset: dataset,
243+
datasetIndex: me.index
244+
};
245+
246+
return helpers.options.resolve([
247+
me.chart.options.elements.arc.angle,
248+
(2 * Math.PI) / count
249+
], context, index);
220250
}
221251
});
222252
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"config": {
3+
"type": "polarArea",
4+
"data": {
5+
"labels": ["A", "B", "C", "D", "E"],
6+
"datasets": [{
7+
"data": [11, 16, 21, 7, 10],
8+
"backgroundColor": [
9+
"rgba(255, 99, 132, 0.8)",
10+
"rgba(54, 162, 235, 0.8)",
11+
"rgba(255, 206, 86, 0.8)",
12+
"rgba(75, 192, 192, 0.8)",
13+
"rgba(153, 102, 255, 0.8)",
14+
"rgba(255, 159, 64, 0.8)"
15+
]
16+
}]
17+
},
18+
"options": {
19+
"elements": {
20+
"arc": {
21+
"angle": [
22+
1.0566, 1.7566, 1.0566, 2.1566, 0.2566
23+
]
24+
}
25+
},
26+
"responsive": false,
27+
"legend": false,
28+
"title": false,
29+
"scale": {
30+
"display": false
31+
}
32+
}
33+
}
34+
}
28.2 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"config": {
3+
"type": "polarArea",
4+
"data": {
5+
"labels": ["A", "B", "C", "D", "E"],
6+
"datasets": [{
7+
"data": [11, 16, 21, 7, 10],
8+
"backgroundColor": [
9+
"rgba(255, 99, 132, 0.8)",
10+
"rgba(54, 162, 235, 0.8)",
11+
"rgba(255, 206, 86, 0.8)",
12+
"rgba(75, 192, 192, 0.8)",
13+
"rgba(153, 102, 255, 0.8)",
14+
"rgba(255, 159, 64, 0.8)"
15+
]
16+
}]
17+
},
18+
"options": {
19+
"responsive": false,
20+
"legend": false,
21+
"title": false,
22+
"scale": {
23+
"display": false
24+
}
25+
}
26+
}
27+
}
26.1 KB
Loading

test/specs/controller.polarArea.tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
describe('auto', jasmine.specsFromFixtures('controller.polarArea'));
2+
13
describe('Chart.controllers.polarArea', function() {
24
it('should be constructed', function() {
35
var chart = window.acquireChart({

0 commit comments

Comments
 (0)