forked from ethersex/ethersex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgph.js
92 lines (76 loc) · 2.14 KB
/
gph.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
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
var vboxh = 300;
var vboxw = 400;
var colors = [ "red", "blue", "green", "lime", "purple", "maroon", "navy", "yellow" ];
function initDiagram(graphs, gid, id, tid, min, max) {
var g = new Array();
graphCreateAxis(id, tid, min, max);
for (var i = 0; i < graphs; i++) {
g[i] = new Graph(gid.replace(/#/, i), 40, min, max,
(i < colors.length) ? colors[i] : undefined);
}
return g;
}
function Graph(id, num, min, max, color) {
this.obj = $(id);
this.xmult = vboxw / num;
this.min = min;
this.max = max;
this.last_y = -1;
this.elements = 0;
this.color = (color) ? color : undefined;
this.append = function(val) { return graphAppend(this, val); }
if (color)
this.obj.setAttribute("stroke", color);
return this;
}
function addLine(obj, x1, y1, x2, y2) {
var ne = document.createElementNS("http://www.w3.org/2000/svg", "line");
ne.setAttribute("x1", x1);
ne.setAttribute("x2", x2);
ne.setAttribute("y1", y1);
ne.setAttribute("y2", y2);
obj.appendChild(ne);
return ne;
}
function addText(obj, x, y, text) {
var ne = document.createElementNS("http://www.w3.org/2000/svg", "text");
ne.setAttribute("x", x);
ne.setAttribute("y", y);
var t = document.createTextNode(text);
ne.appendChild(t);
obj.appendChild(ne);
return ne;
}
function graphAppend(g, val) {
val = vboxh - ((val - g.min) * vboxh / (g.max - g.min));
if (g.last_y != -1) {
var w = (g.elements + 1) * g.xmult;
addLine(g.obj, g.elements * g.xmult, g.last_y, w, val);
g.elements++;
if (w > vboxw) {
g.obj.removeChild(g.obj.getElementsByTagName("line")[0]);
g.obj.setAttribute("transform", "translate(-" + (w - vboxw) + ",0)");
}
}
g.last_y = val;
return g;
}
function graphCreateAxis(id, tid, min, max) {
var obj = $(id);
var tobj = $(tid);
var x;
for (x = 0; x <= vboxw; x += vboxw / 4)
addLine(obj, x, 0, x, vboxh);
var scale = 10;
for ( ; (max - min) / scale > 10; scale *= 2);
var i = min;
if (i % scale)
i += scale - (min % scale);
for ( ; i <= max; i += scale) {
var y = vboxh - ((i - min) * vboxh / (max - min));
addLine(obj, 0, y, vboxw, y);
addText(tobj, 0, y, i);
}
addLine(obj, 0, 0, vboxw, 0);
addLine(obj, 0, vboxh, vboxw, vboxh);
}