Skip to content

Commit

Permalink
v3 stuff, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Dec 22, 2019
1 parent b6ed7cc commit 854c91d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ 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;
return helpers.math.toRadians(deg) - 0.5 * Math.PI;
}

module.exports = DatasetController.extend({
Expand Down
19 changes: 15 additions & 4 deletions src/helpers/helpers.math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import {isFinite as isFiniteNumber} from './helpers.core';

const PI = Math.PI;
const TAU = 2 * PI;

/**
* @alias Chart.helpers.math
* @namespace
Expand Down Expand Up @@ -80,11 +83,11 @@ export const sign = Math.sign ?
};

export function toRadians(degrees) {
return degrees * (Math.PI / 180);
return degrees * (PI / 180);
}

export function toDegrees(radians) {
return radians * (180 / Math.PI);
return radians * (180 / PI);
}

/**
Expand Down Expand Up @@ -115,8 +118,8 @@ export function getAngleFromPoint(centrePoint, anglePoint) {

var angle = Math.atan2(distanceFromYCenter, distanceFromXCenter);

if (angle < (-0.5 * Math.PI)) {
angle += 2.0 * Math.PI; // make sure the returned angle is in the range of (-PI/2, 3PI/2]
if (angle < (-0.5 * PI)) {
angle += 2.0 * PI; // make sure the returned angle is in the range of (-PI/2, 3PI/2]
}

return {
Expand All @@ -128,3 +131,11 @@ export function getAngleFromPoint(centrePoint, anglePoint) {
export function distanceBetweenPoints(pt1, pt2) {
return Math.sqrt(Math.pow(pt2.x - pt1.x, 2) + Math.pow(pt2.y - pt1.y, 2));
}

/**
* Normalize angle to be between 0 and 2*PI
* @private
*/
export function _normalizeAngle(a) {
return (a % TAU + TAU) % TAU;
}
6 changes: 3 additions & 3 deletions src/scales/scale.radialLinear.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import defaults from '../core/core.defaults';
import helpers from '../helpers/index';
import {isNumber, toDegrees} from '../helpers/helpers.math';
import {isNumber, toDegrees, toRadians, _normalizeAngle} from '../helpers/helpers.math';
import LinearScaleBase from './scale.linearbase';
import Ticks from '../core/core.ticks';

Expand Down Expand Up @@ -157,7 +157,7 @@ function fitWithPointLabels(scale) {

// Add quarter circle to make degree 0 mean top of circle
var angleRadians = scale.getIndexAngle(i);
var angle = toDegrees(angleRadians) % 360;
var angle = toDegrees(angleRadians);
var hLimits = determineLimits(angle, pointPosition.x, textSize.w, 0, 180);
var vLimits = determineLimits(angle, pointPosition.y, textSize.h, 90, 270);

Expand Down Expand Up @@ -384,7 +384,7 @@ class RadialLinearScale extends LinearScaleBase {
var options = chart.options || {};
var startAngle = options.startAngle || 0;

return index * angleMultiplier + helpers.toRadians(startAngle);
return _normalizeAngle(index * angleMultiplier + toRadians(startAngle));
}

getDistanceFromCenterForValue(value) {
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/controller.polarArea/angle-lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"threshold": 0.05,
"config": {
"type": "polarArea",
"data": {
"labels": ["A", "B", "C", "D", "E"],
"datasets": [{
"data": [11, 16, 21, 7, 10],
"backgroundColor": [
"rgba(255, 99, 132, 0.8)",
"rgba(54, 162, 235, 0.8)",
"rgba(255, 206, 86, 0.8)",
"rgba(75, 192, 192, 0.8)",
"rgba(153, 102, 255, 0.8)",
"rgba(255, 159, 64, 0.8)"
]
}]
},
"options": {
"responsive": false,
"legend": false,
"title": false,
"scale": {
"display": true,
"angleLines": {
"display": true,
"color": "#000"
},
"ticks": {
"display": false
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/specs/scale.radialLinear.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ describe('Test the radial linear scale', function() {
scale.ctx.getCalls().filter(function(x) {
return x.name === 'setTextAlign';
}).forEach(function(x, i) {
expect(x.args[0]).toBe(expected.textAlign[i]);
expect(x.args[0]).withContext('startAngle: ' + expected.startAngle + ', tick: ' + i).toBe(expected.textAlign[i]);
});

scale.ctx.getCalls().filter(function(x) {
Expand Down

0 comments on commit 854c91d

Please sign in to comment.