-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-calc.js
53 lines (51 loc) · 1.46 KB
/
graph-calc.js
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
function getEventX(event) {
var posx = 0;
if (event.pageX || event.pageY) {
posx = event.pageX;
}
else if (event.clientX || event.clientY) {
posx = event.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
}
return posx;
}
function getOffset(elem) {
var box = (typeof elem.getBoundingClientRect !== 'undefined') ?
elem.getBoundingClientRect() : {top: 0, left: 0};
var w = window, d = elem.ownerDocument.documentElement;
return {
top: box.top + (w.pageYOffset || d.scrollTop) - (d.clientTop || 0),
left: box.left + (w.pageXOffset || d.scrollLeft) - (d.clientLeft || 0)
};
}
function getElementX(obj) {
return Math.round(getOffset(obj).left);
}
function zeroPad(str,len) {
var i;
var pad = "";
var s = str.toString();
for(i=s.length; i < len; i++) {
pad = "0".toString() + pad.toString();
}
return pad.toString() + s.toString();
}
function dateToTimestamp(date) {
return date.getFullYear() +
zeroPad(date.getMonth()+1,2) +
zeroPad(date.getDay()+1,2) +
zeroPad(date.getHours(),2) +
zeroPad(date.getMinutes(),2) +
zeroPad(date.getSeconds(),2);
}
function calcTimestamp(event,element,firstMS,lastMS) {
var eventX = getEventX(event);
var elementX = getElementX(element);
var elementWidth = element.width;
var msWidth = lastMS - firstMS;
var x = eventX - elementX;
var pct = x / elementWidth;
var pctDate = pct * msWidth;
var date = pctDate + firstMS;
return dateToTimestamp(new Date(date));
}