| 1 | Gallery.register( |
| 2 | 'drawing', |
| 3 | { |
| 4 | name: 'Time Series Drawing Demo', |
| 5 | title: 'Time Series Drawing Demo', |
| 6 | setup: function(parent) { |
| 7 | parent.innerHTML = [ |
| 8 | "<div id='toolbar'>", |
| 9 | "<div id='tool_zoom'></div>", |
| 10 | "<div id='tool_pencil'></div>", |
| 11 | "<div id='tool_eraser'></div>", |
| 12 | "</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"); |
| 16 | }, |
| 17 | |
| 18 | run: function() { |
| 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) }; |
| 25 | |
| 26 | var start_date = new Date("2002/12/29").getTime(); |
| 27 | var end_date = new Date().getTime(); |
| 28 | var data = []; |
| 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 ]); |
| 32 | } |
| 33 | |
| 34 | var isDrawing = false; |
| 35 | var lastDrawRow = null, lastDrawValue = null; |
| 36 | var tool = 'pencil'; |
| 37 | var valueRange = [0, 100]; |
| 38 | |
| 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(); |
| 45 | var closest_row = -1; |
| 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) { |
| 52 | smallest_diff = diff; |
| 53 | closest_row = row; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (closest_row != -1) { |
| 58 | if (lastDrawRow === null) { |
| 59 | lastDrawRow = closest_row; |
| 60 | lastDrawValue = value; |
| 61 | } |
| 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])); |
| 70 | data[row][1] = val; |
| 71 | if (val == null || isNaN(val)) console.log(val); |
| 72 | } else if (tool == 'eraser') { |
| 73 | data[row][1] = null; |
| 74 | } |
| 75 | } |
| 76 | lastDrawRow = closest_row; |
| 77 | lastDrawValue = value; |
| 78 | g.updateOptions({ file: data }); |
| 79 | g.setSelection(closest_row); // prevents the dot from being finnicky. |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function finishDraw() { |
| 84 | isDrawing = false; |
| 85 | lastDrawRow = null; |
| 86 | lastDrawValue = null; |
| 87 | } |
| 88 | |
| 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'; |
| 95 | } else { |
| 96 | div.style.backgroundPosition = -(i * 32) + 'px 0px'; |
| 97 | } |
| 98 | } |
| 99 | tool = tool_div.id.replace('tool_', ''); |
| 100 | |
| 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'; |
| 108 | } |
| 109 | } |
| 110 | change_tool(document.getElementById("tool_pencil")); |
| 111 | |
| 112 | g = new Dygraph(document.getElementById("draw_div"), data, |
| 113 | { |
| 114 | valueRange: valueRange, |
| 115 | labels: [ 'Date', 'Value' ], |
| 116 | interactionModel: { |
| 117 | mousedown: function (event, g, context) { |
| 118 | if (tool == 'zoom') { |
| 119 | Dygraph.defaultInteractionModel.mousedown(event, g, context); |
| 120 | } else { |
| 121 | // prevents mouse drags from selecting page text. |
| 122 | if (event.preventDefault) { |
| 123 | event.preventDefault(); // Firefox, Chrome, etc. |
| 124 | } else { |
| 125 | event.returnValue = false; // IE |
| 126 | event.cancelBubble = true; |
| 127 | } |
| 128 | isDrawing = true; |
| 129 | setPoint(event, g, context); |
| 130 | } |
| 131 | }, |
| 132 | mousemove: function (event, g, context) { |
| 133 | if (tool == 'zoom') { |
| 134 | Dygraph.defaultInteractionModel.mousemove(event, g, context); |
| 135 | } else { |
| 136 | if (!isDrawing) return; |
| 137 | setPoint(event, g, context); |
| 138 | } |
| 139 | }, |
| 140 | mouseup: function(event, g, context) { |
| 141 | if (tool == 'zoom') { |
| 142 | Dygraph.defaultInteractionModel.mouseup(event, g, context); |
| 143 | } else { |
| 144 | finishDraw(); |
| 145 | } |
| 146 | }, |
| 147 | mouseout: function(event, g, context) { |
| 148 | if (tool == 'zoom') { |
| 149 | Dygraph.defaultInteractionModel.mouseout(event, g, context); |
| 150 | } |
| 151 | }, |
| 152 | dblclick: function(event, g, context) { |
| 153 | Dygraph.defaultInteractionModel.dblclick(event, g, context); |
| 154 | }, |
| 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); |
| 163 | |
| 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] ]; |
| 168 | |
| 169 | g.updateOptions({ |
| 170 | dateWindow: dateWindow |
| 171 | }); |
| 172 | Dygraph.cancelEvent(event); |
| 173 | } |
| 174 | }, |
| 175 | strokeWidth: 1.5, |
| 176 | gridLineColor: 'rgb(196, 196, 196)', |
| 177 | drawYGrid: false, |
| 178 | drawYAxis: false |
| 179 | }); |
| 180 | window.onmouseup = finishDraw; |
| 181 | } |
| 182 | }); |