]> nos-oignons.net Git - website.git/blob - assets/pie_graphs.js
correction typo
[website.git] / assets / pie_graphs.js
1 function PieDrawer(selector) {
2   this.selector = selector;
3 };
4 PieDrawer.width = 150;
5 PieDrawer.height = 150;
6 PieDrawer.radius = Math.min(PieDrawer.width, PieDrawer.height) / 2;
7 PieDrawer.formatPercent = d3.format(".2%");
8
9 PieDrawer.color_nos_oignons = "#ffa430";
10 PieDrawer.color_others = "#57075f";
11
12 PieDrawer.arc = d3.svg.arc()
13     .outerRadius(PieDrawer.radius - 10)
14     .innerRadius(PieDrawer.radius - 40);
15
16 PieDrawer.labelRadius = PieDrawer.radius - 15;
17
18 PieDrawer.pie = d3.layout.pie()
19     .sort(null)
20     .value(function(d) { return d.frac; });
21
22 PieDrawer.onionoo_url = "https://onionoo.torproject.org/details?type=relay&contact=adminsys@nos-oignons.net";
23
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)
31     .append("g")
32       .attr("transform", "translate(" + PieDrawer.width / 2 + "," + PieDrawer.height / 2 + ")");
33
34   var text = svg.append("text")
35       .attr("text-anchor", "middle")
36       .attr("dy", ".35em")
37       .text(L10n.loading);
38
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);
46           if (value) {
47             frac_nos_oignons += value;
48           }
49         }
50       });
51     });
52     var frac_others = 1 - frac_nos_oignons;
53
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 }, ];
56
57     text.text(PieDrawer.formatPercent(data[0].frac));
58
59     var g = svg.selectAll(".arc")
60         .data(PieDrawer.pie(data))
61       .enter().append("g")
62         .attr("class", "arc");
63
64     g.append("path")
65         .attr("d", PieDrawer.arc)
66         .style("fill", function(d) { return d.data.color; });
67
68   });
69 }
70
71 function ConsensusPieDrawer(selector) {
72   this.selector = selector;
73 };
74 ConsensusPieDrawer.prototype = new PieDrawer();
75 ConsensusPieDrawer.prototype.getSelector = function() {
76   return "#consensus-pie";
77 };
78 ConsensusPieDrawer.prototype.getFieldName = function() {
79   return "consensus_weight_fraction";
80 };
81 ConsensusPieDrawer.prototype.getField = function(r) {
82   return r.consensus_weight_fraction;
83 };
84
85 function ExitPieDrawer(selector) {
86   this.selector = selector;
87 };
88 ExitPieDrawer.prototype = new PieDrawer();
89 ExitPieDrawer.prototype.getFieldName = function() {
90   return "exit_probability";
91 };
92 ExitPieDrawer.prototype.getField = function(r) {
93   return r.exit_probability;
94 };