-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolbox.js
executable file
·102 lines (92 loc) · 2.82 KB
/
toolbox.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
93
94
95
96
97
98
99
100
101
102
function indexPlot(stocks, names){
var data = [];
var fy = function(d) {
d.price;
}
var fx = function(d) {
d.index;
}
var ft = function() {
data[this.parent.index].ticker;
}
var w = 800;
var h = 500;
var S = pv.max(pv.values(stocks), function(s) {s.values.length});
var idx = Math.floor(S/2) - 1;
var x = pv.Scale.linear(0,S-1).range(0,w);
var y = pv.Scale.linear(-1,5).range(0,h);
var rescale = true;
/* Normalize the data according to an index point. */
var indexify = function(data, cols, idx) {
return cols.map(function(c) {
var v = data[c].values[idx];
return { ticker: c,
values: data[c].values.map(function(d,i) {
return {index:i, price:((d-v)/v)}; })
}
});
};
console.log(stocks);
console.log(names);
/* Compute new index values, rescale if needed, and render. */
var update = function() {
data = indexify(stocks, names, idx);
if (rescale) {
var min = pv.min(data.map(function(d) {pv.min(d.values, fy)}));
var max = pv.max(data.map(function(d) {pv.max(d.values, fy)}));
}
y.domain(min, max).nice();
vis.render();
}
/* The visualization panel. Stores the active index. */
var vis = new pv.Panel()
.def("i", -1)
.left(60)
.right(70)
.top(20.5)
.bottom(18)
.width(w)
.height(h);
/* Horizontal gridlines showing %-change. */
vis.add(pv.Rule)
.data(function() {y.ticks(8)})
.bottom(y)
.strokeStyle(function(d) {d==0 ? "black" : "#cccccc"})
.anchor("left").add(pv.Label)
.text(function(d) {(d * 100).toFixed(0) + "%"});
/* Y-axis label */
vis.add(pv.Label)
.data(["Normalized Value"])
.left(-45)
.bottom(h/2)
.font("10pt Arial")
.textAlign("center")
.textAngle(-Math.PI/2);
/* Stock lines. */
vis.add(pv.Panel)
.data(function() {data})
.add(pv.Line)
.data(function(d) {d.values})
.left(x.by(fx))
.bottom(y.by(fy))
.lineWidth(2)
.add(pv.Label)
.visible(function() {this.index == S-1})
.textBaseline("middle")
.textMargin(6)
.text(ft);
/* Current index line. */
vis.add(pv.Rule)
.visible(function() {idx >= 0 && idx != vis.i()})
.left(function() {x(idx)})
.top(-4)
.bottom(-4)
.strokeStyle("red")
.anchor("bottom").add(pv.Label)
.text(function() {stocks.Date.values[idx]});
/* An invisible bar to capture events (without flickering). */
vis.add(pv.Panel)
.events("all")
.event("mousemove", function() { idx = x.invert(vis.mouse().x) >> 0; update(); });
update();
}