X-Git-Url: https://www.nos-oignons.net/gitweb/website.git/blobdiff_plain/4c2924543ea0f426c3342fe8430ca606a6dbc85c..50cd0d282feac1e57750fbb841921298d0ae879a:/assets/weights_graphs.js diff --git a/assets/weights_graphs.js b/assets/weights_graphs.js index 915a8b8..af18990 100644 --- a/assets/weights_graphs.js +++ b/assets/weights_graphs.js @@ -37,11 +37,11 @@ WeightsDrawer.area = d3.svg.area() WeightsDrawer.stack = d3.layout.stack() .values(function(d) { return d.values; }); -WeightsDrawer.onionoo_url = "https://onionoo.torproject.org/weights?type=relay&contact=adminsys@nos-oignons.net"; +WeightsDrawer.onionoo_url = "https://onionoo.torproject.org/weights?type=relay&contact=%20%20%20%200x9F29C15D42A8B6F3"; WeightsDrawer.periods = [ { id: "1_month", label: L10n.t_1_month }, - { id: "3_months", label: L10n.t_3_months }, + { id: "6_months", label: L10n.t_6_months }, { id: "1_year", label: L10n.t_1_year }, { id: "5_years", label: L10n.t_5_years }, ]; @@ -69,8 +69,8 @@ WeightsDrawer.extract_values = function(history, interval, minTime, maxTime) { } WeightsDrawer.color = d3.scale.ordinal(); -WeightsDrawer.color.domain(nos_oignons_relays.map(function(r) {return r.fingerprint})); -WeightsDrawer.color.range(nos_oignons_relays.map(function(r) {return r.color})); +WeightsDrawer.color.domain(nos_oignons_groups.map(function(g) {return g.name})); +WeightsDrawer.color.range(nos_oignons_groups.map(function(g) {return g.color})); WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { var drawer = this; @@ -83,6 +83,7 @@ WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { }); var form_source = d3.select(drawer.selector).append("form") + .attr("id", "weight-sources") .attr("action", "#"); WeightsDrawer.sources.forEach(function(s) { var div = form_source.append("div"); @@ -99,29 +100,36 @@ WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { } }); - drawer.svg = d3.select(drawer.selector).append("svg") + var wrapper = d3.select(drawer.selector).append("div") + .style("display", "flex") + .style("align-items", "flex-start"); + + drawer.svg = wrapper.append("svg") .attr("width", WeightsDrawer.width + WeightsDrawer.margin.left + WeightsDrawer.margin.right) .attr("height", WeightsDrawer.height + WeightsDrawer.margin.top + WeightsDrawer.margin.bottom) .append("g") .attr("transform", "translate(" + WeightsDrawer.margin.left + "," + WeightsDrawer.margin.top + ")"); var form_period = d3.select(drawer.selector).append("form") + .attr("class", "graph-period") .attr("action", "#"); WeightsDrawer.periods.forEach(function(p) { var div = form_period.append("div"); var radio = div.append("input") .attr("type", "radio") .attr("name", "period") - .attr("id", "period_" + p.id) + .attr("id", "weights_period_" + p.id) .on("click", function() { drawer.update_period(p.id); }); div.append("label") - .attr("for", "period_" + p.id) + .attr("for", "weights_period_" + p.id) .text(p.label); if (p.id == WeightsDrawer.periods[0].id) { radio.attr("checked", true); } }); + var relayIndex = buildRelayIndex(raw_data.relays); + WeightsDrawer.sources.map(function(s) { return s.id; }).forEach(function(source) { drawer.weights_data[source] = {}; WeightsDrawer.periods.map(function(p) { return p.id; }).forEach(function(period) { @@ -142,17 +150,12 @@ WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { var maxValue = 0; - var relays = WeightsDrawer.color.domain().map(function(fingerprint) { - var relay_data = raw_data["relays"].filter(function(d) { return d.fingerprint == fingerprint; })[0]; - - var history = relay_data[source][period]; - var values = WeightsDrawer.extract_values(history, interval, minTime, maxTime); - maxValue = maxValue + d3.max(values, function(d) { return d.y; }); - - return { - fingerprint: fingerprint, - values: values, - }; + var relays = nos_oignons_groups.map(function(group) { + var group_values = sumGroupRelayValues(group, relayIndex, function(r) { + return WeightsDrawer.extract_values(r[source][period], interval, minTime, maxTime); + }); + maxValue += (d3.max(group_values, function(d) { return d.y; }) || 0); + return { group: group.name, values: group_values }; }); drawer.weights_data[source][period] = { minTime: minTime, @@ -172,7 +175,7 @@ WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { .enter().append("path") .attr("class", "weight_graph area") .attr("d", function(d) { return WeightsDrawer.area(d.values); }) - .style("fill", function(d) { return WeightsDrawer.color(d.fingerprint); }); + .style("fill", function(d) { return WeightsDrawer.color(d.group); }); drawer.svg.append("g") .attr("class", "x axis") @@ -186,26 +189,31 @@ WeightsDrawer.prototype.draw_weights_graph = function(raw_data) { .attr("class", "y axis") .call(WeightsDrawer.yAxis); - var legend = drawer.svg.selectAll(".legend") - .data(WeightsDrawer.color.domain().slice().reverse()) - .enter().append("g") - .attr("class", "legend") - .attr("transform", function(d, i) { return "translate(0," + ((i * 20) - WeightsDrawer.margin.top) + ")"; }); - - legend.append("rect") - .attr("x", WeightsDrawer.width - 18) - .attr("width", 18) - .attr("height", 18) - .style("fill", WeightsDrawer.color); - - legend.append("text") - .attr("x", WeightsDrawer.width - 24) - .attr("y", 9) - .attr("dy", ".35em") - .style("text-anchor", "end") - .text(function(d) { - return nos_oignons_relays.filter(function(r) { return r.fingerprint == d; })[0].name; - }); + var legendDiv = wrapper.append("div") + .style("overflow-y", "auto") + .style("max-height", (WeightsDrawer.height + WeightsDrawer.margin.top + WeightsDrawer.margin.bottom) + "px") + .style("min-width", "90px") + .style("padding-left", "10px") + .style("padding-top", WeightsDrawer.margin.top + "px") + .style("box-sizing", "border-box"); + + WeightsDrawer.color.domain().slice().reverse().forEach(function(name) { + var item = legendDiv.append("div") + .style("display", "flex") + .style("align-items", "center") + .style("margin-bottom", "3px") + .style("white-space", "nowrap"); + item.append("span") + .style("display", "inline-block") + .style("width", "14px") + .style("min-width", "14px") + .style("height", "14px") + .style("background-color", WeightsDrawer.color(name)) + .style("margin-right", "5px"); + item.append("span") + .style("font-size", "11px") + .text(name); + }); }; WeightsDrawer.prototype.refresh_graph = function() {