forked from kovach/piksi-console
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.js
75 lines (60 loc) · 1.78 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
71
72
73
74
75
var _ = require('underscore');
// Global refs
var svg;
var dimensions = function() {
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 800 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
return {margin: margin, width: width, height: height};
}
var init = function() {
var dims = dimensions();
svg = d3.select("body").append("svg")
.attr("width", dims.width + dims.margin.left + dims.margin.right)
.attr("height", dims.height + dims.margin.top + dims.margin.bottom)
.append("g")
.attr("transform", "translate(" + dims.margin.left + "," + dims.margin.top + ")");
}
var plot = function(color) {
var plot = this;
var dims = dimensions();
plot.color = color;
plot.x = d3.scale.linear()
.range([0, dims.width]);
plot.y = d3.scale.linear()
.range([dims.height, 0]);
plot.line = d3.svg.line()
.x(function(d) { return plot.x(d.x); })
.y(function(d) { return plot.y(d.y); });
plot.data = [];
plot.path = mkPath(plot);
}
var points = false;
plot.prototype.update = function(pt) {
var plot = this;
var data = plot.data;
data.push(pt);
this.x.domain(d3.extent(data, function(d) { return d.x }));
this.y.domain(d3.extent(data, function(d) { return d.y }));
this.path.datum(data).attr("d", plot.line);
}
//var mkPoints = function(data) {
// return svg.selectAll(".point")
// .data(data)
// .enter().append("circle")
// .attr("class", "point")
// .attr("cx", function(d) { return plot.x(d.x); })
// .attr("cy", function(d) { return plot.y(d.y); })
// .attr("r", 2);
//}
var mkPath = function(plot) {
return svg.append("path")
.datum(plot.data)
.attr("class", "line")
.attr("d", plot.line)
.attr("stroke", plot.color);
}
module.exports = {
init: init,
plot: plot,
}