-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (47 loc) · 1.3 KB
/
index.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
module.exports = BarChart;
function BarChart() {}
BarChart.prototype.view = __dirname;
BarChart.prototype.init = function() {
var model = this.model;
model.setNull("data", []);
model.setNull("width", 200);
model.setNull("height", 100);
model.set("layout", []);
this.transform()
};
BarChart.prototype.create = function() {
var model = this.model;
var that = this;
// changes in values inside the array
model.on("all", "data**", function() {
//console.log("event data:", arguments);
that.transform()
});
};
BarChart.prototype.transform = function() {
var model = this.model;
var that = this;
var data = model.get("data") || [];
var width = model.get("width");
var height = model.get("height");
var yMax = 0;
data.forEach(function(d) { if(d.value > yMax) yMax = d.value });
var barWidth = width / data.length;
var yScale = function(v) {
return v * height / yMax;
};
// update the layout
var layout = data.map(function(d,i) {
return {
x: i * barWidth,
y: height - yScale(d.value),
width: barWidth/2,
height: yScale(d.value)
}
});
// we do more computing in js (setDiffDeep) to avoid extra re-rendering
model.setDiffDeep("layout", layout);
};
BarChart.prototype.clicker = function(d,i,evt,el) {
console.log("clicked!", d,i,el)
};