-
Notifications
You must be signed in to change notification settings - Fork 1
/
donutchart.html
73 lines (58 loc) · 1.82 KB
/
donutchart.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>D3 Pie</title>
<script src="https://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script>
var datos = [
{"candidato":"trump","seguidores":14607000,"color":"#ff4d4d"},
{"candidato":"clinton","seguidores":10946000,"color":"#33CAFF"},
{"candidato":"obama","seguidores":8165463,"color":"#0080ff"}
];
var data = datos;
var width = 500,
height = 350,
radius = Math.min(width, height) / 2;
var total = 0;
for (var i = 0, len = datos.length; i < len; i++) {
total = total + datos[i].seguidores;
}
var arc = d3.svg.arc()
.outerRadius(radius)
.innerRadius(radius - 75);
var pie = d3.layout.pie()
.sort(null)
.value(function (d) {
return ((d.seguidores/total)*100);
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function (d) {
return d.data.color;
});
g.append("text")
.attr("transform", function (d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.attr("fill", "white")
.html(function (d) {
return d.data.candidato + " </br> " + Math.ceil((d.data.seguidores/total)*100) + "%";
});
</script>
</body>
</html>