1 /* graphnion: display Tor relay graphs on a website
3 * Copyright © 2013-2015 Nos oignons <contact@nos-oignons.net>
5 * graphnion is licensed under the Creative Commons Attribution-Share
6 * Alike 3.0 Unported license.
10 * • to share – to copy, distribute and transmit the work
11 * • to remix – to adapt the work
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.
22 function PieDrawer(selector) {
23 this.selector = selector;
25 PieDrawer.width = 150;
26 PieDrawer.height = 150;
27 PieDrawer.radius = Math.min(PieDrawer.width, PieDrawer.height) / 2;
28 PieDrawer.formatPercent = d3.format(".2%");
30 PieDrawer.color_nos_oignons = "#ffa430";
31 PieDrawer.color_others = "#57075f";
33 PieDrawer.arc = d3.svg.arc()
34 .outerRadius(PieDrawer.radius - 10)
35 .innerRadius(PieDrawer.radius - 40);
37 PieDrawer.labelRadius = PieDrawer.radius - 15;
39 PieDrawer.pie = d3.layout.pie()
41 .value(function(d) { return d.frac; });
43 PieDrawer.onionoo_url = "https://onionoo.torproject.org/details?type=relay&contact=adminsys@nos-oignons.net";
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)
53 .attr("transform", "translate(" + PieDrawer.width / 2 + "," + PieDrawer.height / 2 + ")");
55 var text = svg.append("text")
56 .attr("text-anchor", "middle")
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);
68 frac_nos_oignons += value;
73 var frac_others = 1 - frac_nos_oignons;
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 }, ];
78 text.text(PieDrawer.formatPercent(data[0].frac));
80 var g = svg.selectAll(".arc")
81 .data(PieDrawer.pie(data))
83 .attr("class", "arc");
86 .attr("d", PieDrawer.arc)
87 .style("fill", function(d) { return d.data.color; });
92 function ConsensusPieDrawer(selector) {
93 this.selector = selector;
95 ConsensusPieDrawer.prototype = new PieDrawer();
96 ConsensusPieDrawer.prototype.getSelector = function() {
97 return "#consensus-pie";
99 ConsensusPieDrawer.prototype.getFieldName = function() {
100 return "consensus_weight_fraction";
102 ConsensusPieDrawer.prototype.getField = function(r) {
103 return r.consensus_weight_fraction;
106 function ExitPieDrawer(selector) {
107 this.selector = selector;
109 ExitPieDrawer.prototype = new PieDrawer();
110 ExitPieDrawer.prototype.getFieldName = function() {
111 return "exit_probability";
113 ExitPieDrawer.prototype.getField = function(r) {
114 return r.exit_probability;