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 Mar 2, 2019
1 parent 946c6d0 commit fb5c00b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/charts/polar.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ All these values, if `undefined`, fallback to the associated [`elements.arc.*`](
### Border Alignment

The following values are supported for `borderAlign`.

* `'center'` (default)
* `'inner'`

Expand All @@ -93,7 +94,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 @@ -102,6 +103,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
13 changes: 9 additions & 4 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defaults._set('polarArea', {
animateScale: true
},

startAngle: -0.5 * Math.PI,
startAngle: 0,
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
Expand Down Expand Up @@ -110,6 +110,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 All @@ -120,7 +126,7 @@ module.exports = DatasetController.extend({
var me = this;
var dataset = me.getDataset();
var meta = me.getMeta();
var start = me.chart.options.startAngle || 0;
var start = getStartAngleRadians(me.chart.options.startAngle || 0);
var starts = me._starts = [];
var angles = me._angles = [];
var arcs = meta.data;
Expand Down Expand Up @@ -173,8 +179,7 @@ module.exports = DatasetController.extend({
var centerX = scale.xCenter;
var centerY = scale.yCenter;

// var negHalfPI = -0.5 * Math.PI;
var datasetStartAngle = opts.startAngle;
var datasetStartAngle = getStartAngleRadians(opts.startAngle);
var distance = arc.hidden ? 0 : scale.getDistanceFromCenterForValue(dataset.data[index]);
var startAngle = me._starts[index];
var endAngle = startAngle + (arc.hidden ? 0 : me._angles[index]);
Expand Down
5 changes: 1 addition & 4 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,7 @@ module.exports = LinearScaleBase.extend({
this.chart.options.startAngle :
0;

var startAngleRadians = startAngle * Math.PI * 2 / 360;

// Start from the top instead of right, so remove a quarter of the circle
return index * angleMultiplier + startAngleRadians;
return index * angleMultiplier + helpers.toRadians(startAngle);
},

getDistanceFromCenterForValue: function(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 @@ -172,7 +172,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 fb5c00b

Please sign in to comment.