-
Notifications
You must be signed in to change notification settings - Fork 0
/
referentiecode-d3-bar-chart.html
173 lines (142 loc) · 5.88 KB
/
referentiecode-d3-bar-chart.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Visualizing execution trace</title>
<script type="text/javascript" src="d3.js"></script>
<script type="text/javascript" src="js/ParsJson.js"></script>
<script type="text/javascript" src="js/states.js"></script>
<link rel="stylesheet" href="css/mystyles.css">
<style type="text/css">
</style>
</head>
<body>
<div id="outercontainer">
<div id="topinfobar" class="row">
<div class="column column-12"></div>
</div>
<div id="lefthalve" class="column column-6">
<div id="lefttophalve">
</div>
<div id="leftbottomhalve">
</div>
</div>
<div id="righthalve" class="column column-6">
</div>
</div>
<script type="text/javascript">
//Width and height
var w = 600;
var h = 250;
var sortOrder = false;
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 13,
11, 12, 15, 20, 18, 17, 16, 18, 23, 25 ];
var xScale = d3.scaleBand()
.domain(d3.range(dataset.length))
.rangeRound([0, w])
.paddingInner(0.05);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset)])
.range([0, h]);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create bars
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return xScale(i);
})
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", xScale.bandwidth())
.attr("height", function(d) {
return yScale(d);
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * 10) + ")";
})
.on("click", function(d){
//this wil run whenever *any* bar is clicked
console.log(d);
sortBars()
})
.on("mouseover", function(d){
//get this bar's x/y values, then augment for the tooltip
var xPosition = parseFloat(d3.select(this).attr("x")) + xScale.bandwidth() /2;
var yPosition = parseFloat(d3.select(this).attr("y")) + 14;
//update the tooltip position and value
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.select("#value")
.text(d);
//show the tooltip
d3.select("#tooltip").classed("hidden",false);
})
.append("title")
.text(function(d){
return "this value is " + d;
//create the tooltip label
svg.append("text")
.attr("id", "tooltip")
.attr("x", xPosition)
.attr("y", yPosition)
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size","11px")
.attr("font-weight", "bold")
.attr("fill", "black")
.text(d);
})
.on("mouseout", function() {
//Hide the tooltip
d3.select("#tooltip").classed("hidden", true);
});
//Create labels
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d;
})
.attr("text-anchor", "middle")
.attr("x", function(d, i) {
return xScale(i) + xScale.bandwidth() / 2;
})
.attr("y", function(d) {
return h - yScale(d) + 14;
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white");
//create sort function
var sortBars = function(){
//flip value of sortOrder
sortOrder = !sortOrder;
svg.selectAll("rect")
.sort(function(a,b){
if (sortOrder){
return d3.ascending(a,b);
} else {
return d3.descending(a,b);
}
})
.transition()
.delay(function(d,i){
return i * 50;
})
.duration(1000)
.attr("x", function(d,i){
return xScale(i);
});
};
</script>
</body>
</html>