4 <link rel=
"stylesheet" href=
"../css/dygraph.css">
5 <title>Dygraphs Drawing Demo
</title>
7 For production (minified) code, use:
8 <script type=
"text/javascript" src=
"dygraph-combined.js"></script>
10 <script type=
"text/javascript" src=
"../dist/dygraph.js"></script>
13 var start_date = new Date(
"2002/12/29").getTime();
14 var end_date = new Date().getTime();
16 for (var d = start_date; d < end_date; d +=
604800 *
1000) {
17 var millis = d +
2 *
3600 *
1000;
18 var date = new Date(millis);
19 var yyyy = date.getFullYear(),
22 data.push( [ new Date(Date.UTC(yyyy, mm, dd)),
50 ]);
28 background: url('drawing/tool-palette.png');
29 background-position:
0px
0px;
33 display: inline-block;
36 background: url('drawing/tool-palette.png');
37 background-position: -
32px
0px;
40 display: inline-block;
43 background: url('drawing/tool-palette.png');
44 background-position: -
64px
0px;
47 display: inline-block;
50 display: inline-block;
55 <h2>Time Series Drawing Demo
</h2>
57 <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>
59 <div id=
"draw_div" style=
"width: 800px; height: 400px;"></div>
61 <script type=
"text/javascript">
62 var isDrawing = false;
63 var lastDrawRow = null, lastDrawValue = null;
65 var valueRange = [
0,
100];
67 function setPoint(event, g, context) {
68 var pos = Dygraph.findPos(g.graphDiv);
69 var canvasx = Dygraph.pageX(event) - pos.x;
70 var canvasy = Dygraph.pageY(event) - pos.y;
71 var xy = g.toDataCoords(canvasx, canvasy);
72 var x = xy[
0], value = xy[
1];
73 var rows = g.numRows();
75 var smallest_diff = -
1;
76 // TODO(danvk): binary search
77 for (var row =
0; row < rows; row++) {
78 var date = g.getValue(row,
0); // millis
79 var diff = Math.abs(date - x);
80 if (smallest_diff <
0 || diff < smallest_diff) {
86 if (closest_row != -
1) {
87 if (lastDrawRow === null) {
88 lastDrawRow = closest_row;
89 lastDrawValue = value;
91 var coeff = (value - lastDrawValue) / (closest_row - lastDrawRow);
92 if (closest_row == lastDrawRow) coeff =
0.0;
93 var minRow = Math.min(lastDrawRow, closest_row);
94 var maxRow = Math.max(lastDrawRow, closest_row);
95 for (var row = minRow; row <= maxRow; row++) {
96 if (tool == 'pencil') {
97 var val = lastDrawValue + coeff * (row - lastDrawRow);
98 val = Math.max(valueRange[
0], Math.min(val, valueRange[
1]));
100 if (val == null || isNaN(val)) console.log(val);
101 } else if (tool == 'eraser') {
105 lastDrawRow = closest_row;
106 lastDrawValue = value;
107 g.updateOptions({ file: data });
108 g.setSelection(closest_row); // prevents the dot from being finnicky.
112 function finishDraw() {
115 lastDrawValue = null;
118 function change_tool(tool_div) {
119 var ids = ['tool_zoom', 'tool_pencil', 'tool_eraser'];
120 for (var i =
0; i < ids.length; i++) {
121 var div = document.getElementById(ids[i]);
122 if (div == tool_div) {
123 div.style.backgroundPosition = -(i *
32) + 'px -
32px';
125 div.style.backgroundPosition = -(i *
32) + 'px
0px';
128 tool = tool_div.id.replace('tool_', '');
130 var dg_div = document.getElementById(
"draw_div");
131 if (tool == 'pencil') {
132 dg_div.style.cursor = 'url(drawing/cursor-pencil.png)
2 30, auto';
133 } else if (tool == 'eraser') {
134 dg_div.style.cursor = 'url(drawing/cursor-eraser.png)
10 30, auto';
135 } else if (tool == 'zoom') {
136 dg_div.style.cursor = 'crosshair';
139 change_tool(document.getElementById(
"tool_pencil"));
141 g = new Dygraph(document.getElementById(
"draw_div"), data,
143 valueRange: valueRange,
144 labels: [ 'Date', 'Value' ],
147 mousedown: function (event, g, context) {
148 if (tool == 'zoom') {
149 Dygraph.defaultInteractionModel.mousedown(event, g, context);
151 event.preventDefault(); // Firefox, Chrome, etc.
153 setPoint(event, g, context);
156 mousemove: function (event, g, context) {
157 if (tool == 'zoom') {
159 if (!isDrawing) return;
160 setPoint(event, g, context);
163 mouseup: function(event, g, context) {
164 if (tool == 'zoom') {
169 mouseout: function(event, g, context) {
170 if (tool == 'zoom') {
173 dblclick: function(event, g, context) {
174 Dygraph.defaultInteractionModel.dblclick(event, g, context);
176 mousewheel: function(event, g, context) {
177 var normal = event.detail ? event.detail * -
1 : event.wheelDelta /
40;
178 var percentage = normal /
50;
179 var axis = g.xAxisRange();
180 var xOffset = g.toDomCoords(axis[
0], null)[
0];
181 var x = event.offsetX - xOffset;
182 var w = g.toDomCoords(axis[
1], null)[
0] - xOffset;
183 var xPct = w ==
0 ?
0 : (x / w);
185 var delta = axis[
1] - axis[
0];
186 var increment = delta * percentage;
187 var foo = [increment * xPct, increment * (
1 - xPct)];
188 var dateWindow = [ axis[
0] + foo[
0], axis[
1] - foo[
1] ];
191 dateWindow: dateWindow
193 event.preventDefault();
197 gridLineColor: 'rgb(
196,
196,
196)',
205 window.onmouseup = finishDraw;
208 <p style='font-size:
10pt'
>Toolbar/cursor icons are CC-licensed from
<a
209 href=
"http://www.fatcow.com/free-icons">FatCow
</a>.
</p>