Skip to content

Commit

Permalink
Polar area: startAngle in degrees, 0 at top.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Dec 31, 2019
1 parent ab39286 commit 2be7703
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
3 changes: 2 additions & 1 deletion docs/charts/polar.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ These are the customisation options specific to Polar Area charts. These options

| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `startAngle` | `number` | `-0.5 * Math.PI` | Starting angle to draw arcs for the first item in a dataset.
| `startAngle` | `number` | `0` | Starting angle to draw arcs for the first item in a dataset. In degrees, 0 is at top.
| `animation.animateRotate` | `boolean` | `true` | If true, the chart will animate in with a rotation animation. This property is in the `options.animation` object.
| `animation.animateScale` | `boolean` | `true` | If true, will animate scaling the chart from the center outwards.

Expand All @@ -110,6 +110,7 @@ These are the customisation options specific to Polar Area charts. These options
We can also change these defaults values for each PolarArea type that is created, this object is available at `Chart.defaults.polarArea`. Changing the global options only affects charts created after the change. Existing charts are not changed.

For example, to configure all new polar area charts with `animateScale = false` you would do:

```javascript
Chart.defaults.polarArea.animation.animateScale = false;
```
Expand Down
19 changes: 11 additions & 8 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defaults._set('polarArea', {
}
},

startAngle: -0.5 * Math.PI,
startAngle: 0,
legendCallback: function(chart) {
var list = document.createElement('ul');
var data = chart.data;
Expand Down Expand Up @@ -106,6 +106,12 @@ defaults._set('polarArea', {
}
});

function getStartAngleRadians(deg) {
// radianLinear scale draws angleLines using startAngle. 0 is excepted to be at top.
// Here we adjust to standard unit circle used in drawing, where 0 is at right.
return helpers.toRadians(deg) - 0.5 * Math.PI;
}

module.exports = DatasetController.extend({

dataElementType: elements.Arc,
Expand Down Expand Up @@ -138,13 +144,10 @@ module.exports = DatasetController.extend({
},

update: function(mode) {
var me = this;
var meta = me._cachedMeta;
var arcs = meta.data;

me._updateRadius();
const arcs = this._cachedMeta.data;

me.updateElements(arcs, 0, mode);
this._updateRadius();
this.updateElements(arcs, 0, mode);
},

/**
Expand Down Expand Up @@ -175,7 +178,7 @@ module.exports = DatasetController.extend({
const scale = chart.scales.r;
const centerX = scale.xCenter;
const centerY = scale.yCenter;
const datasetStartAngle = opts.startAngle || 0;
const datasetStartAngle = getStartAngleRadians(opts.startAngle);
let angle = datasetStartAngle;
let i;

Expand Down
7 changes: 2 additions & 5 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,11 @@ class RadialLinearScale extends LinearScaleBase {

getIndexAngle(index) {
var chart = this.chart;
var angleMultiplier = 360 / chart.data.labels.length;
var angleMultiplier = (Math.PI * 2) / chart.data.labels.length;
var options = chart.options || {};
var startAngle = options.startAngle || 0;

// Start from the top instead of right, so remove a quarter of the circle
var angle = (index * angleMultiplier + startAngle) % 360;

return (angle < 0 ? angle + 360 : angle) * Math.PI * 2 / 360;
return index * angleMultiplier + helpers.toRadians(startAngle);
}

getDistanceFromCenterForValue(value) {
Expand Down
2 changes: 1 addition & 1 deletion test/specs/controller.polarArea.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Chart.controllers.polarArea', function() {
showLines: true,
legend: false,
title: false,
startAngle: 0, // default is -0.5 * Math.PI
startAngle: 90, // default is 0
elements: {
arc: {
backgroundColor: 'rgb(255, 0, 0)',
Expand Down

0 comments on commit 2be7703

Please sign in to comment.