1 tr
.graphing
.Radar = function (size
, radar
) {
4 fib
= new tr
.util
.Fib();
7 self
.svg = function () {
12 return Math
.round(size
/2);
15 function plotLines() {
21 .attr('stroke-width', 14);
28 .attr('stroke-width', 14);
31 function getRadius(cycles
, i
) {
32 var sequence
= fib
.sequence(cycles
.length
);
33 var total
= fib
.sum(cycles
.length
);
36 return center() - (center() * sum
/ total
);
39 function plotCircles(cycles
) {
42 cycles
.forEach(function (cycle
, i
) {
46 .attr('r', getRadius(cycles
, i
));
50 function plotTexts(cycles
) {
53 increment
= Math
.round(center() / cycles
.length
);
55 cycles
.forEach(function (cycle
, i
) {
57 .attr('class', 'line-text')
58 .attr('y', center() + 4)
59 .attr('x', center() - getRadius(cycles
, i
) + 10)
63 .attr('class', 'line-text')
64 .attr('y', center() + 4)
65 .attr('x', center() + getRadius(cycles
, i
) - 10)
66 .attr('text-anchor', 'end')
71 function triangle(x
, y
, cssClass
) {
72 var tsize
, top
, left
, right
, bottom
, points
;
76 left
= (x
- tsize
+ 1);
77 right
= (x
+ tsize
+ 1);
78 bottom
= (y
+ tsize
- tsize
/ 2.5);
80 points
= x
+ 1 + ',' + top
+ ' ' + left
+ ',' + bottom
+ ' ' + right
+ ',' + bottom
;
82 return svg
.append('polygon')
83 .attr('points', points
)
84 .attr('class', cssClass
)
85 .attr('stroke-width', 1.5);
88 function circle(x
, y
, cssClass
) {
92 .attr('class', cssClass
)
93 .attr('stroke-width', 1.5)
97 function plotBlips(cycles
, quadrant
, adjustX
, adjustY
) {
99 blips
= quadrant
.blips();
100 cycles
.forEach(function (cycle
, i
) {
101 var maxRadius
, minRadius
, cycleBlips
;
103 maxRadius
= getRadius(cycles
, i
);
104 minRadius
= (i
== cycles
.length
- 1) ? 0: getRadius(cycles
, i
+ 1);
106 var cycleBlips
= blips
.filter(function (blip
) {
107 return blip
.cycle() == cycle
;
110 cycleBlips
.forEach(function (blip
) {
111 var angleInRad
, radius
;
113 var split
= blip
.name().split('');
114 var sum
= split
.reduce(function (p
, c
) { return p
+ c
.charCodeAt(0); }, 0);
115 chance
= new Chance(sum
* cycle
.name().length
* blip
.number());
117 angleInRad
= Math
.PI
* chance
.integer({ min
: 13, max
: 85 }) / 180;
118 radius
= chance
.floating({ min
: minRadius
+ 25, max
: maxRadius
- 10 });
120 var x
= center() + radius
* Math
.cos(angleInRad
) * adjustX
;
121 var y
= center() + radius
* Math
.sin(angleInRad
) * adjustY
;
124 triangle(x
, y
, cssClassFor(quadrant
.name()));
126 circle(x
, y
, cssClassFor(quadrant
.name()));
132 .attr('class', 'blip-text')
133 .attr('text-anchor', 'middle')
139 function cssClassFor(string
) {
140 return string
.toLowerCase().replace(/\s\&/g, '').replace(/\s/g, '-');
143 function plotQuadrantNames(quadrants
) {
144 function plotName(name
, anchor
, x
, y
) {
148 .attr('class', cssClassFor(name
))
149 .attr('text-anchor', anchor
)
153 plotName(quadrants
.I
.name(), 'end', size
- 10, 10)
154 plotName(quadrants
.II
.name(), 'start', 10, 10)
155 plotName(quadrants
.III
.name(), 'start', 10, size
- 10)
156 plotName(quadrants
.IV
.name(), 'end', size
-10, size
- 10)
159 self
.init = function (selector
) {
160 svg
= d3
.select(selector
|| 'body').append("svg");
164 self
.plot = function () {
165 var cycles
, quadrants
;
167 cycles
= radar
.cycles().reverse();
168 quadrants
= radar
.quadrants();
170 svg
.attr('width', size
).attr('height', size
);
176 if (radar
.hasQuadrants()) {
177 plotQuadrantNames(quadrants
);
178 plotBlips(cycles
, quadrants
.I
, 1, -1);
179 plotBlips(cycles
, quadrants
.II
, -1, -1);
180 plotBlips(cycles
, quadrants
.III
, -1, 1);
181 plotBlips(cycles
, quadrants
.IV
, 1, 1);