1 function PieDrawer(selector) {
2 this.selector = selector;
5 PieDrawer.height = 150;
6 PieDrawer.radius = Math.min(PieDrawer.width, PieDrawer.height) / 2;
7 PieDrawer.formatPercent = d3.format(".2%");
9 PieDrawer.color_nos_oignons = "#ffa430";
10 PieDrawer.color_others = "#57075f";
12 PieDrawer.arc = d3.svg.arc()
13 .outerRadius(PieDrawer.radius - 10)
14 .innerRadius(PieDrawer.radius - 40);
16 PieDrawer.labelRadius = PieDrawer.radius - 15;
18 PieDrawer.pie = d3.layout.pie()
20 .value(function(d) { return d.frac; });
22 PieDrawer.onionoo_url = "https://onionoo.torproject.org/details?type=relay&contact=adminsys@nos-oignons.net";
24 PieDrawer.prototype.getFieldName = undefined;
25 PieDrawer.prototype.getField = undefined;
26 PieDrawer.prototype.draw = function() {
27 var svg = d3.select(this.selector).append("svg")
28 .style("margin", "auto")
29 .attr("width", PieDrawer.width)
30 .attr("height", PieDrawer.height)
32 .attr("transform", "translate(" + PieDrawer.width / 2 + "," + PieDrawer.height / 2 + ")");
34 var text = svg.append("text")
35 .attr("text-anchor", "middle")
39 var field_getter = this.getField;
40 d3.json(PieDrawer.onionoo_url + "&fields=fingerprint," + this.getFieldName(), function(error, raw_data) {
41 var frac_nos_oignons = 0;
42 raw_data['relays'].forEach(function(d) {
43 nos_oignons_relays.forEach(function(r) {
44 if (r.fingerprint == d.fingerprint) {
45 var value = field_getter(d);
47 frac_nos_oignons += value;
52 var frac_others = 1 - frac_nos_oignons;
54 data = [ { name: L10n.nos_oignons, frac: frac_nos_oignons, color: PieDrawer.color_nos_oignons, },
55 { name: L10n.others, frac: frac_others, color: PieDrawer.color_others }, ];
57 text.text(PieDrawer.formatPercent(data[0].frac));
59 var g = svg.selectAll(".arc")
60 .data(PieDrawer.pie(data))
62 .attr("class", "arc");
65 .attr("d", PieDrawer.arc)
66 .style("fill", function(d) { return d.data.color; });
71 function ConsensusPieDrawer(selector) {
72 this.selector = selector;
74 ConsensusPieDrawer.prototype = new PieDrawer();
75 ConsensusPieDrawer.prototype.getSelector = function() {
76 return "#consensus-pie";
78 ConsensusPieDrawer.prototype.getFieldName = function() {
79 return "consensus_weight_fraction";
81 ConsensusPieDrawer.prototype.getField = function(r) {
82 return r.consensus_weight_fraction;
85 function ExitPieDrawer(selector) {
86 this.selector = selector;
88 ExitPieDrawer.prototype = new PieDrawer();
89 ExitPieDrawer.prototype.getFieldName = function() {
90 return "exit_probability";
92 ExitPieDrawer.prototype.getField = function(r) {
93 return r.exit_probability;