-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortedExample2.html
More file actions
277 lines (227 loc) · 6.06 KB
/
sortedExample2.html
File metadata and controls
277 lines (227 loc) · 6.06 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<title>D3: Ascending and descending sorts</title>
<style type="text/css">
body {
background-color: #000;
}
/* New rules for styling buttons */
#buttonContainer {
margin-bottom: 10px;
}
button {
padding: 15px;
}
svg {
background-color: white;
}
g.bar text {
font-family: Lato;
font-size: 11px;
fill: white;
font-style: bold;
text-anchor: middle;
opacity: 0;
}
g.bar.highlight text {
opacity: 1;
}
/* g.bar.highlight rect { Both this is the one below work fine
fill: yellowgreen;
}*/
rect:hover {
/*fill: #ff89fd;*/
fill: yellowgreen;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
#tooltip {
position: absolute;
width: 100px;
height: auto;
padding: 10px;
background-color: yellow;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 3px;
-webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
font-family: sans-serif;
font-size: 16px;
line-height: 20px;
}
</style>
</head>
<body>
<!-- New buttons to trigger sorting!
use divs to setup buttons -->
<div id="buttonContainer">
<button id="sortAscending">Sort Ascending</button>
<button id="sortDescending">Sort Descending</button>
</div>
<div id="tooltip" class="hidden">
<p><strong>Value = </strong> <span id ="value">100 </span></p>
<!-- <p><span id="value">100</span></p> -->
</div>
<script type="text/javascript">
//Width, height, padding
var w = 800;
var h = 500;
var padding = 25;
//Array of dummy data values
var dataset = [ 5, 8, 13, 19, 21, 22, 31, 26, 25, 23,
16, 17, 15, 14, 11, 3, 6, 8, 3, 5 ];
//Configure x and y scale functions
var xScale = d3.scale.ordinal()
.domain(d3.range(dataset.length))
.rangeRoundBands([ padding, w - padding ], 0.1);
var yScale = d3.scale.linear()
.domain([ 0, d3.max(dataset) ])
.rangeRound([ h - padding, padding ]);
//Configure y axis generator
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create groups
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("class", "bar")
.attr("transform", function(d, i) {
return "translate(" + xScale(i) + ",0)";
});
var rects = groups.append("rect")
.attr("x", 0)
.attr("y", function(d) {
return h - padding;
})
.attr("width", xScale.rangeBand())
.attr("height", 0)
.attr("fill", "dodgerblue")
////////////////
.on("mouseover", function(d, i) {
//Get this bar's x/y values, then augment for the tooltip
var xPosition = parseFloat(d3.select(this).attr("x")) + xScale(dataset.indexOf(d));
var yPosition = parseFloat(d3.select(this).attr("y"));
//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);
})
.on("mouseout", function() {
//Hide the tooltip
d3.select("#tooltip").classed("hidden", true);
});
////////////////
/* .on("mouseover", function() {
d3.select(this)
.classed("highlight", true);
})
.on("mouseout", function() {
d3.select(this)
.classed("highlight", false);
});*/
//Add bar to each group
//.attr("fill", "#ff89fd");
//Add label to each group
/* groups.append("text")
.attr("x", xScale.rangeBand() / 2)
.attr("y", function(d) {
return yScale(d) + 20;
})
.text(function(d) {
return d;
})*/
//Transition rects into place
rects.transition()
.delay(function(d, i) {
return i * 100;
})
.duration(1500)
.attr("y", function(d) {
return yScale(d);
})
.attr("height", function(d) {
return h - padding - yScale(d);
});
/* .filter(function(d) {
if (d < 12) {
return true;
}
return false;
})
.attr("fill", "#00e0f4");*/
//Create y axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.attr("opacity", 0)
.call(yAxis)
.transition()
.delay(2000)
.duration(1500)
.attr("opacity", 1.0);
//Sorting logic
d3.select("#sortAscending")
.on("click", function() {
groups.sort(function(a, b) {
return d3.ascending(a, b);
})
.transition()
.delay(function(d, i) {
return i * 50; // gives it a smoother effect
})
.duration(1000)
.attr("transform", function(d, i) {
dataset = dataset.sort(function(a,b){return a- b})
return "translate(" + xScale(i) + ",0)";
});
});
//This is EXACTLY the same as above, except for:
d3.select("#sortDescending") //DEscending
.on("click", function() {
groups.sort(function(a, b) {
return d3.descending(a, b); //DEscending
})
.transition()
.delay(function(d, i) {
return i * 50;
})
.duration(1000)
.attr("transform", function(d, i) {
dataset = dataset.sort(function(a,b){return b- a})
return "translate(" + xScale(i) + ",0)";
});
});
</script>
</body>
</html>