-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.js
84 lines (65 loc) · 1.48 KB
/
graph.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
function Graph(parent, w, h)
{
this.w = w;
this.h = h;
this.canvas = document.createElement('canvas');
this.canvas.width = w;
this.canvas.height = h;
this.context = this.canvas.getContext('2d');
if (typeof parent == 'string') {
this.parent = document.getElementById(parent);
} else {
this.parent = parent;
}
parent.insertBefore(this.canvas, null);
}
Graph.prototype.setViewport = function (x, y, w, h)
{
this.vx = x;
this.vy = y;
this.vw = w;
this.vh = h;
};
Graph.prototype.convertCoord = function (x, y)
{
var rx = x - this.vx;
var ry = y - this.vy;
/*if (rx < 0 || rx > this.vw || ry < 0 || ry > this.vh) {
return null;
}*/
var px = rx * this.w/this.vw;
var py = this.h - ry * this.h/this.vh;
return [px, py];
};
Graph.prototype.plot = function (xs, ys, c)
{
if (c === undefined) c = '#f00';
this.context.beginPath();
this.context.strokeStyle = c;
var _this = this;
xs.map(function (x, i, n) {
var y = ys[i];
if (i == 0) {
_this.context.moveTo(..._this.convertCoord(x, y));
} else {
_this.context.lineTo(..._this.convertCoord(x, y));
}
});
this.context.stroke();
};
Graph.prototype.clear = function (c)
{
if (c === undefined) c = '#fff';
this.context.fillStyle = c;
this.context.fillRect(0, 0, this.w, this.h);
};
function linspace(from, to, num)
{
if (num === undefined) num = 50;
var step = (to - from) / num;
var arr = [];
for (var v = from, i = 0; i <= num; v += step, i++) {
arr.push(v);
}
return arr;
}