Created
August 7, 2012 02:04
-
-
Save frcepeda/3280685 to your computer and use it in GitHub Desktop.
Mathematica code for radar charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SolveLines[x0_, y0_, x1_, y1_, x2_, y2_, x3_, y3_] := ( | |
Module[{m1, m2}, | |
If[x0 - x1 != 0 && x2 - x3 != 0, | |
m1 = (y0 - y1)/(x0 - x1); | |
m2 = (y2 - y3)/(x2 - x3); | |
Return[{x, y} /. | |
Solve[y - y0 == m1 (x - x0) && y - y2 == m2 (x - x2), {x, y}] // | |
Flatten]; | |
, | |
If[x0 - x1 == 0, | |
m2 = (y2 - y3)/(x2 - x3); | |
Return[{x, y} /. Solve[x == x0 && y - y2 == m2 (x - x2), {x, y}] // | |
Flatten]; | |
, | |
m1 = (y0 - y1)/(x0 - x1); | |
Return[{x, y} /. Solve[x == x2 && y - y0 == m1 (x - x0), {x, y}] // | |
Flatten]; | |
] | |
] | |
] | |
) | |
radarchartelementf[{{t0_, t1_}, {r0_, r1_}}, _, metadata_] := ( | |
Module[{last = metadata[[1]][[1]], next = metadata[[1]][[2]], | |
angle = t1 - t0, lastangle, nextangle}, | |
lastangle = t1 + angle/2; | |
nextangle = t0 - angle/2; | |
Polygon[{ | |
r0 {Cos[t0], Sin[t0]}, | |
SolveLines[ | |
r1*Cos[Mean[{t0, t1}]], r1*Sin[Mean[{t0, t1}]], | |
next*Cos[nextangle], next*Sin[nextangle], | |
0, 0, | |
r1*Cos[t0], r1*Sin[t0] | |
], | |
r1 {Cos[Mean[{t0, t1}]], Sin[Mean[{t0, t1}]]}, | |
SolveLines[ | |
r1*Cos[Mean[{t0, t1}]], r1*Sin[Mean[{t0, t1}]], | |
last*Cos[lastangle], last*Sin[lastangle], | |
0, 0, | |
r1*Cos[t1], r1*Sin[t1] | |
], | |
r0 {Cos[t1], Sin[t1]} | |
}] | |
] | |
); | |
RadarChart[list_, stuff___] := ( | |
SectorChart[ | |
Table[ | |
{1, list[[n]]} -> | |
{list[[If[n - 1 == 0, Length@list, n - 1]]], | |
list[[If[n + 1 > Length@list, 1, n + 1]]]}, {n, Length@list} | |
], | |
PolarAxes -> Automatic, | |
ChartElementFunction -> radarchartelementf, | |
PolarGridLines -> {Table[ | |
Pi*Length@types + Pi*2 n/Length@types, {n, 0, Length[types]}], | |
Table[n, {n, 0, 100, 10}]}, | |
stuff | |
] | |
); | |
MakeRadarPlot[person_, s___] := RadarChart[ | |
types /. (person /. averagebytype), | |
LabelingFunction -> (Placed[types[[#2[[2]]]], "RadialCenter"] &), | |
PlotRange -> {-100, 100}, | |
Epilog -> Style[Text[person], Bold, Large], | |
TicksStyle -> Directive[White, FontSize -> 0], | |
ImageSize -> {500, 500}, | |
s | |
]; | |
ExportRadarPlots[] := Export["~/Dropbox/Gráficas/" <> # <> ".png", | |
MakeRadarPlot[#, ImageSize -> {500, 500}] | |
] & /@ people; | |
rawdata = Import[ | |
"https://docs.google.com/spreadsheet/pub?key=\ | |
0AmUMewVtwa9TdFRNWEJFWGJpVTllXzNOd1JwS19kNFE&single=true&gid=0&output=csv", | |
CharacterEncoding -> "UTF-8"]; | |
data = Rest /@ Transpose@rawdata; | |
(* [[1]] = names, [[2]] = types, [[3;;]] = people + scores *) | |
rawscores = data[[3 ;;]]; | |
people = rawdata[[1]][[3 ;;]]; | |
scores = Flatten[ MapThread[({#1 -> #2} & ), {people, rawscores}], 1]; | |
types = (StringSplit[#, RegularExpression[",(\ )*"]] & /@ data[[2]]) // | |
Flatten // DeleteDuplicates // Sort; | |
problemtypes = (# -> | |
Module[{currtype = #}, | |
Flatten@Position[ | |
data[[2]], _?(StringQ [#] && | |
StringMatchQ[#, RegularExpression[".*" <> currtype <> ".*"]] &) | |
] | |
] &) /@ types; | |
countbytype = (# -> Length[# /. problemtypes] &) /@ types; | |
averagebytype = (# -> | |
MapThread[(#1 -> | |
Total[(#2 /. scores)[[(#1 /. problemtypes)]] /. | |
"-" -> 0]/(Length[#1 /. problemtypes] - | |
Count[(#2 /. scores)[[(#1 /. problemtypes)]], "-"]) | |
&), {types, Table[#, {Length@types}]}] | |
&) /@ people; | |
ExportRadarPlots[]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment