-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.js
70 lines (61 loc) · 1.74 KB
/
plot.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
/** @summary A helper library for plotting graphs */
(function() {
function Plotter(canvas) {
this.cnv = canvas;
this.ctx = canvas.getContext('2d');
}
window.Plotter = Plotter;
Plotter.prototype.PlotFunction = function(func, opts) {
const width = this.cnv.width;
const height = this.cnv.height;
if (opts === undefined)
opts = Object.create(null);
if ('color' in opts)
this.ctx.strokeStyle = opts.color;
this.ctx.beginPath();
for (let i = 0; i < width; i++) {
let v = func(i/width) / 20;
let x = i;
let y = height/2 - v * height/2;
if (i == 0)
this.ctx.moveTo(x, y);
else
this.ctx.lineTo(x, y);
}
this.ctx.stroke();
}
Plotter.prototype.PlotBuffer = function(buf, opts) {
let s = 0, i = 1, l = buf.length, ss = 1;
if (opts === undefined)
opts = Object.create(null);
if ('start' in opts)
s = opts.start;
if ('incr' in opts)
i = opts.incr;
if ('size' in opts)
l = opts.size;
if ('scale' in opts)
ss = opts.scale;
return this.PlotFunction(x => ss*buf[s + i * Math.floor(x*l)], opts);
}
Plotter.prototype.CreateLegend = function() {
return {labels: [], colors: []};
}
Plotter.prototype.RegisterLegendLabel = function(legend, name, color) {
legend.labels.push(name);
const i = legend.colors.length;
legend.colors.push(color);
return i;
}
Plotter.prototype.DrawLegend = function(legend, pos) {
let p = {x: pos.x, y: pos.y};
const l = legend.labels;
const c = legend.colors;
this.ctx.font = "18px Arial";
for (let i = 0; i < l.length; i++) {
this.ctx.fillStyle = c[i];
this.ctx.fillText(l[i], p.x, p.y);
p.y += 20;
}
}
})();