| 1 |
% mpkiviat.mp v0.1 |
| 2 |
% @Author: Maxime Chupin |
| 3 |
% @Date: 22 may 2024 |
| 4 |
% @License: LATEX Project Public License 1.3c. |
| 5 |
|
| 6 |
if not known mplib: input latexmp fi; |
| 7 |
pair _axis[]; |
| 8 |
|
| 9 |
numeric _axis_number; |
| 10 |
numeric _kiviat_unit; |
| 11 |
|
| 12 |
numeric _lattice_unit,_lattice_max,_value_max,_unit_value; |
| 13 |
_lattice_unit := 1; |
| 14 |
_lattice_max :=10; |
| 15 |
_value_max := 10; |
| 16 |
_unit_value := 1; |
| 17 |
_kiviat_unit:=0.3cm; |
| 18 |
|
| 19 |
|
| 20 |
pair _kiviat_lines[][]; |
| 21 |
path _kiviat_path[]; |
| 22 |
color _kiviat_lines_color[]; |
| 23 |
numeric _line_nbr; |
| 24 |
_line_nbr:=0; |
| 25 |
|
| 26 |
def set_lattice_grid(expr u,m)= |
| 27 |
save _nbr; |
| 28 |
_nbr := (m/u); |
| 29 |
_value_max := m; |
| 30 |
_unit_value := u; |
| 31 |
_lattice_max := floor(_nbr); |
| 32 |
enddef; |
| 33 |
|
| 34 |
|
| 35 |
string _axis_names[]; |
| 36 |
|
| 37 |
color _kiviat_lattice_color, _kiviat_axis_color; |
| 38 |
_kiviat_axis_color:= black; |
| 39 |
_kiviat_lattice_color:= 0.8*white; |
| 40 |
|
| 41 |
vardef set_axis_color(expr c)= |
| 42 |
_kiviat_axis_color:= c; |
| 43 |
enddef; |
| 44 |
|
| 45 |
def set_kiviat_unit(expr u)= |
| 46 |
_kiviat_unit:=u; |
| 47 |
enddef; |
| 48 |
|
| 49 |
vardef set_lattice_color(expr c)= |
| 50 |
_kiviat_lattice_color:= c; |
| 51 |
enddef; |
| 52 |
|
| 53 |
vardef set_axis(text t)= |
| 54 |
_line_nbr:=0; |
| 55 |
save _st,_i; |
| 56 |
_axis_number:=0; |
| 57 |
for _st=t: |
| 58 |
_axis_number:= _axis_number+1; |
| 59 |
_axis_names[_axis_number]:=_st; |
| 60 |
endfor |
| 61 |
for i:=1 upto _axis_number: |
| 62 |
_axis[i]:=(cosd(i/_axis_number*360)*_lattice_max*_kiviat_unit,sind(i/_axis_number*360)*_lattice_max*_kiviat_unit); |
| 63 |
endfor |
| 64 |
enddef; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
boolean _legend; |
| 71 |
_legend:=true; |
| 72 |
|
| 73 |
% function to determine the point of the bb that intersect the line |
| 74 |
% determined by the angle of the corresponding axis |
| 75 |
vardef _pointbb (expr p, a)= |
| 76 |
save _bb,_d,_point; |
| 77 |
path _bb,_d; |
| 78 |
pair _point; |
| 79 |
_bb := bbox p; |
| 80 |
_d := (0,0)--(3000*cosd(a),3000*sind(a)); |
| 81 |
_point := _bb intersectionpoint _d; |
| 82 |
_point |
| 83 |
enddef; |
| 84 |
|
| 85 |
% set legends true/false |
| 86 |
vardef set_axis_legends(expr b)= |
| 87 |
_legend:=b; |
| 88 |
enddef; |
| 89 |
|
| 90 |
def draw_axis = |
| 91 |
save _plabel; |
| 92 |
picture _plabel; |
| 93 |
for i:=_lattice_unit step _lattice_unit until _lattice_max : |
| 94 |
draw for j:=1 upto _axis_number: (i/_lattice_max)*_axis[j]--endfor cycle withcolor |
| 95 |
_kiviat_lattice_color; |
| 96 |
endfor |
| 97 |
for i:=1 upto _axis_number: |
| 98 |
drawarrow (0,0)--1.2*_axis[i] withcolor _kiviat_axis_color; |
| 99 |
endfor |
| 100 |
if(_legend): |
| 101 |
for i:=1 upto _axis_number: |
| 102 |
_plabel := image(label(textext(_axis_names[i]),origin)); % passage |
| 103 |
% par image et label pour centrer la bbox |
| 104 |
draw _plabel |
| 105 |
shifted (1.2*_axis[i]+labeloffset*(cosd(angle _axis[i]),sind(angle |
| 106 |
_axis[i]))+_pointbb(_plabel,angle _axis[i])) ; |
| 107 |
endfor; |
| 108 |
fi |
| 109 |
enddef; |
| 110 |
|
| 111 |
def draw_grad(expr p,s)(expr j)= |
| 112 |
for i:=1 upto _lattice_max: |
| 113 |
unfill bbox (textext(p&decimal(i*_unit_value)&s) shifted (i/_lattice_max*_axis[j])); |
| 114 |
draw textext(p&decimal(i*_unit_value)&s) shifted (i/_lattice_max*_axis[j]); |
| 115 |
endfor |
| 116 |
enddef; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
vardef _build_line(text t)= |
| 121 |
save _values,_i; |
| 122 |
_line_nbr:=_line_nbr+1; |
| 123 |
_i:=0; |
| 124 |
for _values=t: |
| 125 |
_i:=_i+1; |
| 126 |
_kiviat_lines[_line_nbr][_i]:=(_values/_value_max)*_axis[_i]; |
| 127 |
if(_i=1): |
| 128 |
_kiviat_path[_line_nbr]:=_kiviat_lines[_line_nbr][_i]; |
| 129 |
else: |
| 130 |
_kiviat_path[_line_nbr]:=_kiviat_path[_line_nbr]--_kiviat_lines[_line_nbr][_i]; |
| 131 |
fi |
| 132 |
endfor |
| 133 |
_kiviat_path[_line_nbr]:=_kiviat_path[_line_nbr]--cycle; |
| 134 |
enddef; |
| 135 |
|
| 136 |
|
| 137 |
boolean _line_mark_b; |
| 138 |
_line_mark_b:=true; |
| 139 |
numeric _line_mark_scale; |
| 140 |
_line_mark_scale:=1; |
| 141 |
|
| 142 |
def set_line_mark_scale(expr n)= |
| 143 |
_line_mark_scale:=n; |
| 144 |
enddef; |
| 145 |
|
| 146 |
def set_line_mark(expr b)= |
| 147 |
_line_mark_b:=b; |
| 148 |
enddef; |
| 149 |
|
| 150 |
def line_mark_square(expr p)= |
| 151 |
((((-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle) scaled _line_mark_scale) shifted p) |
| 152 |
enddef; |
| 153 |
|
| 154 |
def line_mark_circle(expr p)= |
| 155 |
((fullcircle scaled 2.5 scaled _line_mark_scale) shifted p) |
| 156 |
enddef; |
| 157 |
|
| 158 |
string _mark_type; |
| 159 |
_mark_type := "square"; |
| 160 |
string _save_type[]; |
| 161 |
boolean _save_mark_b[]; |
| 162 |
numeric _save_scale[]; |
| 163 |
|
| 164 |
def set_line_mark_type(expr s)= |
| 165 |
_mark_type:=s; |
| 166 |
enddef; |
| 167 |
|
| 168 |
def _mark_line(expr c)= |
| 169 |
_save_type[_line_nbr]:=_mark_type; |
| 170 |
_save_scale[_line_nbr]:=_line_mark_scale; |
| 171 |
for i:=1 upto _axis_number: |
| 172 |
fill |
| 173 |
if(_mark_type="square"): |
| 174 |
line_mark_square |
| 175 |
elseif(_mark_type="circle"): |
| 176 |
line_mark_circle |
| 177 |
elseif(_mark_type="custom"): |
| 178 |
line_mark_custom |
| 179 |
fi |
| 180 |
(_kiviat_lines[_line_nbr][i]) withcolor c; |
| 181 |
endfor; |
| 182 |
enddef; |
| 183 |
|
| 184 |
vardef draw_line(text t)(expr c)= |
| 185 |
_build_line(t); |
| 186 |
_kiviat_lines_color[_line_nbr]:=c; |
| 187 |
draw _kiviat_path[_line_nbr] withcolor c; |
| 188 |
_save_mark_b[_line_nbr]:=_line_mark_b; |
| 189 |
if(_line_mark_b): |
| 190 |
_mark_line(c); |
| 191 |
fi; |
| 192 |
enddef; |
| 193 |
|
| 194 |
|
| 195 |
vardef filldraw_line(text t)(expr c)= |
| 196 |
_build_line(t); |
| 197 |
_kiviat_lines_color[_line_nbr]:=c; |
| 198 |
alphafill(_kiviat_path[_line_nbr],c,0.1); |
| 199 |
draw _kiviat_path[_line_nbr] withcolor c; |
| 200 |
_save_mark_b[_line_nbr]:=_line_mark_b; |
| 201 |
if(_line_mark_b): |
| 202 |
_mark_line(c); |
| 203 |
fi; |
| 204 |
enddef; |
| 205 |
|
| 206 |
vardef _anchor@# (expr p)= |
| 207 |
if(str @# = "top"): |
| 208 |
(1/2*(urcorner p)+1/2*(ulcorner p)) |
| 209 |
elseif(str @# = "bot"): |
| 210 |
(1/2*(lrcorner p)+1/2*(llcorner p)) |
| 211 |
elseif(str @# = "lft"): |
| 212 |
(1/2*(llcorner p)+1/2*(ulcorner p)) |
| 213 |
elseif(str @# = "rt" ): |
| 214 |
(1/2*(lrcorner p)+1/2*(urcorner p)) |
| 215 |
elseif(str @# = "urt"): |
| 216 |
(urcorner p) |
| 217 |
elseif(str @# = "ulft"): |
| 218 |
(ulcorner p) |
| 219 |
elseif(str @# = "lrt"): |
| 220 |
(lrcorner p) |
| 221 |
elseif(str @# = "llft"): |
| 222 |
(llcorner p) |
| 223 |
elseif(str @# = "c"): |
| 224 |
(1/4*(lrcorner p)+1/4*(urcorner p)+1/4*(llcorner p)+1/4*(ulcorner p)) |
| 225 |
else: |
| 226 |
(1/4*(lrcorner p)+1/4*(urcorner p)+1/4*(llcorner p)+1/4*(ulcorner p)) |
| 227 |
fi |
| 228 |
|
| 229 |
enddef; |
| 230 |
|
| 231 |
vardef draw_legends@# (text l) = |
| 232 |
save _mypic,i,_s_scale; |
| 233 |
_s_scale := _line_mark_scale; |
| 234 |
picture _mypic; |
| 235 |
_mypic = image( |
| 236 |
i:=0; |
| 237 |
for s=l: |
| 238 |
i:=i+1; |
| 239 |
draw ((0,0)--(0.7*_kiviat_unit,0)) shifted (0,i*(_kiviat_unit)) withcolor |
| 240 |
_kiviat_lines_color[i]; |
| 241 |
if(_save_mark_b[i]): |
| 242 |
_line_mark_scale:=_save_scale[i]; |
| 243 |
fill |
| 244 |
if(_save_type[i]="square"): |
| 245 |
line_mark_square |
| 246 |
elseif(_save_type[i]="circle"): |
| 247 |
line_mark_circle |
| 248 |
elseif(_save_type[i]="custom"): |
| 249 |
line_mark_custom |
| 250 |
fi |
| 251 |
(0.5[(0,0),(0.7*_kiviat_unit,0)] )shifted (0,i*(_kiviat_unit)) |
| 252 |
withcolor _kiviat_lines_color[i]; |
| 253 |
fi |
| 254 |
label.rt(textext(s),(0.7*_kiviat_unit,0) shifted (0,i*(_kiviat_unit))); |
| 255 |
endfor |
| 256 |
draw bbox currentpicture; |
| 257 |
_line_mark_scale := _s_scale ; |
| 258 |
); |
| 259 |
draw _mypic |
| 260 |
if(str @# = "top"): |
| 261 |
shifted (_anchor.top(currentpicture)-_anchor.bot(_mypic)) |
| 262 |
elseif(str @# = "bot"): |
| 263 |
shifted (_anchor.bot(currentpicture)-_anchor.top(_mypic)) |
| 264 |
elseif(str @# = "lft"): |
| 265 |
shifted (_anchor.lft(currentpicture)-_anchor.rt(_mypic)) |
| 266 |
elseif(str @# = "rt" ): |
| 267 |
shifted (_anchor.rt(currentpicture)-_anchor.lft(_mypic)) |
| 268 |
elseif(str @# = "urt"): |
| 269 |
shifted (_anchor.urt(currentpicture)-_anchor.ulft(_mypic)) |
| 270 |
elseif(str @# = "ulft"): |
| 271 |
shifted (_anchor.ulft(currentpicture)-_anchor.urt(_mypic)) |
| 272 |
elseif(str @# = "lrt"): |
| 273 |
shifted (_anchor.lrt(currentpicture)-_anchor.llft(_mypic)) |
| 274 |
elseif(str @# = "llft"): |
| 275 |
shifted (_anchor.llft(currentpicture)-_anchor.lrt(_mypic)) |
| 276 |
else: |
| 277 |
shifted (_anchor.rt(currentpicture)-_anchor.lft(_mypic)) |
| 278 |
fi |
| 279 |
; |
| 280 |
enddef; |
| 281 |
% transparency Phan : http://www-math.univ-poitiers.fr/~phan/metalpha.html |
| 282 |
% revisité par Jean-Michel Sarlat |
| 283 |
color _tc; |
| 284 |
numeric _ta; |
| 285 |
picture _ap; |
| 286 |
|
| 287 |
def _AlphaFill expr c = |
| 288 |
_ap := nullpicture; |
| 289 |
_AlphaPicture(currentpicture,c); |
| 290 |
addto currentpicture also _ap; |
| 291 |
enddef; |
| 292 |
|
| 293 |
vardef _AlphaPicture(expr p,c) = |
| 294 |
save p_; picture p_; |
| 295 |
p_ := nullpicture; |
| 296 |
addto p_ contour c withcolor _ta[background,_tc]; |
| 297 |
for p__ within p: |
| 298 |
if (not clipped p__) and (not bounded p__): |
| 299 |
addto p_ also p__ withcolor |
| 300 |
_ta[(redpart p__,greenpart p__,bluepart p__), _tc]; |
| 301 |
else: |
| 302 |
begingroup save _ap; |
| 303 |
picture _ap; _ap = nullpicture; |
| 304 |
_AlphaPicture(p__,pathpart p__); |
| 305 |
addto p_ also _ap; |
| 306 |
endgroup; |
| 307 |
fi |
| 308 |
endfor; |
| 309 |
clip p_ to c; |
| 310 |
addto _ap also p_; |
| 311 |
enddef; |
| 312 |
|
| 313 |
vardef alphafill(expr p,c,a) = |
| 314 |
_tc := c; _ta := a; |
| 315 |
_AlphaFill p |
| 316 |
enddef; |
| 317 |
|
| 318 |
endinput; |