3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
9 * Including this file will add several additional shapes to Dygraph.Circles
10 * which can be passed to drawPointCallback.
11 * See tests/custom-circles.html for usage.
17 * @param {!CanvasRenderingContext2D} ctx the canvas context
18 * @param {number} sides the number of sides in the shape.
19 * @param {number} radius the radius of the image.
20 * @param {number} cx center x coordate
21 * @param {number} cy center y coordinate
22 * @param {number=} rotationRadians the shift of the initial angle, in radians.
23 * @param {number=} delta the angle shift for each line. If missing, creates a
26 var regularShape
= function(
27 ctx
, sides
, radius
, cx
, cy
, rotationRadians
, delta
) {
28 rotationRadians
= rotationRadians
|| 0;
29 delta
= delta
|| Math
.PI
* 2 / sides
;
32 var initialAngle
= rotationRadians
;
33 var angle
= initialAngle
;
35 var computeCoordinates
= function() {
36 var x
= cx
+ (Math
.sin(angle
) * radius
);
37 var y
= cy
+ (-Math
.cos(angle
) * radius
);
41 var initialCoordinates
= computeCoordinates();
42 var x
= initialCoordinates
[0];
43 var y
= initialCoordinates
[1];
46 for (var idx
= 0; idx
< sides
; idx
++) {
47 angle
= (idx
== sides
- 1) ? initialAngle
: (angle
+ delta
);
48 var coords
= computeCoordinates();
49 ctx
.lineTo(coords
[0], coords
[1]);
56 * TODO(danvk): be more specific on the return type.
57 * @param {number} sides
58 * @param {number=} rotationRadians
59 * @param {number=} delta
63 var shapeFunction
= function(sides
, rotationRadians
, delta
) {
64 return function(g
, name
, ctx
, cx
, cy
, color
, radius
) {
65 ctx
.strokeStyle
= color
;
66 ctx
.fillStyle
= "white";
67 regularShape(ctx
, sides
, radius
, cx
, cy
, rotationRadians
, delta
);
71 Dygraph
.update(Dygraph
.Circles
, {
72 TRIANGLE
: shapeFunction(3),
73 SQUARE
: shapeFunction(4, Math
.PI
/ 4),
74 DIAMOND
: shapeFunction(4),
75 PENTAGON
: shapeFunction(5),
76 HEXAGON
: shapeFunction(6),
77 CIRCLE
: function(g
, name
, ctx
, cx
, cy
, color
, radius
) {
79 ctx
.strokeStyle
= color
;
80 ctx
.fillStyle
= "white";
81 ctx
.arc(cx
, cy
, radius
, 0, 2 * Math
.PI
, false);
85 STAR
: shapeFunction(5, 0, 4 * Math
.PI
/ 5),
86 PLUS
: function(g
, name
, ctx
, cx
, cy
, color
, radius
) {
87 ctx
.strokeStyle
= color
;
90 ctx
.moveTo(cx
+ radius
, cy
);
91 ctx
.lineTo(cx
- radius
, cy
);
96 ctx
.moveTo(cx
, cy
+ radius
);
97 ctx
.lineTo(cx
, cy
- radius
);
101 EX
: function(g
, name
, ctx
, cx
, cy
, color
, radius
) {
102 ctx
.strokeStyle
= color
;
105 ctx
.moveTo(cx
+ radius
, cy
+ radius
);
106 ctx
.lineTo(cx
- radius
, cy
- radius
);
111 ctx
.moveTo(cx
+ radius
, cy
- radius
);
112 ctx
.lineTo(cx
- radius
, cy
+ radius
);