-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPie_Chart_D3_Visualization.html
71 lines (63 loc) · 1.66 KB
/
Pie_Chart_D3_Visualization.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
<!DOCTYPE html>
<html>
<head>
<title>Pie Chart</title>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
.slice text {
font-size: 16pt;
font-family: Arial;
}
</style>
</head>
<body>
<div id="chart">
</div>
<script type="text/javascript">
var width = 600,
height = 600,
radius = Math.min(width, height) / 2,
color = d3.scaleOrdinal(d3.schemeCategory20);
var svg = d3.select('#chart')
.append('svg')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')');
var arc = d3.arc()
.innerRadius(0)
.outerRadius(radius);
var data = [
{labels: 'School', value: 6 },
{labels: 'Sleep', value: 8 },
{labels: 'Playing', value: 2},
{labels: 'Study', value: 4 },
{labels: "T.V.", value: 1},
{labels: "Others", value: 3}];
var pie = d3.pie()
.value(function(d) { return d.value; })
.sort(null);
var path = svg.selectAll('path')
.data(pie(data))
.enter()
.append('path')
.attr('d', arc)
.attr('fill', function(d, i){return color(d.data.labels);});
var arcs = svg.selectAll("g.slice")
.data(pie(data))
.enter()
.append("svg:g")
.attr("class", "slice");
arcs.append("svg:text")
.attr("transform", function(d){
d.outerRadius = radius + 50;
d.innerRadius = radius + 45;
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.style("fill", "White")
.style("font", "bold 15px Arial")
.text(function(d, i){return data[i].labels;});
</script>
</body>
</html>