Made v3 interaction model slightly more powerful. (shift-operate to
[dygraphs.git] / tests / interaction.html
CommitLineData
885c13e4
RK
1
2<html>
3 <head>
4 <title>interaction model</title>
5 <!--[if IE]>
6 <script type="text/javascript" src="../excanvas.js"></script>
7 <![endif]-->
8 <script type="text/javascript" src="../strftime/strftime-min.js"></script>
9 <script type="text/javascript" src="../rgbcolor/rgbcolor.js"></script>
10 <script type="text/javascript" src="../dygraph-canvas.js"></script>
11 <script type="text/javascript" src="../dygraph.js"></script>
12 <script type="text/javascript" src="data.js"></script>
13 </head>
14 <body>
15 <table border='1'>
16 <tr><td>
17 <b>Default interaction model</b>
18 <div id="div_g" style="width:600px; height:300px;"></div>
19 </td><td>Zoom: click-drag<br/>Pan: shift-click-drag<br/>Restore zoom level: double-click<br/>
20 </td></tr>
21 <tr><td>
22 <b>No interaction model</b>
23 <div id="div_g2" style="width:600px; height:300px;"></div>
24 </td><td>Click and drag all you like, it won't do anything!</td></tr>
25 <tr><td>
26 <b>Custom interaction model</b>
27
28 <input type="button" value="Unzoom" onclick="unzoomGraph(g3)">
29 <div id="div_g3" style="width:600px; height:300px;"></div>
30 </td><td>
31 Zoom in: double-click, scroll wheel<br/>
32 Zoom out: ctrl-double-click, scroll wheel<br/>
1f237ed6
RK
33 Standard Zoom: shift-click-drag
34 Standard Pan: click-drag<br/>
885c13e4
RK
35 Restore zoom level: press button<br/>
36 </td></tr>
37 <tr><td>
38 <b>Fun model!</b>
39 <div id="div_g4" style="width:600px; height:300px;"></div>
40 </td><td>
41 Keep the mouse button pressed, and hover over all points
42 to mark them.
43 </td></tr>
44
45 </table>
1f237ed6 46
885c13e4
RK
47 <script type="text/javascript">
48 function downV3(event, g, context) {
49 context.initializeMouseDown(event, g, context);
1f237ed6
RK
50 if (event.altKey || event.shiftKey) {
51 Dygraph.startZoom(event, g, context);
52 } else {
53 Dygraph.startPan(event, g, context);
54 }
885c13e4
RK
55 }
56
57 function moveV3(event, g, context) {
58 if (context.isPanning) {
59 Dygraph.movePan(event, g, context);
1f237ed6
RK
60 } else if (context.isZooming) {
61 Dygraph.moveZoom(event, g, context);
885c13e4
RK
62 }
63 }
64
65 function upV3(event, g, context) {
66 if (context.isPanning) {
67 Dygraph.endPan(event, g, context);
1f237ed6
RK
68 } else if (context.isZooming) {
69 Dygraph.endZoom(event, g, context);
885c13e4
RK
70 }
71 }
72
73 function dblClickV3(event, g, context) {
74 if (event.ctrlKey) {
75 zoom(g, -(1/9));
76 } else {
77 zoom(g, +.1);
78 }
79 }
80
81 function scrollV3(event, g, context) {
82 var normal = event.detail ? event.detail * -1 : event.wheelDelta / 40;
83 // For me the normalized value shows 0.075 for one click. If I took
84 // that verbatim, it would be a 7.5%. I think I'm gonna take 1/10 of that.
85 // (double for left and right side)
86 var percentage = normal / 100;
87
88 zoom(g, percentage);
89 Dygraph.cancelEvent(event);
90 }
91
92 function zoom(g, percentage) {
93 // Adjusts [x, y] toward each other by percentage%
94 function adjustAxis(axis, percentage) {
95 var delta = axis[1] - axis[0];
96 var increment = delta * percentage;
97 return [ axis[0] + increment, axis[1] - increment ];
98 }
99
100 var yAxes = g.yAxisRanges();
101 var newYAxes = [];
102 for (var i = 0; i < yAxes.length; i++) {
103 newYAxes[i] = adjustAxis(yAxes[i], percentage);
104 }
105
106 g.updateOptions({
107 dateWindow: adjustAxis(g.xAxisRange(), percentage),
108 valueRange: newYAxes[0]
109 });
110 }
111
112 var v4Active = false;
113 var v4Canvas = null;
114
115 function downV4(event, g, context) {
116 context.initializeMouseDown(event, g, context);
117 v4Active = true;
118 moveV4(event, g, context); // in case the mouse went down on a data point.
119 }
120
121 var processed = [];
122
123 function moveV4(event, g, context) {
124 var RANGE = 7;
125
126 if (v4Active) {
127 var canvasx = Dygraph.pageX(event) - Dygraph.findPosX(g.graphDiv);
128 var canvasy = Dygraph.pageY(event) - Dygraph.findPosY(g.graphDiv);
129
130 var rows = g.numRows();
131 // Row layout:
132 // [date, [val1, stdev1], [val2, stdev2]]
133 for (var row = 0; row < rows; row++) {
134 var date = g.getValue(row, 0);
135 var x = g.toDomCoords(date, null)[0];
136 var diff = Math.abs(canvasx - x);
137 if (diff < RANGE) {
138 for (var col = 1; col < 3; col++) {
139 // TODO(konigsberg): these will throw exceptions as data is removed.
140 var vals = g.getValue(row, col);
141 if (vals == null) { continue; }
142 var val = vals[0];
143 var y = g.toDomCoords(null, val)[1];
144 var diff2 = Math.abs(canvasy - y);
145 if (diff2 < RANGE) {
146 var found = false;
147 for (var i in processed) {
148 var stored = processed[i];
149 if(stored[0] == row && stored[1] == col) {
150 found = true;
151 break;
152 }
153 }
154 if (!found) {
155 processed.push([row, col]);
156 drawV4(x, y);
157 }
158 return;
159 }
160 }
161 // drawV4(false, canvasx, canvasy);
162 }
163 }
164 // drawV4(false, canvasx, canvasy);
165 }
166 }
167
168 function upV4(event, g, context) {
169 if (v4Active) {
170 v4Active = false;
171 }
172 }
173
174 function dblClickV4(event, g, context) {
175 unzoomGraph(g4);
176 }
177
178 function drawV4(x, y) {
179 var ctx = v4Canvas;
180
181 ctx.strokeStyle = "#000000";
182 ctx.fillStyle = "#FFFF00";
183 ctx.beginPath();
184 ctx.arc(x,y,5,0,Math.PI*2,true);
185 ctx.closePath();
186 ctx.stroke();
187 ctx.fill();
188 }
189
190 function captureCanvas(canvas, area, g) {
191 v4Canvas = canvas;
192 }
193
194 var g = new Dygraph(document.getElementById("div_g"),
195 NoisyData, { errorBars : true });
196 var g2 = new Dygraph(document.getElementById("div_g2"),
197 NoisyData, { errorBars : true, interactionModel : {} });
198 var g3 = new Dygraph(document.getElementById("div_g3"),
199 NoisyData, { errorBars : true, interactionModel : {
200 'mousedown' : downV3,
201 'mousemove' : moveV3,
202 'mouseup' : upV3,
203 'dblclick' : dblClickV3,
204 'mousewheel' : scrollV3
205 }});
206 var g4 = new Dygraph(document.getElementById("div_g4"),
207 NoisyData, { errorBars : true, drawPoints : true, interactionModel : {
208 'mousedown' : downV4,
209 'mousemove' : moveV4,
210 'mouseup' : upV4,
211 'dblclick' : dblClickV4,
212 },
213 underlayCallback : captureCanvas
214 });
215
216 function unzoomGraph(g) {
217 g.updateOptions({
218 dateWindow: null,
219 valueRange: null
220 });
221 }
222 </script>
223
224 </body>
225</html>