Move most of the interaction javascript to a .js file.
[dygraphs.git] / tests / interaction.js
1 function downV3(event, g, context) {
2 context.initializeMouseDown(event, g, context);
3 if (event.altKey || event.shiftKey) {
4 Dygraph.startZoom(event, g, context);
5 } else {
6 Dygraph.startPan(event, g, context);
7 }
8 }
9
10 function moveV3(event, g, context) {
11 if (context.isPanning) {
12 Dygraph.movePan(event, g, context);
13 } else if (context.isZooming) {
14 Dygraph.moveZoom(event, g, context);
15 }
16 }
17
18 function upV3(event, g, context) {
19 if (context.isPanning) {
20 Dygraph.endPan(event, g, context);
21 } else if (context.isZooming) {
22 Dygraph.endZoom(event, g, context);
23 }
24 }
25
26 function dblClickV3(event, g, context) {
27 if (event.ctrlKey) {
28 zoom(g, -(1/8));
29 } else {
30 zoom(g, +.1);
31 }
32 }
33
34 function scrollV3(event, g, context) {
35 var normal = event.detail ? event.detail * -1 : event.wheelDelta / 40;
36 // For me the normalized value shows 0.075 for one click. If I took
37 // that verbatim, it would be a 7.5%. I think I'm gonna take 1/10 of that.
38 // (double for left and right side)
39 var percentage = normal / 100;
40
41 zoom(g, percentage);
42 Dygraph.cancelEvent(event);
43 }
44
45 function zoom(g, percentage) {
46 // Adjusts [x, y] toward each other by percentage%
47 function adjustAxis(axis, percentage) {
48 var delta = axis[1] - axis[0];
49 var increment = delta * percentage;
50 return [ axis[0] + increment, axis[1] - increment ];
51 }
52 var yAxes = g.yAxisRanges();
53 var newYAxes = [];
54 for (var i = 0; i < yAxes.length; i++) {
55 newYAxes[i] = adjustAxis(yAxes[i], percentage);
56 }
57
58 g.updateOptions({
59 dateWindow: adjustAxis(g.xAxisRange(), percentage),
60 valueRange: newYAxes[0]
61 });
62 }
63
64 var v4Active = false;
65 var v4Canvas = null;
66
67 function downV4(event, g, context) {
68 context.initializeMouseDown(event, g, context);
69 v4Active = true;
70 moveV4(event, g, context); // in case the mouse went down on a data point.
71 }
72
73 var processed = [];
74
75 function moveV4(event, g, context) {
76 var RANGE = 7;
77
78 if (v4Active) {
79 var canvasx = Dygraph.pageX(event) - Dygraph.findPosX(g.graphDiv);
80 var canvasy = Dygraph.pageY(event) - Dygraph.findPosY(g.graphDiv);
81
82 var rows = g.numRows();
83 // Row layout:
84 // [date, [val1, stdev1], [val2, stdev2]]
85 for (var row = 0; row < rows; row++) {
86 var date = g.getValue(row, 0);
87 var x = g.toDomCoords(date, null)[0];
88 var diff = Math.abs(canvasx - x);
89 if (diff < RANGE) {
90 for (var col = 1; col < 3; col++) {
91 // TODO(konigsberg): these will throw exceptions as data is removed.
92 var vals = g.getValue(row, col);
93 if (vals == null) { continue; }
94 var val = vals[0];
95 var y = g.toDomCoords(null, val)[1];
96 var diff2 = Math.abs(canvasy - y);
97 if (diff2 < RANGE) {
98 var found = false;
99 for (var i in processed) {
100 var stored = processed[i];
101 if(stored[0] == row && stored[1] == col) {
102 found = true;
103 break;
104 }
105 }
106 if (!found) {
107 processed.push([row, col]);
108 drawV4(x, y);
109 }
110 return;
111 }
112 }
113 // drawV4(false, canvasx, canvasy);
114 }
115 }
116 // drawV4(false, canvasx, canvasy);
117 }
118 }
119
120 function upV4(event, g, context) {
121 if (v4Active) {
122 v4Active = false;
123 }
124 }
125
126 function dblClickV4(event, g, context) {
127 unzoomGraph(g4);
128 }
129
130 function drawV4(x, y) {
131 var ctx = v4Canvas;
132
133 ctx.strokeStyle = "#000000";
134 ctx.fillStyle = "#FFFF00";
135 ctx.beginPath();
136 ctx.arc(x,y,5,0,Math.PI*2,true);
137 ctx.closePath();
138 ctx.stroke();
139 ctx.fill();
140 }
141
142 function captureCanvas(canvas, area, g) {
143 v4Canvas = canvas;
144 }
145
146 function unzoomGraph(g) {
147 g.updateOptions({
148 dateWindow: null,
149 valueRange: null
150 });
151 }