-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpieChart.html
140 lines (114 loc) · 3.98 KB
/
pieChart.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/javascript" src="d3/d3.v3.js"></script>
<title>d3 Examples</title>
</head>
<body>
<header>
<div class="container">
<div class="row">
<div class="col-md-2">
<img src="resources/d3logo.png">
</div>
<div class="col-md-10">
<h1 class="logo">D3 Examples</h1>
<h4>Richard Dalton</h4>
<div class="btn-group btn-group-md" role="group" aria-label="...">
<a href="index.html" class="btn btn-default" role="button">Home</a>
<a href="svg.html" class="btn btn-default" role="button">SVG</a>
<a href="barChart.html" class="btn btn-default" role="button">Bar</a>
<a href="pieChart.html" class="btn btn-default" role="button">Pie</a>
<a href="bubbleChart.html" class="btn btn-default" role="button">Bubble</a>
<a href="force.html" class="btn btn-default" role="button">Force</a>
<a href="globe.html" class="btn btn-default" role="button">Globe</a>
<a href="https://d3js.org" class="btn btn-default" role="button">D3.js</a>
</div>
</div>
</div>
</div>
</header>
<div class="container">
<h3>Pie Chart</h3>
<p>We can draw a pie chart by using svg to render arcs.</p>
<p>The conversion of data to appropriate arc sizes is handled by d3's pie layout.</p>
<div class="col-md-6">
<div id="draw_here"></div>
</div>
<div class="col-md-6">
<pre>
var dataset = [ 5, 10, 20, 20, 6, 25 ];
var w = 500;
var h = 500;
var svg = d3.select("#draw_here")
.append("svg")
.attr("width", w)
.attr("height", h);
var outerRadius = w / 2;
var innerRadius = w / 4;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
<strong>var pie = d3.layout.pie();</strong>
var color = d3.scale.category10();
var arcs = svg.selectAll("g.arc")
<strong>.data(pie(dataset))</strong>
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate("+ outerRadius + ", " + outerRadius +")");
arcs.append("path")
.attr("fill", function(d, i) {
return color(i)
})
.attr("d", arc);
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.value;
});
</pre>
</div>
</div>
<script type="text/javascript">
var dataset = [ 5, 10, 20, 20, 6, 25 ];
var w = 500;
var h = 500;
var svg = d3.select("#draw_here")
.append("svg")
.attr("width", w)
.attr("height", h);
var outerRadius = w / 2;
var innerRadius = w / 4;
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var pie = d3.layout.pie();
var color = d3.scale.category10();
var arcs = svg.selectAll("g.arc")
.data(pie(dataset))
.enter()
.append("g")
.attr("class", "arc")
.attr("transform", "translate("+ outerRadius + ", " + outerRadius +")");
arcs.append("path")
.attr("fill", function(d, i) {
return color(i)
})
.attr("d", arc);
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("text-anchor", "middle")
.text(function(d) {
return d.value;
});
</script>
</body>
</html>