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.labelRadius = this.radius - (this.options.size / 10)
55 this.formatPercent = d3.format(".2%");
56 this.arc = d3.svg.arc()
57 .outerRadius(this.radius - (this.options.size / 10))
58 .innerRadius(this.radius - (this.options.size / 4));
60 this.pie = d3.layout.pie()
62 .value(function(d) { return d.frac; });
64 switch (this.options.type) {
66 this.field = "consensus_weight_fraction";
69 this.field = "guard_probability";
72 this.field = "middle_probability";
75 this.field= "exit_probability";
80 draw : function(frac) {
86 color: this.options.colors.relays
90 color: this.options.colors.others
94 var svg = d3.select(this.selector).append("svg")
95 .style("margin", "auto")
96 .attr("width", this.options.size)
97 .attr("height", this.options.size)
99 .attr("transform", "translate(" + this.options.size / 2 + "," + this.options.size / 2 + ")");
102 var text = svg.append("text")
103 .attr("text-anchor", "middle")
105 .text(this.formatPercent(frac));
107 var g = svg.selectAll(".arc")
108 .data(this.pie(data))
111 .attr("class", "arc");
115 .style("fill", function(d) { return d.data.color; });
119 callOnionoo : function() {
122 // Create Onionoo url
123 var url = "https://onionoo.torproject.org/details?type=relay&fields=fingerprint,nickname," + this.field;
124 if (typeof this.options.onionoo.contact !== 'undefined') {
125 url += "&contact=" + this.options.onionoo.contact;
127 else if (typeof this.options.onionoo.family !== 'undefined') {
128 url += "&family=" + this.options.onionoo.family;
131 // Load data, and draw graph
132 d3.json(url, function(error, data) {
133 if (error) return console.warn(error);
135 var frac = self.computeData(data);
140 computeData : function(rawData) {
144 rawData['relays'].forEach(function(d) {
145 frac += d[self.field];
153 if (typeof define === 'function' && define.amd) {
154 define("Graphnion", ["d3"], Graphnion);
155 } else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
156 module.exports = Graphnion;
158 window.Graphnion = Graphnion;