#Bug 333: Support for passing point index to drawPointCallback and drawHighlightPoint...
[dygraphs.git] / plugins / grid.js
CommitLineData
cbe41be1
DV
1/**
2 * @license
3 * Copyright 2012 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
0cd1ad15 6/*global Dygraph:false */
cbe41be1
DV
7
8Dygraph.Plugins.Grid = (function() {
9
10/*
11
12Current bits of jankiness:
a353dfe3
DV
13- Direct layout access
14- Direct area access
cbe41be1
DV
15
16*/
17
18"use strict";
19
20
21/**
22 * Draws the gridlines, i.e. the gray horizontal & vertical lines running the
23 * length of the chart.
24 *
25 * @constructor
26 */
27var grid = function() {
28};
29
30grid.prototype.toString = function() {
31 return "Gridline Plugin";
32};
33
34grid.prototype.activate = function(g) {
35 return {
98eb4713 36 willDrawChart: this.willDrawChart
cbe41be1
DV
37 };
38};
39
98eb4713 40grid.prototype.willDrawChart = function(e) {
cbe41be1
DV
41 // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to
42 // half-integers. This prevents them from drawing in two rows/cols.
43 var g = e.dygraph;
44 var ctx = e.drawingContext;
45 var layout = g.layout_;
46 var area = e.dygraph.plotter_.area;
47
48 function halfUp(x) { return Math.round(x) + 0.5; }
49 function halfDown(y){ return Math.round(y) - 0.5; }
50
51 var x, y, i, ticks;
52 if (g.getOption('drawYGrid')) {
53 ticks = layout.yticks;
54 ctx.save();
55 ctx.strokeStyle = g.getOption('gridLineColor');
56 ctx.lineWidth = g.getOption('gridLineWidth');
57 for (i = 0; i < ticks.length; i++) {
58 // TODO(danvk): allow secondary axes to draw a grid, too.
59 if (ticks[i][0] !== 0) continue;
60 x = halfUp(area.x);
61 y = halfDown(area.y + ticks[i][1] * area.h);
62 ctx.beginPath();
63 ctx.moveTo(x, y);
64 ctx.lineTo(x + area.w, y);
65 ctx.closePath();
66 ctx.stroke();
67 }
68 ctx.restore();
69 }
70
71 if (g.getOption('drawXGrid')) {
72 ticks = layout.xticks;
73 ctx.save();
74 ctx.strokeStyle = g.getOption('gridLineColor');
75 ctx.lineWidth = g.getOption('gridLineWidth');
76 for (i = 0; i < ticks.length; i++) {
77 x = halfUp(area.x + ticks[i][0] * area.w);
78 y = halfDown(area.y + area.h);
79 ctx.beginPath();
80 ctx.moveTo(x, y);
81 ctx.lineTo(x, area.y);
82 ctx.closePath();
83 ctx.stroke();
84 }
85 ctx.restore();
86 }
42a9ebb8 87};
cbe41be1
DV
88
89grid.prototype.destroy = function() {
90};
91
92return grid;
93
94})();