-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarchart.js
84 lines (76 loc) · 1.86 KB
/
barchart.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
var w = 1200;
var h = 200;
var bp = 1;
var gp = 3;
var c = d3.scale.category10();
var svg = d3.select("body")
.append("svg")
.attr("width",w)
.attr("height",h);
queue()
.defer(d3.csv, "asthma.csv")
.defer(d3.tsv, "us-state-names.tsv")
.await(ready);
function ready(error, data, statenames) {
svg.selectAll("rect.Response")
.data(data)
.enter()
.append("rect")
.attr("x",function(d,i){
return i*((w-data.length*gp)/data.length)+i*gp;
})
.attr("y",function(d){
return h-d.Response*1000-12;
})
.attr("width",(w-data.length*gp)/data.length/3-bp)
.attr("height",function(d){
return d.Response*1000;
})
.attr("fill",c(1));
svg.selectAll("rect.Predicted")
.data(data)
.enter()
.append("rect")
.attr("x",function(d,i){
return i*((w-data.length*gp)/data.length)+(w-data.length*gp)/data.length/3+i*gp;
})
.attr("y",function(d){
return h-d.Predicted*1000-12;
})
.attr("width",(w-data.length*gp)/data.length/3-bp)
.attr("height",function(d){
return d.Predicted*1000;
})
.attr("fill",c(2));
svg.selectAll("rect.Difference")
.data(data)
.enter()
.append("rect")
.attr("x",function(d,i){
return i*((w-data.length*gp)/data.length)+2*(w-data.length*gp)/data.length/3+i*gp;
})
.attr("y",function(d){
return h-d.Difference*1000-12;
})
.attr("width",(w-data.length*gp)/data.length/3-bp)
.attr("height",function(d){
return d.Difference*1000;
})
.attr("fill",c(3));
var names = {};
statenames.forEach(function(d){
names[d.id] = d.code;
});
svg.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("x",function(d,i){
return i*((w-data.length*gp)/data.length)+i*gp+bp+(3/2)*(w-data.length*gp)/data.length/3-bp;
})
.attr("y",h)
.text(function(d,i){return names[d.State];})
.attr("font-family","sans-serif")
.attr("font-size","12px")
.attr("text-anchor","middle");
};