New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added 'angle' option to Polar Charts #5279
Changes from 2 commits
94bf815
8c8b864
ff432f6
debbf75
3f1eb49
86419e2
66e8e53
45d3bc0
4a42e41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,13 +116,18 @@ module.exports = function(Chart) { | |
|
||
linkScales: helpers.noop, | ||
|
||
_starts: {}, | ||
|
||
_angles: {}, | ||
|
||
update: function(reset) { | ||
var me = this; | ||
var chart = me.chart; | ||
var chartArea = chart.chartArea; | ||
var meta = me.getMeta(); | ||
var opts = chart.options; | ||
var arcOpts = opts.elements.arc; | ||
var dataset = me.getDataset(); | ||
var minSize = Math.min(chartArea.right - chartArea.left, chartArea.bottom - chartArea.top); | ||
chart.outerRadius = Math.max((minSize - arcOpts.borderWidth / 2) / 2, 0); | ||
chart.innerRadius = Math.max(opts.cutoutPercentage ? (chart.outerRadius / 100) * (opts.cutoutPercentage) : 1, 0); | ||
|
@@ -133,6 +138,14 @@ module.exports = function(Chart) { | |
|
||
meta.count = me.countVisibleElements(); | ||
|
||
var start = opts.startAngle || 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minifier] I would move this declaration right after |
||
for (var i = 0; i < meta.count; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure we only want to iterate until |
||
me._starts[i] = start; | ||
var angle = me.computeAngle(dataset.data[i], i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minifier/optim] I would move |
||
me._angles[i] = angle; | ||
start += angle; | ||
} | ||
|
||
helpers.each(meta.data, function(arc, index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid accessing object properties at every iteration, I would use local variables: var minSize = ...
var start = opts.startAngle || 0;
var starts = me._starts = [];
var angles = me._angles = [];
var i, ilen;
//...
meta.count = me.countVisibleElements();
for (i = 0, ilen = dataset.data.length; i < ilen; i++) {
angle = me._computeAngle(i);
angles[i] = angle;
starts[i] = start;
start += angle
} Edit: |
||
me.updateElement(arc, index, reset); | ||
}); | ||
|
@@ -147,7 +160,6 @@ module.exports = function(Chart) { | |
var scale = chart.scale; | ||
var labels = chart.data.labels; | ||
|
||
var circumference = me.calculateCircumference(dataset.data[index]); | ||
var centerX = scale.xCenter; | ||
var centerY = scale.yCenter; | ||
|
||
|
@@ -164,8 +176,8 @@ module.exports = function(Chart) { | |
// var negHalfPI = -0.5 * Math.PI; | ||
var datasetStartAngle = opts.startAngle; | ||
var distance = arc.hidden ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]); | ||
var startAngle = datasetStartAngle + (circumference * visibleCount); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Travis complains that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, thanks "Travis" 😅 |
||
var endAngle = startAngle + (arc.hidden ? 0 : circumference); | ||
var startAngle = me._starts[index]; | ||
var endAngle = startAngle + (arc.hidden ? 0 : me._angles[index]); | ||
|
||
var resetRadius = animationOpts.animateScale ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]); | ||
|
||
|
@@ -211,12 +223,29 @@ module.exports = function(Chart) { | |
return count; | ||
}, | ||
|
||
calculateCircumference: function(value) { | ||
computeAngle: function(value, index) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If we agree to rename it, then the new one should be explicitly private: /**
* @private
*/
_computeAngle: function(value, index) {
// ...
} |
||
var me = this; | ||
var count = this.getMeta().count; | ||
if (count > 0 && !isNaN(value)) { | ||
return (2 * Math.PI) / count; | ||
if (count <= 0 || isNaN(value)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also need to return |
||
return 0; | ||
} | ||
return 0; | ||
|
||
var defaultAngle = (2 * Math.PI) / count; | ||
var angleOption = me.chart.options.angle || defaultAngle; | ||
var resolve = helpers.options.resolve; | ||
var dataset = me.getDataset(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change the method signature to only pass _computeAngle: function(index) {
//...
var dataset = me.getDataset();
var value = dataset.data[index];
//...
} |
||
|
||
// Scriptable options | ||
var context = { | ||
chart: me.chart, | ||
dataIndex: index, | ||
dataset: dataset, | ||
datasetIndex: me.index | ||
}; | ||
|
||
return resolve([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
return helpers.options.resolve([
// potentially: dataset.angle
// potentially: me.chart.options.elements.arc.angle
me.chart.options.angle,
(2 * Math.PI) / count
], context, index); This is because if |
||
angleOption | ||
], context, index); | ||
} | ||
}); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"debug": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be removed, it's only used for debugging and is likely the reason why unit tests are failing on Travis. |
||
"config": { | ||
"debug": true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one can be removed because it doesn't do anything |
||
"type": "polarArea", | ||
"data": { | ||
"labels": ["A", "B", "C", "D", "E"], | ||
"datasets": [{ | ||
"data": [11, 16, 21, 7, 10] | ||
}] | ||
}, | ||
"options": { | ||
"angle": [ | ||
1.0566, 1.7566, 1.0566, 2.1566, 0.2566 | ||
], | ||
"responsive": false, | ||
"legend": false, | ||
"title": false | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove all UI that are not part of the tests, which include the labels and the "grid lines". Maybe adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also pick a more accentuated and different background color for each slices to make sure it fails if there is overlapping elements. |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extend()
(alias ofinherit()
) augments the prototype,_starts
and_angles
shouldn't be declared that way else they will be shared between all polar controller instances. Actually, I don't think they need to be pre-declared at all since you need to reset both inupdate()
(me._starts = []
andme._angles = []
).