-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_helper.js
56 lines (48 loc) · 2.3 KB
/
draw_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function degreesToRadians(degrees){
return (degrees * (Math.PI/180));
}
function getLocationOnCircle(centerX, centerY, magnitude, radians){
var point = {};
point.x = centerX + (magnitude * Math.sin(radians));
point.y = centerY - (magnitude * Math.cos(radians));
return point;
}
function drawTicks(data, tickLength, tickColour){
var drawnticks = data.map(function(point, index){
var angleInRadians = degreesToRadians(point.position);
console.log("point",point,"angel",angleInRadians);
var tickStartPoint = getLocationOnCircle(centerX, centerY, radius, angleInRadians);
var tickEndPoint = getLocationOnCircle(centerX, centerY, (radius + tickLength), angleInRadians);
context.beginPath();
// tick
context.moveTo(tickStartPoint.xValue, tickStartPoint.yValue);
context.strokeStyle = tickColour;
context.lineTo(tickEndPoint.xValue, tickEndPoint.yValue);
context.closePath();
context.stroke();
context.save();
// tick dates
context.translate(centerX, centerY);
context.rotate(angleInRadians);
context.rotate(-Math.PI/2);
context.textBaseline = 'middle';
context.strokeStyle = tickColour;
context.fillText(point.date, radius + tickLength + 10, 0);
context.restore();
});
console.table(drawnticks, ["angleInRadians"]);
}
// temporary colour changer from https://stackoverflow.com/questions/12934720/how-to-increment-decrement-hex-color-values-with-javascript-jquery
function incrementColour(color, step) {
var colorToInt = parseInt(color.substr(1), 16), // Convert HEX color to integer
nstep = parseInt(step); // Convert step to integer
if(!isNaN(colorToInt) && !isNaN(nstep)){ // Make sure that color has been converted to integer
colorToInt += nstep; // Increment integer with step
var ncolor = colorToInt.toString(16); // Convert back integer to HEX
ncolor = '#' + (new Array(7-ncolor.length).join(0)) + ncolor; // Left pad "0" to make HEX look like a color
if(/^#[0-9a-f]{6}$/i.test(ncolor)){ // Make sure that HEX is a valid color
return ncolor;
}
}
return color;
};