-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_sort_data.html
48 lines (44 loc) · 1.28 KB
/
08_sort_data.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ordenando dados</title>
<link rel="stylesheet" href="css/styles.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
function render(data, comparator) {
d3.select("#chart").selectAll("div.h-bar")
.data(data)
.enter().append("div")
.attr("class", "h-bar")
.append("span");
d3.select("#chart").selectAll("div.h-bar")
.data(data)
.attr("class", "h-bar")
.style("width", function (d) {
return (d.despesa * 10) + "px";
})
.select("span")
.text(function (d) {
return d.categoria;
});
if(comparator)
d3.select("body")
.selectAll("div.h-bar")
.sort(comparator);
}
let comparePorDespesa = function (a, b) {
return a.despesa - b.despesa;
};
let comparePorCategoria = function (a, b) {
return a.categoria < b.categoria?-1:1;
};
d3.json("data/data.json").then(function(json){
render(json, comparePorDespesa);
});
</script>
</body>
</html>