-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathbar-chart.js
More file actions
89 lines (69 loc) · 2.03 KB
/
bar-chart.js
File metadata and controls
89 lines (69 loc) · 2.03 KB
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
d3.chart("BarChart", {
initialize: function(options) {
options = options || {};
var chart = this;
this.x = d3.scale.linear();
this.y = d3.scale.linear()
.domain([0, 100]);
this.base
.attr("class", "chart");
function onEnter() {
var length = this.data().length;
this.attr("x", function(d, i) { return chart.x(i + 1) - .5; })
.attr("y", function(d) { return chart.h - chart.y(d.value) - .5; })
.attr("width", chart.width() / length)
.attr("height", function(d) { return chart.y(d.value); });
}
function onEnterTrans() {
this.duration(1000)
.attr("x", function(d, i) { return chart.x(i) - .5; });
}
function onTrans() {
this.duration(1000)
.attr("x", function(d, i) { return chart.x(i) - .5; });
}
function onExitTrans() {
this.duration(1000)
.attr("x", function(d, i) { return chart.x(i - 1) - .5; });
}
function dataBind(data) {
return this.selectAll("rect")
.data(data, function(d) { return d.time; });
}
function insert() {
return this.insert("rect", "line");
}
this.layer("bars", this.base.append("g"), {
dataBind: dataBind,
insert: insert
});
this.layer("bars").on("enter", onEnter);
this.layer("bars").on("enter:transition", onEnterTrans);
this.layer("bars").on("update:transition", onTrans);
this.layer("bars").on("exit:transition", onExitTrans);
this.width(options.width || 600);
this.height(options.height || 80);
},
width: function(newWidth) {
if (!arguments.length) {
return this.w;
}
this.w = newWidth;
this.x.range([0, this.w]);
this.base.attr("width", this.w);
return this;
},
height: function(newHeight) {
if (!arguments.length) {
return this.h;
}
this.h = newHeight;
this.y.rangeRound([0, this.h]);
this.base.attr("height", this.h);
return this;
},
transform: function(data) {
this.x.domain([0, data.length]);
return data;
}
});