-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
73 lines (61 loc) · 1.92 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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);
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()
that.draw()
});
that.draw();
};
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 })]);
};
BarChart.prototype.draw = function() {
var that = this;
var model = this.model;
var data = model.get("data");
var barSel = d3.select(this.chart).selectAll("rect.bar")
.data(data);
barSel.enter()
.append("rect")
.classed("bar", true)
barSel.exit()
.remove()
barSel
.transition()
.attr({
x: function(d,i) { return that.x(d,i) },
y: function(d,i) { return that.y(d,i) },
width: function(d,i) { return that.width(d,i) },
height: function(d,i) { return that.height(d,i) }
});
barSel.on("click", function(d,i) {
console.log("clicked!", d,i,this);
})
};
BarChart.prototype.x = function(d,i) { return this.xScale(i); };
BarChart.prototype.y = function(d,i) { return this.yScale.range()[1] - this.yScale(d.value); };
BarChart.prototype.width = function(d,i) { return this.xScale.rangeBand()/2; };
BarChart.prototype.height = function(d,i) { return this.yScale(d.value); };