added fibonacci algorithm to render the arcs
[tech-radar.git] / src / graphing / radar.js
1 tr.graphing.Radar = function (svg, size, radar) {
2 var self, fib;
3 self = {};
4 fib = new tr.util.Fib();
5
6 svg.attr('width', size).attr('height', size);
7
8 function center () {
9 return Math.round(size/2);
10 }
11
12 function plotLines() {
13 svg.append('line')
14 .attr('x1', center())
15 .attr('y1', 0)
16 .attr('x2', center())
17 .attr('y2', size)
18 .attr('stroke-width', 14);
19
20 svg.append('line')
21 .attr('x1', 0)
22 .attr('y1', center())
23 .attr('x2', size)
24 .attr('y2', center())
25 .attr('stroke-width', 14);
26 };
27
28 function getRadius(cycles, i) {
29 var sequence = fib.sequence(cycles.length);
30 var total = fib.sum(cycles.length);
31 var sum = fib.sum(i);
32
33 return center() - (center() * sum / total);
34 }
35
36 function plotCircles(cycles) {
37 var increment;
38
39 cycles.forEach(function (cycle, i) {
40 svg.append('circle')
41 .attr('cx', center())
42 .attr('cy', center())
43 .attr('r', getRadius(cycles, i));
44 });
45 }
46
47 function plotTexts(cycles) {
48 var increment;
49
50 increment = Math.round(center() / cycles.length);
51
52 cycles.forEach(function (cycle, i) {
53 svg.append('text')
54 .attr('y', center() + 4)
55 .attr('x', center() - getRadius(cycles, i) + 10)
56 .text(cycle.name());
57
58 svg.append('text')
59 .attr('y', center() + 4)
60 .attr('x', center() + getRadius(cycles, i) - 10)
61 .attr('text-anchor', 'end')
62 .text(cycle.name());
63 });
64 };
65
66 self.plot = function () {
67 var cycles = radar.cycles().reverse();
68
69 plotCircles(cycles);
70 plotLines();
71 plotTexts(cycles);
72 };
73
74 return self;
75 };