4 name
: 'Time Series Drawing Demo',
5 title
: 'Time Series Drawing Demo',
6 setup
: function(parent
) {
9 "<div id='tool_zoom'></div>",
10 "<div id='tool_pencil'></div>",
11 "<div id='tool_eraser'></div>",
13 "<div id='draw_div' style='width: 600px; height: 300px;'></div>",
14 "<p style='font-size: 10pt'>Toolbar/cursor icons are CC-licensed from ",
15 "<a href='http://www.fatcow.com/free-icons'>FatCow</a>.</p>"].join("\n");
19 var zoom
= document
.getElementById('tool_zoom');
20 zoom
.onclick
= function() { change_tool(zoom
) };
21 var pencil
= document
.getElementById('tool_pencil');
22 pencil
.onclick
= function() { change_tool(pencil
) };
23 var eraser
= document
.getElementById('tool_eraser');
24 eraser
.onclick
= function() { change_tool(eraser
) };
26 var start_date
= new Date("2002/12/29").getTime();
27 var end_date
= new Date().getTime();
29 for (var d
= start_date
; d
< end_date
; d
+= 604800 * 1000) {
30 var millis
= d
+ 2 * 3600 * 1000;
31 data
.push( [ new Date(new Date(millis
).strftime("%Y/%m/%d")), 50 ]);
34 var isDrawing
= false;
35 var lastDrawRow
= null, lastDrawValue
= null;
37 var valueRange
= [0, 100];
39 function setPoint(event
, g
, context
) {
40 var canvasx
= Dygraph
.pageX(event
) - Dygraph
.findPosX(g
.graphDiv
);
41 var canvasy
= Dygraph
.pageY(event
) - Dygraph
.findPosY(g
.graphDiv
);
42 var xy
= g
.toDataCoords(canvasx
, canvasy
);
43 var x
= xy
[0], value
= xy
[1];
44 var rows
= g
.numRows();
46 var smallest_diff
= -1;
47 // TODO(danvk): binary search
48 for (var row
= 0; row
< rows
; row
++) {
49 var date
= g
.getValue(row
, 0); // millis
50 var diff
= Math
.abs(date
- x
);
51 if (smallest_diff
< 0 || diff
< smallest_diff
) {
57 if (closest_row
!= -1) {
58 if (lastDrawRow
=== null) {
59 lastDrawRow
= closest_row
;
60 lastDrawValue
= value
;
62 var coeff
= (value
- lastDrawValue
) / (closest_row
- lastDrawRow
);
63 if (closest_row
== lastDrawRow
) coeff
= 0.0;
64 var minRow
= Math
.min(lastDrawRow
, closest_row
);
65 var maxRow
= Math
.max(lastDrawRow
, closest_row
);
66 for (var row
= minRow
; row
<= maxRow
; row
++) {
67 if (tool
== 'pencil') {
68 var val
= lastDrawValue
+ coeff
* (row
- lastDrawRow
);
69 val
= Math
.max(valueRange
[0], Math
.min(val
, valueRange
[1]));
71 if (val
== null || isNaN(val
)) console
.log(val
);
72 } else if (tool
== 'eraser') {
76 lastDrawRow
= closest_row
;
77 lastDrawValue
= value
;
78 g
.updateOptions({ file
: data
});
79 g
.setSelection(closest_row
); // prevents the dot from being finnicky.
83 function finishDraw() {
89 var change_tool
= function(tool_div
) {
90 var ids
= ['tool_zoom', 'tool_pencil', 'tool_eraser'];
91 for (var i
= 0; i
< ids
.length
; i
++) {
92 var div
= document
.getElementById(ids
[i
]);
93 if (div
== tool_div
) {
94 div
.style
.backgroundPosition
= -(i
* 32) + 'px -32px';
96 div
.style
.backgroundPosition
= -(i
* 32) + 'px 0px';
99 tool
= tool_div
.id
.replace('tool_', '');
101 var dg_div
= document
.getElementById("draw_div");
102 if (tool
== 'pencil') {
103 dg_div
.style
.cursor
= 'url(images/cursor-pencil.png) 2 30, auto';
104 } else if (tool
== 'eraser') {
105 dg_div
.style
.cursor
= 'url(images/cursor-eraser.png) 10 30, auto';
106 } else if (tool
== 'zoom') {
107 dg_div
.style
.cursor
= 'crosshair';
110 change_tool(document
.getElementById("tool_pencil"));
112 g
= new Dygraph(document
.getElementById("draw_div"), data
,
114 valueRange
: valueRange
,
115 labels
: [ 'Date', 'Value' ],
117 mousedown
: function (event
, g
, context
) {
118 if (tool
== 'zoom') {
119 Dygraph
.defaultInteractionModel
.mousedown(event
, g
, context
);
121 // prevents mouse drags from selecting page text.
122 if (event
.preventDefault
) {
123 event
.preventDefault(); // Firefox, Chrome, etc.
125 event
.returnValue
= false; // IE
126 event
.cancelBubble
= true;
129 setPoint(event
, g
, context
);
132 mousemove
: function (event
, g
, context
) {
133 if (tool
== 'zoom') {
134 Dygraph
.defaultInteractionModel
.mousemove(event
, g
, context
);
136 if (!isDrawing
) return;
137 setPoint(event
, g
, context
);
140 mouseup
: function(event
, g
, context
) {
141 if (tool
== 'zoom') {
142 Dygraph
.defaultInteractionModel
.mouseup(event
, g
, context
);
147 mouseout
: function(event
, g
, context
) {
148 if (tool
== 'zoom') {
149 Dygraph
.defaultInteractionModel
.mouseout(event
, g
, context
);
152 dblclick
: function(event
, g
, context
) {
153 Dygraph
.defaultInteractionModel
.dblclick(event
, g
, context
);
155 mousewheel
: function(event
, g
, context
) {
156 var normal
= event
.detail
? event
.detail
* -1 : event
.wheelDelta
/ 40;
157 var percentage
= normal
/ 50;
158 var axis
= g
.xAxisRange();
159 var xOffset
= g
.toDomCoords(axis
[0], null)[0];
160 var x
= event
.offsetX
- xOffset
;
161 var w
= g
.toDomCoords(axis
[1], null)[0] - xOffset
;
162 var xPct
= w
== 0 ? 0 : (x
/ w
);
164 var delta
= axis
[1] - axis
[0];
165 var increment
= delta
* percentage
;
166 var foo
= [increment
* xPct
, increment
* (1 - xPct
)];
167 var dateWindow
= [ axis
[0] + foo
[0], axis
[1] - foo
[1] ];
170 dateWindow
: dateWindow
172 Dygraph
.cancelEvent(event
);
176 gridLineColor
: 'rgb(196, 196, 196)',
180 window
.onmouseup
= finishDraw
;