]> nos-oignons.net Git - graphnion.git/blob - pie_graphs.js
Use 'width' setting in demo.
[graphnion.git] / pie_graphs.js
1 /* graphnion: display Tor relay graphs on a website
2  *
3  * Copyright © 2013-2015 Nos oignons <contact@nos-oignons.net>
4  *
5  * graphnion is licensed under the Creative Commons Attribution-Share
6  * Alike 3.0 Unported license.
7  *
8  * You are free:
9  *
10  * • to share – to copy, distribute and transmit the work
11  * • to remix – to adapt the work
12  *
13  * Under the following conditions:
14  * • attribution – You must attribute the work in the manner specified
15  *   by the author or licensor (but not in any way that suggests that they
16  *   endorse you or your use of the work).
17  * • share alike – If you alter, transform, or build upon this work,
18  *   you may distribute the resulting work only under the same or similar
19  *   license to this one.
20  */
21
22 function PieDrawer(selector) {
23   this.selector = selector;
24 };
25 PieDrawer.width = 150;
26 PieDrawer.height = 150;
27 PieDrawer.radius = Math.min(PieDrawer.width, PieDrawer.height) / 2;
28 PieDrawer.formatPercent = d3.format(".2%");
29
30 PieDrawer.color_nos_oignons = "#ffa430";
31 PieDrawer.color_others = "#57075f";
32
33 PieDrawer.arc = d3.svg.arc()
34     .outerRadius(PieDrawer.radius - 10)
35     .innerRadius(PieDrawer.radius - 40);
36
37 PieDrawer.labelRadius = PieDrawer.radius - 15;
38
39 PieDrawer.pie = d3.layout.pie()
40     .sort(null)
41     .value(function(d) { return d.frac; });
42
43 PieDrawer.onionoo_url = "https://onionoo.torproject.org/details?type=relay&contact=adminsys@nos-oignons.net";
44
45 PieDrawer.prototype.getFieldName = undefined;
46 PieDrawer.prototype.getField = undefined;
47 PieDrawer.prototype.draw = function() {
48   var svg = d3.select(this.selector).append("svg")
49       .style("margin", "auto")
50       .attr("width", PieDrawer.width)
51       .attr("height", PieDrawer.height)
52     .append("g")
53       .attr("transform", "translate(" + PieDrawer.width / 2 + "," + PieDrawer.height / 2 + ")");
54
55   var text = svg.append("text")
56       .attr("text-anchor", "middle")
57       .attr("dy", ".35em")
58       .text(L10n.loading);
59
60   var field_getter = this.getField;
61   d3.json(PieDrawer.onionoo_url + "&fields=fingerprint," + this.getFieldName(), function(error, raw_data) {
62     var frac_nos_oignons = 0;
63     raw_data['relays'].forEach(function(d) {
64       nos_oignons_relays.forEach(function(r) {
65         if (r.fingerprint == d.fingerprint) {
66           var value = field_getter(d);
67           if (value) {
68             frac_nos_oignons += value;
69           }
70         }
71       });
72     });
73     var frac_others = 1 - frac_nos_oignons;
74
75     data = [ { name: L10n.nos_oignons, frac: frac_nos_oignons, color: PieDrawer.color_nos_oignons, },
76              { name: L10n.others,      frac: frac_others,      color: PieDrawer.color_others }, ];
77
78     text.text(PieDrawer.formatPercent(data[0].frac));
79
80     var g = svg.selectAll(".arc")
81         .data(PieDrawer.pie(data))
82       .enter().append("g")
83         .attr("class", "arc");
84
85     g.append("path")
86         .attr("d", PieDrawer.arc)
87         .style("fill", function(d) { return d.data.color; });
88
89   });
90 }
91
92 function ConsensusPieDrawer(selector) {
93   this.selector = selector;
94 };
95 ConsensusPieDrawer.prototype = new PieDrawer();
96 ConsensusPieDrawer.prototype.getSelector = function() {
97   return "#consensus-pie";
98 };
99 ConsensusPieDrawer.prototype.getFieldName = function() {
100   return "consensus_weight_fraction";
101 };
102 ConsensusPieDrawer.prototype.getField = function(r) {
103   return r.consensus_weight_fraction;
104 };
105
106 function ExitPieDrawer(selector) {
107   this.selector = selector;
108 };
109 ExitPieDrawer.prototype = new PieDrawer();
110 ExitPieDrawer.prototype.getFieldName = function() {
111   return "exit_probability";
112 };
113 ExitPieDrawer.prototype.getField = function(r) {
114   return r.exit_probability;
115 };