1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Dygraphs Drawing Demo</title>
<link rel="stylesheet" type="text/css" href="../dist/dygraph.css" />
<link rel="stylesheet" type="text/css" href="../common/vextlnk.css" />
<script type="text/javascript" src="../dist/dygraph.js"></script>
<script type="text/javascript"><!--//--><![CDATA[//><!--
var start_date = new Date("2002/12/29").getTime();
var end_date = new Date().getTime();
data = [];
for (var d = start_date; d < end_date; d += 604800 * 1000) {
var millis = d + 2 * 3600 * 1000;
var date = new Date(millis);
var yyyy = date.getFullYear(),
mm = date.getMonth(),
dd = date.getDate();
data.push( [ new Date(Date.UTC(yyyy, mm, dd)), 50 ]);
}
//--><!]]></script>
<style>
#tool_zoom {
background: url('../common/tool-palette.png');
background-position: 0px 0px;
width: 32px;
height: 33px;
margin-left: 50px;
display: inline-block;
}
#tool_pencil {
background: url('../common/tool-palette.png');
background-position: -32px 0px;
width: 32px;
height: 33px;
display: inline-block;
}
#tool_eraser {
background: url('../common/tool-palette.png');
background-position: -64px 0px;
width: 33px;
height: 33px;
display: inline-block;
}
#toolbar {
display: inline-block;
}
</style>
</head>
<body>
<h2>Time Series Drawing Demo</h2>
<div id='toolbar'>
<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>
</div>
<div id="draw_div" style="width: 800px; height: 400px;"></div>
<script type="text/javascript"><!--//--><![CDATA[//><!--
Dygraph.onDOMready(function onDOMready() {
var isDrawing = false;
var lastDrawRow = null, lastDrawValue = null;
var tool = 'pencil';
var valueRange = [0, 100];
function setPoint(event, g, context) {
var pos = Dygraph.findPos(g.graphDiv);
var canvasx = Dygraph.pageX(event) - pos.x;
var canvasy = Dygraph.pageY(event) - pos.y;
var xy = g.toDataCoords(canvasx, canvasy);
var x = xy[0], value = xy[1];
var rows = g.numRows();
var closest_row = -1;
var smallest_diff = -1;
// TODO(danvk): binary search
for (var row = 0; row < rows; row++) {
var date = g.getValue(row, 0); // millis
var diff = Math.abs(date - x);
if (smallest_diff < 0 || diff < smallest_diff) {
smallest_diff = diff;
closest_row = row;
}
}
if (closest_row != -1) {
if (lastDrawRow === null) {
lastDrawRow = closest_row;
lastDrawValue = value;
}
var coeff = (value - lastDrawValue) / (closest_row - lastDrawRow);
if (closest_row == lastDrawRow) coeff = 0.0;
var minRow = Math.min(lastDrawRow, closest_row);
var maxRow = Math.max(lastDrawRow, closest_row);
for (var row = minRow; row <= maxRow; row++) {
if (tool == 'pencil') {
var val = lastDrawValue + coeff * (row - lastDrawRow);
val = Math.max(valueRange[0], Math.min(val, valueRange[1]));
data[row][1] = val;
if (val == null || isNaN(val)) console.log(val);
} else if (tool == 'eraser') {
data[row][1] = null;
}
}
lastDrawRow = closest_row;
lastDrawValue = value;
g.updateOptions({ file: data });
g.setSelection(closest_row); // prevents the dot from being finnicky.
}
}
function finishDraw() {
isDrawing = false;
lastDrawRow = null;
lastDrawValue = null;
}
change_tool = function change_tool(tool_div) {
var ids = ['tool_zoom', 'tool_pencil', 'tool_eraser'];
for (var i = 0; i < ids.length; i++) {
var div = document.getElementById(ids[i]);
if (div == tool_div) {
div.style.backgroundPosition = -(i * 32) + 'px -32px';
} else {
div.style.backgroundPosition = -(i * 32) + 'px 0px';
}
}
tool = tool_div.id.replace('tool_', '');
var dg_div = document.getElementById("draw_div");
if (tool == 'pencil') {
dg_div.style.cursor = 'url(../common/cursor-pencil.png) 2 30, auto';
} else if (tool == 'eraser') {
dg_div.style.cursor = 'url(../common/cursor-eraser.png) 10 30, auto';
} else if (tool == 'zoom') {
dg_div.style.cursor = 'crosshair';
}
}
change_tool(document.getElementById("tool_pencil"));
g = new Dygraph(document.getElementById("draw_div"), data,
{
valueRange: valueRange,
labels: [ 'Date', 'Value' ],
labelsUTC: true,
interactionModel: {
// the next line is required when using the
// defaultInteractionModel mousedown function.
willDestroyContextMyself: true,
mousedown: function (event, g, context) {
if (tool == 'zoom') {
Dygraph.defaultInteractionModel.mousedown(event, g, context);
} else {
event.preventDefault(); // Firefox, Chrome, etc.
isDrawing = true;
setPoint(event, g, context);
}
},
mousemove: function (event, g, context) {
// note that the defaultInteractionModel dynamically binds
// its own mousemove event inside the mousedown handler
if (tool != 'zoom') {
if (!isDrawing) return;
setPoint(event, g, context);
}
},
mouseup: function(event, g, context) {
// note that the defaultInteractionModel dynamically binds
// its own mouseup event inside the mousedown handler
if (tool != 'zoom') {
finishDraw();
}
},
mouseout: function(event, g, context) {
// note that the defaultInteractionModel does not use
// the mouseout event, instead it detects when the mouse
// is outside the chart using a dynamically bound
// mousemove event
},
dblclick: function(event, g, context) {
Dygraph.defaultInteractionModel.dblclick(event, g, context);
},
mousewheel: function(event, g, context) {
var normal = event.detail ? event.detail * -1 : event.wheelDelta / 40;
var percentage = normal / 50;
var axis = g.xAxisRange();
var xOffset = g.toDomCoords(axis[0], null)[0];
var x = event.offsetX - xOffset;
var w = g.toDomCoords(axis[1], null)[0] - xOffset;
var xPct = w == 0 ? 0 : (x / w);
var delta = axis[1] - axis[0];
var increment = delta * percentage;
var foo = [increment * xPct, increment * (1 - xPct)];
var dateWindow = [ axis[0] + foo[0], axis[1] - foo[1] ];
g.updateOptions({
dateWindow: dateWindow
});
event.preventDefault();
}
},
strokeWidth: 1.5,
gridLineColor: 'rgb(196, 196, 196)',
axes: {
y: {
drawAxis: false,
drawGrid: false
}
}
});
window.onmouseup = finishDraw;
});
//--><!]]></script>
</body>
</html>
|