Skip to content
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

Treat negative values in doughnut charts as positive #5165

Merged
merged 1 commit into from Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controllers/controller.doughnut.js
Expand Up @@ -273,7 +273,7 @@ module.exports = function(Chart) {
calculateCircumference: function(value) {
var total = this.getMeta().total;
if (total > 0 && !isNaN(value)) {
return (Math.PI * 2.0) * (value / total);
return (Math.PI * 2.0) * (Math.abs(value) / total);
}
return 0;
},
Expand Down
40 changes: 40 additions & 0 deletions test/specs/controller.doughnut.tests.js
Expand Up @@ -205,6 +205,46 @@ describe('Chart.controllers.doughnut', function() {
});
});

it('should treat negative values as positive', function() {
var chart = window.acquireChart({
type: 'doughnut',
data: {
datasets: [{
data: [-1, -3]
}],
labels: ['label0', 'label1']
},
options: {
legend: false,
title: false,
cutoutPercentage: 50,
rotation: Math.PI,
circumference: Math.PI * 0.5,
elements: {
arc: {
backgroundColor: 'rgb(255, 0, 0)',
borderColor: 'rgb(0, 0, 255)',
borderWidth: 2
}
}
}
});

var meta = chart.getDatasetMeta(0);

expect(meta.data.length).toBe(2);

// Only startAngle, endAngle and circumference should be different.
[
{c: Math.PI / 8, s: Math.PI, e: Math.PI + Math.PI / 8},
{c: 3 * Math.PI / 8, s: Math.PI + Math.PI / 8, e: Math.PI + Math.PI / 2}
].forEach(function(expected, i) {
expect(meta.data[i]._model.circumference).toBeCloseTo(expected.c, 8);
expect(meta.data[i]._model.startAngle).toBeCloseTo(expected.s, 8);
expect(meta.data[i]._model.endAngle).toBeCloseTo(expected.e, 8);
});
});

it ('should draw all arcs', function() {
var chart = window.acquireChart({
type: 'doughnut',
Expand Down