4 <meta http-equiv=
"X-UA-Compatible" content=
"IE=EmulateIE7; IE=EmulateIE9">
5 <title>Dygraphs Drawing Demo
</title>
7 <script type=
"text/javascript" src=
"../excanvas.js"></script>
10 For production (minified) code, use:
11 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
13 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
16 <script type=
"text/javascript">
17 var start_date = new Date(
"2002/12/29").getTime();
18 var end_date = new Date().getTime();
20 for (var d = start_date; d < end_date; d +=
604800 *
1000) {
21 var millis = d +
2 *
3600 *
1000;
22 data.push( [ new Date(new Date(millis).strftime(
"%Y/%m/%d")),
50 ]);
25 <style type=
"text/css">
27 background: url('drawing/tool-palette.png');
28 background-position:
0px
0px;
32 display: inline-block;
35 background: url('drawing/tool-palette.png');
36 background-position: -
32px
0px;
39 display: inline-block;
42 background: url('drawing/tool-palette.png');
43 background-position: -
64px
0px;
46 display: inline-block;
49 display: inline-block;
54 <h2>Time Series Drawing Demo
</h2>
56 <div id=
"tool_zoom" onClick='change_tool(this)'
></div><div id=
"tool_pencil" onClick='change_tool(this)'
></div><div id=
"tool_eraser" onClick='change_tool(this)'
></div>
58 <div id=
"draw_div" style=
"width: 800px; height: 400px;"></div>
60 <script type=
"text/javascript">
61 var isDrawing = false;
62 var lastDrawRow = null, lastDrawValue = null;
64 var valueRange = [
0,
100];
66 function setPoint(event, g, context) {
67 var canvasx = Dygraph.pageX(event) - Dygraph.findPosX(g.graphDiv);
68 var canvasy = Dygraph.pageY(event) - Dygraph.findPosY(g.graphDiv);
69 var xy = g.toDataCoords(canvasx, canvasy);
70 var x = xy[
0], value = xy[
1];
71 var rows = g.numRows();
73 var smallest_diff = -
1;
74 // TODO(danvk): binary search
75 for (var row =
0; row < rows; row++) {
76 var date = g.getValue(row,
0); // millis
77 var diff = Math.abs(date - x);
78 if (smallest_diff <
0 || diff < smallest_diff) {
84 if (closest_row != -
1) {
85 if (lastDrawRow === null) {
86 lastDrawRow = closest_row;
87 lastDrawValue = value;
89 var coeff = (value - lastDrawValue) / (closest_row - lastDrawRow);
90 if (closest_row == lastDrawRow) coeff =
0.0;
91 var minRow = Math.min(lastDrawRow, closest_row);
92 var maxRow = Math.max(lastDrawRow, closest_row);
93 for (var row = minRow; row <= maxRow; row++) {
94 if (tool == 'pencil') {
95 var val = lastDrawValue + coeff * (row - lastDrawRow);
96 val = Math.max(valueRange[
0], Math.min(val, valueRange[
1]));
98 if (val == null || isNaN(val)) console.log(val);
99 } else if (tool == 'eraser') {
103 lastDrawRow = closest_row;
104 lastDrawValue = value;
105 g.updateOptions({ file: data });
106 g.setSelection(closest_row); // prevents the dot from being finnicky.
110 function finishDraw() {
113 lastDrawValue = null;
116 function change_tool(tool_div) {
117 var ids = ['tool_zoom', 'tool_pencil', 'tool_eraser'];
118 for (var i =
0; i < ids.length; i++) {
119 var div = document.getElementById(ids[i]);
120 if (div == tool_div) {
121 div.style.backgroundPosition = -(i *
32) + 'px -
32px';
123 div.style.backgroundPosition = -(i *
32) + 'px
0px';
126 tool = tool_div.id.replace('tool_', '');
128 var dg_div = document.getElementById(
"draw_div");
129 if (tool == 'pencil') {
130 dg_div.style.cursor = 'url(drawing/cursor-pencil.png)
2 30, auto';
131 } else if (tool == 'eraser') {
132 dg_div.style.cursor = 'url(drawing/cursor-eraser.png)
10 30, auto';
133 } else if (tool == 'zoom') {
134 dg_div.style.cursor = 'crosshair';
137 change_tool(document.getElementById(
"tool_pencil"));
139 g = new Dygraph(document.getElementById(
"draw_div"), data,
141 valueRange: valueRange,
142 labels: [ 'Date', 'Value' ],
144 mousedown: function (event, g, context) {
145 if (tool == 'zoom') {
146 Dygraph.defaultInteractionModel.mousedown(event, g, context);
148 // prevents mouse drags from selecting page text.
149 if (event.preventDefault) {
150 event.preventDefault(); // Firefox, Chrome, etc.
152 event.returnValue = false; // IE
153 event.cancelBubble = true;
156 setPoint(event, g, context);
159 mousemove: function (event, g, context) {
160 if (tool == 'zoom') {
161 Dygraph.defaultInteractionModel.mousemove(event, g, context);
163 if (!isDrawing) return;
164 setPoint(event, g, context);
167 mouseup: function(event, g, context) {
168 if (tool == 'zoom') {
169 Dygraph.defaultInteractionModel.mouseup(event, g, context);
174 mouseout: function(event, g, context) {
175 if (tool == 'zoom') {
176 Dygraph.defaultInteractionModel.mouseout(event, g, context);
179 dblclick: function(event, g, context) {
180 Dygraph.defaultInteractionModel.dblclick(event, g, context);
182 mousewheel: function(event, g, context) {
183 var normal = event.detail ? event.detail * -
1 : event.wheelDelta /
40;
184 var percentage = normal /
50;
185 var axis = g.xAxisRange();
186 var xOffset = g.toDomCoords(axis[
0], null)[
0];
187 var x = event.offsetX - xOffset;
188 var w = g.toDomCoords(axis[
1], null)[
0] - xOffset;
189 var xPct = w ==
0 ?
0 : (x / w);
191 var delta = axis[
1] - axis[
0];
192 var increment = delta * percentage;
193 var foo = [increment * xPct, increment * (
1 - xPct)];
194 var dateWindow = [ axis[
0] + foo[
0], axis[
1] - foo[
1] ];
197 dateWindow: dateWindow
199 Dygraph.cancelEvent(event);
203 gridLineColor: 'rgb(
196,
196,
196)',
207 window.onmouseup = finishDraw;
210 <p style='font-size:
10pt'
>Toolbar/cursor icons are CC-licensed from
<a
211 href=
"http://www.fatcow.com/free-icons">FatCow
</a>.
</p>