-
Notifications
You must be signed in to change notification settings - Fork 5
/
radar-chart.js
154 lines (141 loc) · 6.44 KB
/
radar-chart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// I do not know the origin of this code.
var RadarChart = {
draw: function(id, d, options){
var cfg = {
radius: 7,
w: 800,
h: 800,
factor: .95,
factorLegend: 1,
levels: 3,
maxValue: 1, // The value that the biggest circle will represent.
// Needed when all data points are <=0.
radians: 2 * Math.PI,
opacityArea: 0,
color: d3.scale.category10(),
fontSize: 16
};
if('undefined' !== typeof options){
for(var i in options){
if('undefined' !== typeof options[i]){
cfg[i] = options[i];
}
}
}
cfg.maxValue = Math.max(cfg.maxValue, d3.max(d, function(i){return d3.max(i.map(function(o){return o.value;}))}));
var allAxis = (d[0].map(function(i, j){return i.axis}));
var total = allAxis.length;
var radius = cfg.factor*Math.min(cfg.w/2, cfg.h/2);
d3.select(id).select("svg").remove();
var g = d3.select(id).append("svg").attr("width", cfg.w).attr("height", cfg.h).append("g");
var tooltip;
function getPosition(i, range, factor, func){
factor = typeof factor !== 'undefined' ? factor : 1;
return range * (1 - factor * func(i * cfg.radians / total));
}
function getHorizontalPosition(i, range, factor){
return getPosition(i, range, factor, Math.sin);
}
function getVerticalPosition(i, range, factor){
return getPosition(i, range, factor, Math.cos);
}
for(var j=0; j<cfg.levels; j++){
var levelFactor = radius*((j+1)/cfg.levels);
g.selectAll(".levels").data(allAxis).enter().append("svg:line")
.attr("x1", function(d, i){return getHorizontalPosition(i, levelFactor);})
.attr("y1", function(d, i){return getVerticalPosition(i, levelFactor);})
.attr("x2", function(d, i){return getHorizontalPosition(i+1, levelFactor);})
.attr("y2", function(d, i){return getVerticalPosition(i+1, levelFactor);})
.attr("class", "line").style("stroke", "grey").style("stroke-width", "0.5px").attr("transform", "translate(" + (cfg.w/2-levelFactor) + ", " + (cfg.h/2-levelFactor) + ")");
}
series = 0;
var axis = g.selectAll(".axis").data(allAxis)
.enter().append("g").attr("class", "axis");
axis.append("line")
.attr("x1", cfg.w/2)
.attr("y1", cfg.h/2)
.attr("x2", function(j, i){return getHorizontalPosition(i, cfg.w/2, cfg.factor);})
.attr("y2", function(j, i){return getVerticalPosition(i, cfg.h/2, cfg.factor);})
.attr("class", "line").style("stroke", "grey").style("stroke-width", "1px");
axis.append("text").attr("class", "legend")
.text(function(d){return d})
.style("font-family", "sans-serif").style("font-size", cfg.fontSize + "px")
.style("text-anchor", function(d, i){
var p = getHorizontalPosition(i, 0.5);
return (p < 0.4) ? "start" : ((p > 0.6) ? "end" : "middle");
})
.attr("transform", function(d, i){
var p = getVerticalPosition(i, cfg.h / 2);
return p < cfg.fontSize ? "translate(0, " + (cfg.fontSize - p) + ")" : "";
})
.attr("x", function(d, i){return getHorizontalPosition(i, cfg.w / 2, cfg.factorLegend);})
.attr("y", function(d, i){return getVerticalPosition(i, cfg.h / 2, cfg.factorLegend);});
d.forEach(function(y, x){
dataValues = [];
g.selectAll(".nodes")
.data(y, function(j, i){
dataValues.push([
getHorizontalPosition(i, cfg.w/2, (parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor),
getVerticalPosition(i, cfg.h/2, (parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor)
]);
});
dataValues.push(dataValues[0]);
g.selectAll(".area")
.data([dataValues])
.enter()
.append("polygon")
.attr("class", "radar-chart-serie"+series)
.style("stroke-width", "4px")
.style("stroke", cfg.color(series))
.attr("points",function(d) {
var str="";
for(var pti=0;pti<d.length;pti++){
str=str+d[pti][0]+","+d[pti][1]+" ";
}
return str;
})
.style("fill", function(j, i){return cfg.color(series)})
.style("fill-opacity", cfg.opacityArea);
series++;
});
series=0;
d.forEach(function(y, x){ // x=number of the data point, y=data point
g.selectAll(".nodes")
.data(y).enter() // http://bost.ocks.org/mike/join/
.append("svg:circle").attr("class", "radar-chart-serie"+series)
// On drive-side (x==0), place mark onto even axis (j.axis==0),
// on non-drive - onto odd. TODO: make in a generic way, pass a function?
.attr('r', function(j) {
return j.axis % 2 == x ? 0 : cfg.radius;
})
.attr("alt", function(j){return Math.max(j.value, 0);})
.attr("cx", function(j, i){
dataValues.push([
getHorizontalPosition(i, cfg.w/2, (parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor),
getVerticalPosition(i, cfg.h/2, (parseFloat(Math.max(j.value, 0))/cfg.maxValue)*cfg.factor)
]);
return getHorizontalPosition(i, cfg.w/2, (Math.max(j.value, 0)/cfg.maxValue)*cfg.factor);
})
.attr("cy", function(j, i){
return getVerticalPosition(i, cfg.h/2, (Math.max(j.value, 0)/cfg.maxValue)*cfg.factor);
})
.attr("data-id", function(j){return j.axis})
.style("fill", cfg.color(series)).style("fill-opacity", .9)
.on('mouseover', function (d){
newX = parseFloat(d3.select(this).attr('cx')) - 10;
newY = parseFloat(d3.select(this).attr('cy')) - 5;
tooltip.attr('x', newX).attr('y', newY).text(d.value).transition(200).style('opacity', 1);
z = "polygon."+d3.select(this).attr("class");
})
.on('mouseout', function(){
tooltip.transition(200).style('opacity', 0);
g.selectAll("polygon").transition(200).style("fill-opacity", cfg.opacityArea);
})
.append("svg:title")
.text(function(j){return Math.max(j.value, 0)});
series++;
});
//Tooltip
tooltip = g.append('text').style('opacity', 0).style('font-family', 'sans-serif').style('font-size', '13px');
}
};