Commit | Line | Data |
---|---|---|
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 | 7 | |
cbe41be1 DV |
8 | /* |
9 | ||
10 | Current bits of jankiness: | |
a353dfe3 DV |
11 | - Direct layout access |
12 | - Direct area access | |
cbe41be1 DV |
13 | |
14 | */ | |
15 | ||
16 | "use strict"; | |
17 | ||
18 | ||
19 | /** | |
20 | * Draws the gridlines, i.e. the gray horizontal & vertical lines running the | |
21 | * length of the chart. | |
22 | * | |
23 | * @constructor | |
24 | */ | |
25 | var grid = function() { | |
26 | }; | |
27 | ||
28 | grid.prototype.toString = function() { | |
29 | return "Gridline Plugin"; | |
30 | }; | |
31 | ||
32 | grid.prototype.activate = function(g) { | |
33 | return { | |
98eb4713 | 34 | willDrawChart: this.willDrawChart |
cbe41be1 DV |
35 | }; |
36 | }; | |
37 | ||
98eb4713 | 38 | grid.prototype.willDrawChart = function(e) { |
cbe41be1 DV |
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. | |
41 | var g = e.dygraph; | |
42 | var ctx = e.drawingContext; | |
43 | var layout = g.layout_; | |
44 | var area = e.dygraph.plotter_.area; | |
45 | ||
46 | function halfUp(x) { return Math.round(x) + 0.5; } | |
47 | function halfDown(y){ return Math.round(y) - 0.5; } | |
48 | ||
49 | var x, y, i, ticks; | |
7f6a7190 | 50 | if (g.getOptionForAxis('drawGrid', 'y')) { |
e8b3c7b4 DE |
51 | var axes = ["y", "y2"]; |
52 | var strokeStyles = [], lineWidths = [], drawGrid = [], stroking = [], strokePattern = []; | |
53 | for (var i = 0; i < axes.length; i++) { | |
7f6a7190 | 54 | drawGrid[i] = g.getOptionForAxis('drawGrid', axes[i]); |
e8b3c7b4 DE |
55 | if (drawGrid[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); | |
9e906ae6 DE |
60 | } |
61 | } | |
cbe41be1 DV |
62 | ticks = layout.yticks; |
63 | ctx.save(); | |
e8b3c7b4 | 64 | // draw grids for the different y axes |
cbe41be1 | 65 | for (i = 0; i < ticks.length; i++) { |
9e906ae6 | 66 | var axis = ticks[i][0]; |
34fc91d4 | 67 | if(drawGrid[axis]) { |
e8c70e4e | 68 | ctx.save(); |
9e906ae6 | 69 | if (stroking[axis]) { |
e8c70e4e | 70 | if (ctx.setLineDash) ctx.setLineDash(strokePattern[axis]); |
9e906ae6 DE |
71 | } |
72 | ctx.strokeStyle = strokeStyles[axis]; | |
73 | ctx.lineWidth = lineWidths[axis]; | |
74 | ||
75 | x = halfUp(area.x); | |
76 | y = halfDown(area.y + ticks[i][1] * area.h); | |
77 | ctx.beginPath(); | |
78 | ctx.moveTo(x, y); | |
79 | ctx.lineTo(x + area.w, y); | |
9e906ae6 DE |
80 | ctx.stroke(); |
81 | ||
e8c70e4e | 82 | ctx.restore(); |
97adf46b | 83 | } |
cbe41be1 DV |
84 | } |
85 | ctx.restore(); | |
86 | } | |
87 | ||
e8b3c7b4 | 88 | // draw grid for x axis |
7f6a7190 | 89 | if (g.getOptionForAxis('drawGrid', 'x')) { |
cbe41be1 DV |
90 | ticks = layout.xticks; |
91 | ctx.save(); | |
e8b3c7b4 | 92 | var strokePattern = g.getOptionForAxis('gridLinePattern', 'x'); |
9e906ae6 DE |
93 | var stroking = strokePattern && (strokePattern.length >= 2); |
94 | if (stroking) { | |
e8c70e4e | 95 | if (ctx.setLineDash) ctx.setLineDash(strokePattern); |
9e906ae6 | 96 | } |
e8b3c7b4 DE |
97 | ctx.strokeStyle = g.getOptionForAxis('gridLineColor', 'x'); |
98 | ctx.lineWidth = g.getOptionForAxis('gridLineWidth', 'x'); | |
cbe41be1 DV |
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); | |
102 | ctx.beginPath(); | |
103 | ctx.moveTo(x, y); | |
104 | ctx.lineTo(x, area.y); | |
105 | ctx.closePath(); | |
106 | ctx.stroke(); | |
107 | } | |
9e906ae6 | 108 | if (stroking) { |
e8c70e4e | 109 | if (ctx.setLineDash) ctx.setLineDash([]); |
9e906ae6 | 110 | } |
cbe41be1 DV |
111 | ctx.restore(); |
112 | } | |
42a9ebb8 | 113 | }; |
cbe41be1 DV |
114 | |
115 | grid.prototype.destroy = function() { | |
116 | }; | |
117 | ||
6ecc0739 | 118 | export default grid; |