-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_barChart.html
60 lines (56 loc) · 1.75 KB
/
03_barChart.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="js/d3.min.js" charset="utf-8"></script>
</head>
<body>
<h3>Grafico de barras:</h3>
<script type="text/javascript">
var w = 300;
var h = 200;
var padding = 2;
var dataset = [5,10,15,20,25];
// Area de plotagem
var svg = d3.select("body")
.append("svg")
.attr("width",w)
.attr("height",h);
// Barras com tamanhos e cores ligados aos dados
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i){
return i*(w/dataset.length);
})
.attr("y",function(d){
return h-(d/Math.max.apply(null,dataset))*h;
})
.attr("width", w/dataset.length-padding)
.attr("height",function(d){
return (d/Math.max.apply(null,dataset))*h;
})
.attr("fill",function(d){
return "rgb(0,0,"+(d/Math.max.apply(null,dataset))*255+")";
});
// Rótulos posicionados dentro das barras
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d){
return d;
})
.attr("text-anchor","middle")
.attr("x",function(d,i){
return i * w/dataset.length + (w/dataset.length - padding)/2;
})
.attr("y",function(d){
return h-(d/Math.max.apply(null,dataset))*h + 20;
})
.attr("font-family","sans-serif")
.attr("fill","white");
</script>
</body>
</html>