-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (47 loc) · 1.44 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
var d3 = require('d3');
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.yScale = d3.scale.linear()
.range([0, model.get("height")]);
this.xScale = d3.scale.ordinal()
.rangeBands([0, model.get("width")], 0.1);
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") || [];
this.xScale.domain(d3.range(data.length));
// this could be implemented as extent for a relative scale
this.yScale.domain([0, d3.max(data, function(d) { return d.value })]);
// update the layout
var layout = data.map(function(d,i) {
return {
x: that.xScale(i),
y: that.yScale.range()[1] - that.yScale(d.value),
width: that.xScale.rangeBand()/2,
height: that.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)
};