3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
7 Dygraph
.Plugins
.Grid
= (function() {
11 Current bits of jankiness:
12 - Direct layout access
21 * Draws the gridlines, i.e. the gray horizontal & vertical lines running the
22 * length of the chart.
26 var grid
= function() {
29 grid
.prototype.toString
= function() {
30 return "Gridline Plugin";
33 grid
.prototype.activate
= function(g
) {
35 willDrawChart
: this.willDrawChart
39 grid
.prototype.willDrawChart
= function(e
) {
40 // Draw the new X/Y grid
. Lines appear crisper when pixels are rounded to
41 // half-integers. This prevents them from drawing in two rows/cols.
43 var ctx
= e
.drawingContext
;
44 var layout
= g
.layout_
;
45 var area
= e
.dygraph
.plotter_
.area
;
47 function halfUp(x
) { return Math
.round(x
) + 0.5; }
48 function halfDown(y
){ return Math
.round(y
) - 0.5; }
51 if (g
.getOption('drawYGrid')) {
52 ticks
= layout
.yticks
;
54 ctx
.strokeStyle
= g
.getOption('gridLineColor');
55 ctx
.lineWidth
= g
.getOption('gridLineWidth');
56 for (i
= 0; i
< ticks
.length
; i
++) {
57 // TODO(danvk): allow secondary axes to draw a grid, too.
58 if (ticks
[i
][0] !== 0) continue;
60 y
= halfDown(area
.y
+ ticks
[i
][1] * area
.h
);
63 ctx
.lineTo(x
+ area
.w
, y
);
70 if (g
.getOption('drawXGrid')) {
71 ticks
= layout
.xticks
;
73 ctx
.strokeStyle
= g
.getOption('gridLineColor');
74 ctx
.lineWidth
= g
.getOption('gridLineWidth');
75 for (i
= 0; i
< ticks
.length
; i
++) {
76 x
= halfUp(area
.x
+ ticks
[i
][0] * area
.w
);
77 y
= halfDown(area
.y
+ area
.h
);
80 ctx
.lineTo(x
, area
.y
);
88 grid
.prototype.destroy
= function() {