Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame^] | 1 | /*global d3*/ |
| 2 | |
| 3 | function createTopologyView() { |
| 4 | return d3.select('#svg-container').append('svg:svg').attr('viewBox', '0 0 1000 1000'). |
| 5 | append('svg:g').attr('transform', 'translate(500 500)'); |
| 6 | } |
| 7 | |
| 8 | |
| 9 | |
| 10 | function drawHeader(model) { |
| 11 | d3.select('#lastUpdate').text(model.timestamp); |
| 12 | } |
| 13 | |
| 14 | function drawTopology(svg, model) { |
| 15 | |
| 16 | var rings = [{ |
| 17 | radius: 3, |
| 18 | width: 16, |
| 19 | switches: model.edgeSwitches |
| 20 | }, { |
| 21 | radius: 1.5, |
| 22 | width: 32, |
| 23 | switches: model.aggregationSwitches |
| 24 | }, { |
| 25 | radius: 1, |
| 26 | width: 32, |
| 27 | switches: model.coreSwitches |
| 28 | }]; |
| 29 | |
| 30 | function ringEnter(data, i) { |
| 31 | if (!data.switches.length) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | var k = 360 / data.switches.length; |
| 36 | |
| 37 | d3.select(this).selectAll("g") |
| 38 | .data(d3.range(data.switches.length).map(function() { |
| 39 | return data; |
| 40 | })) |
| 41 | .enter().append("svg:g") |
| 42 | .attr("class", "square") |
| 43 | .attr("transform", function(_, i) { |
| 44 | return "rotate(" + i * k + ")translate(" + data.radius*150 + ")"; |
| 45 | }) |
| 46 | .append("svg:rect") |
| 47 | .attr("x", -data.width / 2) |
| 48 | .attr("y", -data.width / 2) |
| 49 | .attr("width", data.width) |
| 50 | .attr("height", data.width); |
| 51 | } |
| 52 | |
| 53 | var ring = svg.selectAll("g") |
| 54 | .data(rings) |
| 55 | .enter().append("svg:g") |
| 56 | .attr("class", "ring") |
| 57 | .each(ringEnter); |
| 58 | } |
| 59 | |
| 60 | function sync(svg) { |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 61 | updateModel(function (model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame^] | 62 | |
| 63 | drawHeader(model); |
| 64 | drawTopology(svg, model); |
| 65 | |
| 66 | // do it again in 1s |
| 67 | setTimeout(function () { |
| 68 | sync(svg) |
| 69 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 70 | }); |
| 71 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame^] | 72 | |
| 73 | sync(createTopologyView()); |