-
Notifications
You must be signed in to change notification settings - Fork 2
/
pie.html
68 lines (54 loc) · 1.55 KB
/
pie.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.arc text {
font: 40px Helvetica;
text-anchor: middle;
}
.arc path {
stroke: #fff;
}
</style>
<body>
<script src="./js/d3.v3.min.js"></script>
<script>
var width = 800,
height = 600,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#23ccc4", "#97e04c", "#fdb62e"]);
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var labelArc = d3.svg.arc()
.outerRadius(radius - 40)
.innerRadius(radius - 40);
var pie = d3.layout.pie()
.sort(null)
.value(function (d) { return d.value; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
const data = JSON.parse(`[
{"label": "Beginners", "value": 50},
{"label": "Devs, JS", "value": 20},
{"label": "Devs, no JS", "value": 30}
]`);
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 color(d.data.label); });
g.append("text")
.attr("transform", function (d) { return "translate(" + labelArc.centroid(d) + ")"; })
.attr("dy", ".35em")
.text(function (d) { return d.data.label; });
function type(d) {
d.value = +d.value;
return d;
}
</script>