4 /*global define, module, exports, require */
7 Graphnion.pie = function (selector, options) {
8 return new GraphnionPie(selector, options);
16 function GraphnionPie(selector, options) {
28 this.selector = selector;
32 options = options || {};
34 this.options.type = options.type || defaults.type;
35 this.options.size = options.size || defaults.size;
36 this.options.colors = options.colors || defaults.colors;
37 this.options.onionoo = options.onionoo;
40 // initialize some attributes
45 Graphnion.version = "0.1";
47 GraphnionPie.prototype = {
48 constructor: GraphnionPie,
53 this.radius = this.options.size / 2,
54 this.formatPercent = d3.format(".2%");
55 this.arc = d3.svg.arc()
56 .outerRadius(this.radius - (this.options.size / 10))
57 .innerRadius(this.radius - (this.options.size / 4));
59 this.pie = d3.layout.pie()
61 .value(function(d) { return d.frac; });
63 switch (this.options.type) {
65 this.field = "consensus_weight_fraction";
68 this.field = "guard_probability";
71 this.field = "middle_probability";
74 this.field= "exit_probability";
79 draw : function(frac) {
85 color: this.options.colors.relays
89 color: this.options.colors.others
93 var svg = d3.select(this.selector).append("svg")
94 .style("margin", "auto")
95 .attr("width", this.options.size)
96 .attr("height", this.options.size)
98 .attr("transform", "translate(" + this.options.size / 2 + "," + this.options.size / 2 + ")");
101 var text = svg.append("text")
102 .attr("text-anchor", "middle")
104 .text(this.formatPercent(frac));
106 var g = svg.selectAll(".arc")
107 .data(this.pie(data))
110 .attr("class", "arc");
114 .style("fill", function(d) { return d.data.color; });
118 callOnionoo : function() {
121 // Create Onionoo url
122 var url = "https://onionoo.torproject.org/details?type=relay&fields=fingerprint,nickname," + this.field;
123 if (typeof this.options.onionoo.contact !== 'undefined') {
124 url += "&contact=" + this.options.onionoo.contact;
126 else if (typeof this.options.onionoo.family !== 'undefined') {
127 url += "&family=" + this.options.onionoo.family;
130 // Load data, and draw graph
131 d3.json(url, function(error, data) {
132 if (error) return console.warn(error);
134 var frac = self.computeData(data);
139 computeData : function(rawData) {
143 rawData['relays'].forEach(function(d) {
144 frac += d[self.field];
152 if (typeof define === 'function' && define.amd) {
153 define("Graphnion", ["d3"], Graphnion);
154 } else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
155 module.exports = Graphnion;
157 window.Graphnion = Graphnion;