-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbar_di.html~
123 lines (106 loc) · 2.72 KB
/
bar_di.html~
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Page Template</title>
<script type="text/javascript" src="d3/d3.js"></script>
<style>
div.bar {
display: inline-block;
width: 20px;
height: 75px; /* We'll override height later */
background-color: teal;
margin-right: 2px;
}
rect:hover {
fill: orange;
}
</style>
</head>
<body>
<script type="text/javascript">
var dataset;
d3.csv("test_di.csv", function(d){
dataset = d;
generateVis(d);
});
function generateVis(d){
//declare the height and width of the element
var w = 400;
var h = 300;
var barPadding = 1;
var xScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d){
return +d.Validated;})
])
.range([0, w]);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("y", function(d,i){
return i*(h/dataset.length); //i, the second arg of the function, represents the order of the element passed.
})
//.attr("x", function(d){
//return w - 40*d.Validated;
//})
.attr("height", h/dataset.length - barPadding)
.attr("width", function(d,i){
return xScale(d.Validated) + "px";
})
.attr("fill",function(d,i){
return "rgb(0,0,"+d.Validated*30+")";})
.on("click",function(d){
sortBars();
})
.append("title")
.text(function(d){
return "Validated by: " + d["User id"];
});
var sortBars = function() {
svg.selectAll("rect")
.sort(function(a, b) {
return d3.descending(+a.Validated, +b.Validated);
})
.transition()
.duration(1000)
.attr("y", function(d, i) {
return i*(h/dataset.length);
});
svg.selectAll("text")
.sort(function(a, b) {
return d3.descending(+a.Validated, +b.Validated);
})
.transition()
.duration(1000)
.attr("y", function(d, i) {
return (i+1)*(h/dataset.length)-15*barPadding;
});
};
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.style("pointer-events","none")
.text(function(d){
return d.Validated;
})
.attr("y", function(d, i) {
return (i+1)*(h/dataset.length)-15*barPadding;
})
.attr("x", function(d) {
return xScale(d.Validated) -15;
})
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "white")
.attr("text-anchor", "middle");
};
</script>
</body>
</html>