2 // Code for a variety of interaction models. Used in interaction.html, but split out from
3 // that file so they can be tested in isolation.
5 function downV3(event
, g
, context
) {
6 context
.initializeMouseDown(event
, g
, context
);
7 if (event
.altKey
|| event
.shiftKey
) {
8 Dygraph
.startZoom(event
, g
, context
);
10 Dygraph
.startPan(event
, g
, context
);
14 function moveV3(event
, g
, context
) {
15 if (context
.isPanning
) {
16 Dygraph
.movePan(event
, g
, context
);
17 } else if (context
.isZooming
) {
18 Dygraph
.moveZoom(event
, g
, context
);
22 function upV3(event
, g
, context
) {
23 if (context
.isPanning
) {
24 Dygraph
.endPan(event
, g
, context
);
25 } else if (context
.isZooming
) {
26 Dygraph
.endZoom(event
, g
, context
);
30 // Take the offset of a mouse event on the dygraph canvas and
31 // convert it to a pair of percentages from the bottom left.
32 // (Not top left, bottom is where the lower value is.)
33 function offsetToPercentage(g
, offsetX
, offsetY
) {
34 // This is calculating the pixel offset of the leftmost date.
35 var xOffset
= g
.toDomCoords(g
.xAxisRange()[0], null)[0];
36 var yar0
= g
.yAxisRange(0);
38 // This is calculating the pixel of the higest value. (Top pixel)
39 var yOffset
= g
.toDomCoords(null, yar0
[1])[1];
41 // x y w and h are relative to the corner of the drawing area,
42 // so that the upper corner of the drawing area is (0, 0).
43 var x
= offsetX
- xOffset
;
44 var y
= offsetY
- yOffset
;
46 // This is computing the rightmost pixel, effectively defining the
48 var w
= g
.toDomCoords(g
.xAxisRange()[1], null)[0] - xOffset
;
50 // This is computing the lowest pixel, effectively defining the height.
51 var h
= g
.toDomCoords(null, yar0
[0])[1] - yOffset
;
53 // Percentage from the left.
54 var xPct
= w
=== 0 ? 0 : (x
/ w
);
55 // Percentage from the top.
56 var yPct
= h
=== 0 ? 0 : (y
/ h
);
58 // The (1-) part below changes it from "% distance down from the top"
59 // to "% distance up from the bottom".
60 return [xPct
, (1-yPct
)];
63 function dblClickV3(event
, g
, context
) {
64 // Reducing by 20% makes it 80% the original size, which means
65 // to restore to original size it must grow by 25%
67 if (!(event
.offsetX
&& event
.offsetY
)){
68 event
.offsetX
= event
.layerX
- event
.target
.offsetLeft
;
69 event
.offsetY
= event
.layerY
- event
.target
.offsetTop
;
72 var percentages
= offsetToPercentage(g
, event
.offsetX
, event
.offsetY
);
73 var xPct
= percentages
[0];
74 var yPct
= percentages
[1];
77 zoom(g
, -0.25, xPct
, yPct
);
79 zoom(g
, +0.2, xPct
, yPct
);
83 var lastClickedGraph
= null;
85 function clickV3(event
, g
, context
) {
87 Dygraph
.cancelEvent(event
);
90 function scrollV3(event
, g
, context
) {
91 if (lastClickedGraph
!= g
) {
94 var normal
= event
.detail
? event
.detail
* -1 : event
.wheelDelta
/ 40;
95 // For me the normalized value shows 0.075 for one click. If I took
96 // that verbatim, it would be a 7.5%.
97 var percentage
= normal
/ 50;
99 if (!(event
.offsetX
&& event
.offsetY
)){
100 event
.offsetX
= event
.layerX
- event
.target
.offsetLeft
;
101 event
.offsetY
= event
.layerY
- event
.target
.offsetTop
;
104 var percentages
= offsetToPercentage(g
, event
.offsetX
, event
.offsetY
);
105 var xPct
= percentages
[0];
106 var yPct
= percentages
[1];
108 zoom(g
, percentage
, xPct
, yPct
);
109 Dygraph
.cancelEvent(event
);
112 // Adjusts [x, y] toward each other by zoomInPercentage%
113 // Split it so the left/bottom axis gets xBias
/yBias of that change and
114 // tight/top
gets (1-xBias
)/(1-yBias
) of that change
.
116 // If a bias is missing it splits it down the middle.
117 function zoom(g
, zoomInPercentage
, xBias
, yBias
) {
118 xBias
= xBias
|| 0.5;
119 yBias
= yBias
|| 0.5;
120 function adjustAxis(axis
, zoomInPercentage
, bias
) {
121 var delta
= axis
[1] - axis
[0];
122 var increment
= delta
* zoomInPercentage
;
123 var foo
= [increment
* bias
, increment
* (1-bias
)];
124 return [ axis
[0] + foo
[0], axis
[1] - foo
[1] ];
126 var yAxes
= g
.yAxisRanges();
128 for (var i
= 0; i
< yAxes
.length
; i
++) {
129 newYAxes
[i
] = adjustAxis(yAxes
[i
], zoomInPercentage
, yBias
);
133 dateWindow
: adjustAxis(g
.xAxisRange(), zoomInPercentage
, xBias
),
134 valueRange
: newYAxes
[0]
138 var v4Active
= false;
141 function downV4(event
, g
, context
) {
142 context
.initializeMouseDown(event
, g
, context
);
144 moveV4(event
, g
, context
); // in case the mouse went down on a data point.
149 function moveV4(event
, g
, context
) {
153 var graphPos
= Dygraph
.findPos(g
.graphDiv
);
154 var canvasx
= Dygraph
.pageX(event
) - graphPos
.x
;
155 var canvasy
= Dygraph
.pageY(event
) - graphPos
.y
;
157 var rows
= g
.numRows();
159 // [date, [val1, stdev1], [val2, stdev2]]
160 for (var row
= 0; row
< rows
; row
++) {
161 var date
= g
.getValue(row
, 0);
162 var x
= g
.toDomCoords(date
, null)[0];
163 var diff
= Math
.abs(canvasx
- x
);
165 for (var col
= 1; col
< 3; col
++) {
166 // TODO(konigsberg): these will throw exceptions as data is removed.
167 var vals
= g
.getValue(row
, col
);
168 if (vals
=== null || vals
=== undefined
) { continue; }
170 var y
= g
.toDomCoords(null, val
)[1];
171 var diff2
= Math
.abs(canvasy
- y
);
174 for (var i
in processed
) {
175 var stored
= processed
[i
];
176 if(stored
[0] == row
&& stored
[1] == col
) {
182 processed
.push([row
, col
]);
193 function upV4(event
, g
, context
) {
199 function dblClickV4(event
, g
, context
) {
200 restorePositioning(g
);
203 function drawV4(x
, y
) {
206 ctx
.strokeStyle
= "#000000";
207 ctx
.fillStyle
= "#FFFF00";
209 ctx
.arc(x
,y
,5,0,Math
.PI
*2,true);
215 function captureCanvas(canvas
, area
, g
) {
219 function restorePositioning(g
) {