4 <title>Dygraphs Drawing Demo
</title>
6 For production (minified) code, use:
7 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
9 <script type=
"text/javascript" src=
"../dygraph-dev.js"></script>
12 var start_date = new Date(
"2002/12/29").getTime();
13 var end_date = new Date().getTime();
15 for (var d = start_date; d < end_date; d +=
604800 *
1000) {
16 var millis = d +
2 *
3600 *
1000;
17 var date = new Date(millis);
18 var yyyy = date.getFullYear(),
21 data.push( [ new Date(Date.UTC(yyyy, mm, dd)),
50 ]);
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 pos = Dygraph.findPos(g.graphDiv);
68 var canvasx = Dygraph.pageX(event) - pos.x;
69 var canvasy = Dygraph.pageY(event) - pos.y;
70 var xy = g.toDataCoords(canvasx, canvasy);
71 var x = xy[
0], value = xy[
1];
72 var rows = g.numRows();
74 var smallest_diff = -
1;
75 // TODO(danvk): binary search
76 for (var row =
0; row < rows; row++) {
77 var date = g.getValue(row,
0); // millis
78 var diff = Math.abs(date - x);
79 if (smallest_diff <
0 || diff < smallest_diff) {
85 if (closest_row != -
1) {
86 if (lastDrawRow === null) {
87 lastDrawRow = closest_row;
88 lastDrawValue = value;
90 var coeff = (value - lastDrawValue) / (closest_row - lastDrawRow);
91 if (closest_row == lastDrawRow) coeff =
0.0;
92 var minRow = Math.min(lastDrawRow, closest_row);
93 var maxRow = Math.max(lastDrawRow, closest_row);
94 for (var row = minRow; row <= maxRow; row++) {
95 if (tool == 'pencil') {
96 var val = lastDrawValue + coeff * (row - lastDrawRow);
97 val = Math.max(valueRange[
0], Math.min(val, valueRange[
1]));
99 if (val == null || isNaN(val)) console.log(val);
100 } else if (tool == 'eraser') {
104 lastDrawRow = closest_row;
105 lastDrawValue = value;
106 g.updateOptions({ file: data });
107 g.setSelection(closest_row); // prevents the dot from being finnicky.
111 function finishDraw() {
114 lastDrawValue = null;
117 function change_tool(tool_div) {
118 var ids = ['tool_zoom', 'tool_pencil', 'tool_eraser'];
119 for (var i =
0; i < ids.length; i++) {
120 var div = document.getElementById(ids[i]);
121 if (div == tool_div) {
122 div.style.backgroundPosition = -(i *
32) + 'px -
32px';
124 div.style.backgroundPosition = -(i *
32) + 'px
0px';
127 tool = tool_div.id.replace('tool_', '');
129 var dg_div = document.getElementById(
"draw_div");
130 if (tool == 'pencil') {
131 dg_div.style.cursor = 'url(drawing/cursor-pencil.png)
2 30, auto';
132 } else if (tool == 'eraser') {
133 dg_div.style.cursor = 'url(drawing/cursor-eraser.png)
10 30, auto';
134 } else if (tool == 'zoom') {
135 dg_div.style.cursor = 'crosshair';
138 change_tool(document.getElementById(
"tool_pencil"));
140 g = new Dygraph(document.getElementById(
"draw_div"), data,
142 valueRange: valueRange,
143 labels: [ 'Date', 'Value' ],
146 mousedown: function (event, g, context) {
147 if (tool == 'zoom') {
148 Dygraph.defaultInteractionModel.mousedown(event, g, context);
150 // prevents mouse drags from selecting page text.
151 if (event.preventDefault) {
152 event.preventDefault(); // Firefox, Chrome, etc.
154 event.returnValue = false; // IE
155 event.cancelBubble = true;
158 setPoint(event, g, context);
161 mousemove: function (event, g, context) {
162 if (tool == 'zoom') {
164 if (!isDrawing) return;
165 setPoint(event, g, context);
168 mouseup: function(event, g, context) {
169 if (tool == 'zoom') {
174 mouseout: function(event, g, context) {
175 if (tool == 'zoom') {
178 dblclick: function(event, g, context) {
179 Dygraph.defaultInteractionModel.dblclick(event, g, context);
181 mousewheel: function(event, g, context) {
182 var normal = event.detail ? event.detail * -
1 : event.wheelDelta /
40;
183 var percentage = normal /
50;
184 var axis = g.xAxisRange();
185 var xOffset = g.toDomCoords(axis[
0], null)[
0];
186 var x = event.offsetX - xOffset;
187 var w = g.toDomCoords(axis[
1], null)[
0] - xOffset;
188 var xPct = w ==
0 ?
0 : (x / w);
190 var delta = axis[
1] - axis[
0];
191 var increment = delta * percentage;
192 var foo = [increment * xPct, increment * (
1 - xPct)];
193 var dateWindow = [ axis[
0] + foo[
0], axis[
1] - foo[
1] ];
196 dateWindow: dateWindow
198 Dygraph.cancelEvent(event);
202 gridLineColor: 'rgb(
196,
196,
196)',
210 window.onmouseup = finishDraw;
213 <p style='font-size:
10pt'
>Toolbar/cursor icons are CC-licensed from
<a
214 href=
"http://www.fatcow.com/free-icons">FatCow
</a>.
</p>