forked from sidooms/Recsys-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pie_chart.html
93 lines (75 loc) · 2.43 KB
/
pie_chart.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
<!--
# Recsys-frontend, Copyright (c) 2013, Simon Dooms
# http://github.com/sidooms/recsys-frontend
# MIT License
#
-->
<style>
.arc path {
stroke: #fff;
}
</style>
<div id='pie_chart'></div>
<script>
$( document ).ready(function() {
init_the_d3_pie_chart();
pie_chart_initialized = true;
});
var the_pie_svg ;
var arc ;
var pie ;
var colorsss ;
var labelr;
var radius;
function init_the_d3_pie_chart()
{
var width = 960,
height = 500;
radius = Math.min(width, height) / 2.2;
labelr = radius + 10;
colorsss = d3.scale.category20c();
arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.weight; });
the_pie_svg = d3.select("#pie_chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
update_pie_chart(piechart_data);
}
//http://stackoverflow.com/questions/8053424/label-outside-arc-pie-chart-d3-js
function update_pie_chart(data){
// update the slices
var g = the_pie_svg.selectAll(".arc")
.data(pie(data));
g.enter().append("path")
.attr("class", "arc")
.attr("d", arc)
.style("fill", function (d, i) { return colorsss(i); });
g.attr("d", arc);
var g = the_pie_svg.selectAll(".label")
.data(pie(data));
// update the labels
g.enter().append("text")
.attr("class", "label")
.attr("dy", ".35em")
.style("text-anchor", function (d) {
// are we past the center?
return (d.endAngle + d.startAngle)/2 > Math.PI ?"end" : "start";
})
.text(function(d) { return d.data.weight > 0 ? d.data.algo : ''; });
g.attr("transform", function (d) {
var c = arc.centroid(d),
x = c[0],
y = c[1],
// pythagorean theorem for hypotenuse
h = Math.sqrt(x*x + y*y);
return "translate(" + (x/h * labelr) + ',' + (y/h * labelr) + ")";
})
.text(function(d) { return d.data.weight > 0 ? d.data.algo : ''; });
}
</script>