3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
6 /*global Dygraph:false */
10 Current bits of jankiness:
11 - Direct layout access
20 * Draws the gridlines, i.e. the gray horizontal & vertical lines running the
21 * length of the chart.
25 var grid
= function() {
28 grid
.prototype.toString
= function() {
29 return "Gridline Plugin";
32 grid
.prototype.activate
= function(g
) {
34 willDrawChart
: this.willDrawChart
38 grid
.prototype.willDrawChart
= function(e
) {
39 // Draw the new X/Y grid
. Lines appear crisper when pixels are rounded to
40 // half-integers. This prevents them from drawing in two rows/cols.
42 var ctx
= e
.drawingContext
;
43 var layout
= g
.layout_
;
44 var area
= e
.dygraph
.plotter_
.area
;
46 function halfUp(x
) { return Math
.round(x
) + 0.5; }
47 function halfDown(y
){ return Math
.round(y
) - 0.5; }
50 if (g
.getOptionForAxis('drawGrid', 'y')) {
51 var axes
= ["y", "y2"];
52 var strokeStyles
= [], lineWidths
= [], drawGrid
= [], stroking
= [], strokePattern
= [];
53 for (var i
= 0; i
< axes
.length
; i
++) {
54 drawGrid
[i
] = g
.getOptionForAxis('drawGrid', axes
[i
]);
56 strokeStyles
[i
] = g
.getOptionForAxis('gridLineColor', axes
[i
]);
57 lineWidths
[i
] = g
.getOptionForAxis('gridLineWidth', axes
[i
]);
58 strokePattern
[i
] = g
.getOptionForAxis('gridLinePattern', axes
[i
]);
59 stroking
[i
] = strokePattern
[i
] && (strokePattern
[i
].length
>= 2);
62 ticks
= layout
.yticks
;
64 // draw grids for the different y axes
65 for (i
= 0; i
< ticks
.length
; i
++) {
66 var axis
= ticks
[i
][0];
70 if (ctx
.setLineDash
) ctx
.setLineDash(strokePattern
[axis
]);
72 ctx
.strokeStyle
= strokeStyles
[axis
];
73 ctx
.lineWidth
= lineWidths
[axis
];
76 y
= halfDown(area
.y
+ ticks
[i
][1] * area
.h
);
79 ctx
.lineTo(x
+ area
.w
, y
);
88 // draw grid for x axis
89 if (g
.getOptionForAxis('drawGrid', 'x')) {
90 ticks
= layout
.xticks
;
92 var strokePattern
= g
.getOptionForAxis('gridLinePattern', 'x');
93 var stroking
= strokePattern
&& (strokePattern
.length
>= 2);
95 if (ctx
.setLineDash
) ctx
.setLineDash(strokePattern
);
97 ctx
.strokeStyle
= g
.getOptionForAxis('gridLineColor', 'x');
98 ctx
.lineWidth
= g
.getOptionForAxis('gridLineWidth', 'x');
99 for (i
= 0; i
< ticks
.length
; i
++) {
100 x
= halfUp(area
.x
+ ticks
[i
][0] * area
.w
);
101 y
= halfDown(area
.y
+ area
.h
);
104 ctx
.lineTo(x
, area
.y
);
109 if (ctx
.setLineDash
) ctx
.setLineDash([]);
115 grid
.prototype.destroy
= function() {