Debian Codegraph @

texmf-dist/metapost/piechartmp/piechartmp.mp

raw
1 %H package name: piechartMP
2 %H description: Metapost package for 2D/3D pie charts
3 %H file: piechartmp.mp
4 %H author: Jens-Uwe Morawski <[email protected]>
5 %H version: 0.3.0 2002/05/14
6
7 %L Copyright 2002, Jens-Uwe Morawski
8 %L License: LPPL 1.0, see LEGAL
9
10
11 %I * all user commands start with uppercase letters:
12 %I Segment, SegmentState,
13 %I SetupColors, SetupNumbers, SetupText,
14 %I SetupValue, SetupPercent, SetupName,
15 %I PieChart, Label
16 %I * user variables start with uppercase letters:
17 %I PiechartBBox, R
18 %I * all internal functions start with '_' except
19 %I SegmentColor, SegmentPoint
20 %I * all internal variables start with 'pc_' except
21 %I auto, hidden, normal, invisible, this,
22 %I inwards, outwards, name, percent, value
23
24 %\
25 % initialisation
26 %\
27 if known piechart_module: endinput ; fi ;
28 boolean piechart_module ; piechart_module := true ;
29
30 % === data / input routines === (pc-input.mp)
31 %\
32 % vars / internals
33 %\
34 newinternal hidden ; hidden := -1 ;
35 newinternal invisible ; invisible := 0 ;
36 newinternal normal ; normal := 1 ;
37
38 %\
39 % data input/ user values / segment state
40 %\
41 numeric pc_Count ; pc_Count := 0 ; % number of defined segments
42 numeric pc_xValue[], pc_xOffset[], pc_xState[];
43 color pc_xFill[] ;
44 string pc_n_Label[], pc_v_Label[] ;
45
46 def Segment(text s) =
47 begingroup;
48 save i ; numeric i ; i:=0;
49 pc_Count := pc_Count + 1 ;
50 for e=s:
51 i := i+1 ;
52 if i=1:
53 pc_xValue[pc_Count] := e ;
54 pc_n_Label[pc_Count] := _numericToString(pc_Count, -1) ;
55 pc_xFill[pc_Count] := auto ;
56 pc_v_Label[pc_Count] := _numericToString(e, -1) ;
57 elseif i=2:
58 pc_n_Label[pc_Count] := e ;
59 elseif i=3:
60 pc_xFill[pc_Count] := _applyColorOrPattern(e) ;
61 elseif i=4:
62 pc_v_Label[pc_Count] := e ;
63 fi;
64 endfor;
65
66 if i=0:
67 errmessage "Segment: too few arguments for segment " &
68 (decimal pc_Count) ;
69 fi;
70
71 pc_xState[pc_Count] := normal ;
72 pc_xOffset[pc_Count] := 0 ;
73 endgroup;
74 enddef;
75
76 def SegmentState(expr s, st, sh ) =
77 if not _isThis(st):
78 pc_xState[s] := st ;
79 fi;
80
81 if not _isThis(sh):
82 pc_xOffset[s] := sh ;
83 fi;
84 enddef;
85
86 %C reset diagramm data
87 def ResetSegments =
88 pc_Count := 0 ;
89 enddef;
90
91 % === drawing routines === (pc-draw.mp)
92 %\
93 % vars / internals
94 %\
95
96 newinternal R;
97 pair pc_Centre; pc_Centre := origin ;
98 numeric pc_Offset ;
99 numeric pc_Depth ;
100 numeric pc_View ;
101 numeric pc_Sum ;
102 numeric pc_xStart[], pc_xEnd[] ;
103
104
105 boolean pc_Metafun ; pc_Metafun := false ;
106 if known context_spec : pc_Metafun := true ; fi ;
107
108
109 %\
110 % diagram drawing
111 %\
112
113 %C PieChart(<diagramm radius>, <thickness in R>, <angle>, <rotation>,<global offset in R>)
114 def PieChart (expr u, d, va, r, go) =
115 begingroup ;
116 save rot, pointer ; numeric rot , pointer;
117 R := u ;
118 if ( va < 90 ) and ( va >= 0 ):
119 pc_View := va ;
120 else:
121 message "Warning: Observation angle of " & ( decimal va ) & " is not valid. Substituted with 65." ;
122 pc_View := 65 ;
123 fi ;
124
125 pc_Offset := go ;
126
127 pc_Sum := 0 ;
128 for i=1 upto pc_Count:
129 if (pc_xState[i] >= 0):
130 pc_Sum := pc_Sum + pc_xValue[i] ;
131 fi ;
132 endfor;
133
134 if (( r >= 0 ) and ( r <= 359 )):
135 rot := r ;
136 else:
137 rot := 0 ;
138 message "Warning: Rotation " & (decimal r) & " is not within 0-359. Defaults to 0" ;
139 fi ;
140 pointer := rot ;
141 for i=1 upto pc_Count:
142 if ( pc_xState[i] >= 0 ):
143 pc_xStart[i] := pointer ;
144 pc_xEnd[i] := pc_xStart[i] + (pc_xValue[i] / pc_Sum)*360.00 ;
145 else:
146 pc_xStart[i] := pointer ;
147 pc_xEnd[i] := pointer ;
148 fi ;
149 pointer := pc_xEnd[i] ;
150 endfor;
151
152 pc_Depth := d*R*(sind pc_View) ;
153 if (pc_View > 0): _drawSides ; fi ;
154 _drawTop ;
155 endgroup ;
156 enddef;
157
158 %C draws top sides of all visible segments
159 def _drawTop =
160 begingroup ;
161 save c , a, e, p ; path c, a, e ; picture p ;
162 for i=1 upto pc_Count:
163 if ( pc_xState[i] > 0) :
164 c := fullcircle rotated (pc_xStart[i]) scaled (2*R) ;
165 e := origin--(1.1*R*(cosd pc_xEnd[i]) , 1.1*R*(sind pc_xEnd[i]) ) ;
166 c := c cutafter e ; c := origin--c--cycle ;
167 fill ( c yscaled (cosd pc_View) ) shifted _calcSegmentShift(i) withcolor SegmentColor(i) ;
168 if ( greenpart pc_xFill[i]) = -2 :
169 p := _makePattern(i, c) ;
170 draw ( p yscaled (cosd pc_View) ) shifted _calcSegmentShift(i) withcolor _patternColor(i) ;
171 fi;
172 fi ;
173 endfor;
174 endgroup ;
175 enddef;
176
177 %C visual ordering of side elements
178 def _drawSides =
179 begingroup ;
180 save _etd, c , a , e;
181 numeric _etd[][] , c , a , e;
182 c := 0 ;
183 for i=1 upto pc_Count:
184 if (pc_xState[i] > 0) :
185 if _visibleStart(i):
186 c := c + 1 ;
187 _etd[c][1] := 1 ;
188 _etd[c][2] := sind ( _normalizeAngle(pc_xStart[i])) ;
189 _etd[c][3] := i ;
190 _etd[c][4] := _normalizeAngle(pc_xStart[i]) ;
191 fi
192 if _visibleEnd(i):
193 c := c + 1 ;
194 _etd[c][1] := 1 ;
195 _etd[c][2] := sind ( _normalizeAngle(pc_xEnd[i])) ;
196 _etd[c][3] := i ;
197 _etd[c][4] := _normalizeAngle(pc_xEnd[i]) ;
198 fi
199 if _visibleOutside(i):
200 c := c + 1 ;
201 if _outsideIsDivided(i):
202 _etd[c][1] := 2 ;
203 _etd[c][3] := i ;
204 _etd[c][4] := _normalizeAngle(pc_xStart[i]) ;
205 _etd[c][5] := 360 ;
206 c := c + 1 ;
207 _etd[c][1] := 2 ;
208 _etd[c][3] := i ;
209 _etd[c][4] := 180 ;
210 _etd[c][5] := _normalizeAngle(pc_xEnd[i]) ;
211 else :
212 a := pc_xStart[i] ;
213 e := pc_xEnd[i] ;
214
215 if ( (sind a) >= 0 ):
216 a := 180 ;
217 fi ;
218 if ( (sind e) >= 0 ) :
219 e := 360 ;
220 fi ;
221
222 _etd[c][1] := 2 ;
223 _etd[c][3] := i ;
224 _etd[c][4] := _normalizeAngle(a) ;
225 _etd[c][5] := _normalizeAngle(e) ;
226 fi
227 fi
228 fi
229 endfor;
230
231 for i=1 upto c:
232 if (_etd[i][1] = 2 ):
233 if (( (cosd _etd[i][4])*(cosd _etd[i][5]) ) >= 0):
234 _etd[i][2] := sind (0.5*(_etd[i][4] + _etd[i][5]));
235 else :
236 _etd[i][2] := -1 ;
237 fi
238 fi
239 endfor;
240
241 for i=1 upto c :
242 a := -2 ; e := 0 ;
243 for j=1 upto c:
244 if (_etd[j][1] > 0) and (_etd[j][2] > a):
245 e := j ;
246 a := _etd[j][2] ;
247 fi
248 endfor;
249 if (e > 0):
250 if (_etd[e][1] = 1):
251 _drawInside(_etd[e][3],_etd[e][4]) ;
252 else:
253 _drawOutside(_etd[e][3],_etd[e][4],_etd[e][5]) ;
254 fi
255 _etd[e][1] := 0 ;
256 fi
257 endfor;
258 endgroup ;
259 enddef;
260
261 def _drawInside (expr s, a) =
262 begingroup ;
263 save b, e; path e, b;
264
265 e := origin--(R*(cosd a), R*(sind a)*(cosd pc_View) ) ;
266 b := (R*(cosd a), R*(sind a)*(cosd pc_View) )--origin ;
267 %if ( (sind a) > 0): b := b yscaled (1 + (0.1*(sind a)*(sind pc_View))); fi ;
268
269 b := b shifted (0, -pc_Depth) ;
270 e := b--e--cycle ; e := e shifted _calcSegmentShift(s) ;
271 fill e withcolor _calcInsideColor(s,a) ;
272 endgroup ;
273 enddef;
274
275
276 def _drawOutside (expr s, a, e) =
277 begingroup ;
278 save p, h, g ;
279 path p, h, g ;
280 if (pc_Metafun and (a < 270) and (e > 270)) :
281 _drawOutside(s,a,270) ;
282 _drawOutside(s,270,e) ;
283 else :
284 g := fullcircle xscaled (2*R) yscaled (2*R*(cosd pc_View));
285 g := subpath (4,8) of g ; %top
286 h := g shifted (0, -pc_Depth) ; % below
287 g := g xscaled -1 ; % in the reverse direction
288
289 p := (R*(cosd e),0)--(R*(cosd e),-1.1*(R*(cosd pc_View)+pc_Depth)) ;
290 h := h cutafter p ;
291 g := g cutbefore p ;
292
293 p := (R*(cosd a),0)--(R*(cosd a),-1.1*(R*(cosd pc_View)+pc_Depth)) ;
294 h := h cutbefore p ;
295 g := g cutafter p ;
296
297 p := h--g--cycle ; p := p shifted _calcSegmentShift(s) ;
298 if pc_Metafun:
299 linear_shade(p,0,_calcOutsideColor(s,a),_calcOutsideColor(s,e)) ;
300 else :
301 fill p withcolor _calcOutsideColor(s, (0.5*(a+e)) ) ;
302 fi ;
303 fi ;
304 endgroup ;
305 enddef;
306
307 %\
308 % help macros
309 %\
310
311 vardef _visibleStart (expr s) =
312 save b; boolean b;
313 b:= ( ( cosd ( _normalizeAngle(pc_xStart[s]) )) > 0 ) and
314 ( (pc_Offset > 0) or (pc_xOffset[s] > 0) or
315 (pc_xState[ _prevSegment(s) ] = 0) or
316 (pc_xOffset[_prevSegment(s)] > 0) );
317 b
318 enddef;
319
320 vardef _visibleEnd (expr s) =
321 save b; boolean b;
322 b:= ( ( cosd ( _normalizeAngle(pc_xEnd[s]) )) < 0 ) and
323 ( (pc_Offset > 0) or (pc_xOffset[s] > 0) or
324 (pc_xState[ _nextSegment(s) ] = 0) or
325 (pc_xOffset[_nextSegment(s)] > 0) );
326 b
327 enddef;
328
329 vardef _visibleOutside (expr s) =
330 save b; boolean b;
331 b := ((sind pc_xStart[s] < 0) or (sind (pc_xEnd[s]) < 0) or
332 (sind (0.5*(pc_xStart[s] + pc_xEnd[s])) < 0)) ;
333 b
334 enddef;
335
336 vardef _outsideIsDivided (expr s) =
337 save b; boolean b;
338 b := ( ( (sind pc_xStart[s]) < 0 ) and
339 ( (sind pc_xEnd[s] ) < 0 ) and
340 ( (sind (0.5*(pc_xStart[s]+pc_xEnd[s])) ) >= 0 ) );
341 b
342 enddef;
343
344 % === colors ==== (pc-color.mp)
345 %\
346 % vars / internals
347 %\
348 color auto ; auto := (-1,-1,-1) ;
349
350 %\
351 % color
352 %\
353
354 %C returns the color of the segment with number s;
355 %C if the segment uses pattern fill it returns the
356 %C background color
357 vardef SegmentColor(expr s) =
358 save tC; color tC;
359 if pc_xFill[s] = (-1,-1,-1):
360 tC := _autoColor( 0.5*(pc_xStart[s]+pc_xEnd[s]) - pc_xStart[1] ) ;
361 elseif ( (greenpart pc_xFill[s]) = -2 ) :
362 tC := pc_patternBGColor[(bluepart pc_xFill[s])] ;
363 if tC = (-1,-1,-1):
364 tC := _autoColor( 0.5*(pc_xStart[s]+pc_xEnd[s]) - pc_xStart[1] ) ;
365 fi ;
366 else:
367 tC := pc_xFill[s] ;
368 fi;
369 if pc_sGrayscale:
370 tC := (0.3*(redpart tC) + 0.59*(greenpart tC) + 0.11*(bluepart tC))*white ;
371 fi ;
372 tC
373 enddef;
374
375 %C returns the foreground color of the pattern
376 vardef _patternColor (expr s) =
377 save tC, v, h ; color tC; numeric v, h ;
378 tC := pc_patternFGColor[(bluepart pc_xFill[s])] ;
379 if tC = (-1,-1,-1):
380 tC := SegmentColor(s) ;
381 tC := _rgbToHsv(tC) ;
382 h := redpart tC ;
383 h := h - 180 ;
384 if h<0: h := h + 360 ; fi ;
385 v := bluepart tC ;
386 v := 1 - v ;
387 tC := ( h , greenpart tC , v) ;
388 tC := _hsvToRgb(tC) ;
389 fi;
390 if pc_sGrayscale:
391 tC := (0.3*(redpart tC) + 0.59*(greenpart tC) + 0.11*(bluepart tC))*white ;
392 fi ;
393 tC
394 enddef;
395
396 %C returns the color of the insides in 3D view of the segment s
397 %C a is the angle/direction of the side in the diagram
398 vardef _calcInsideColor (expr s, a) =
399 save tC, f; color tC; numeric f ;
400 f := cosd ( (abs(cosd a))*pc_View ) ;
401 tC := SegmentColor(s) + f*(xpart pc_sShadeSV)*( _minSaturation(SegmentColor(s)) - SegmentColor(s) ) ;
402 tC := tC + f*(ypart pc_sShadeSV)*( black - tC ) ;
403 tC
404 enddef;
405
406 %C returns the color of the outsides in 3D view of the segment s
407 %C a is the angle/direction of the side in the diagram
408 vardef _calcOutsideColor (expr s, a) =
409 save tC, f; color tC; numeric f ;
410 f := cosd ( (abs(sind a))*pc_View ) ;
411 tC := SegmentColor(s) + f*(xpart pc_sShadeSV)*( _minSaturation(SegmentColor(s)) - SegmentColor(s) ) ;
412 tC := tC + f*(ypart pc_sShadeSV)*( black - tC ) ;
413 tC
414 enddef;
415
416 %\
417 % help macros
418 %\
419
420 %C return a color from c as if saturation is set to zero
421 vardef _minSaturation (expr c)=
422 save sat; numeric sat; sat := 0 ;
423 sat := 0.3*(redpart c) + 0.59*(greenpart c) + 0.11*(bluepart c) ;
424 sat*white
425 enddef;
426
427
428 %C automatic color values
429 vardef _autoColor(expr a) =
430 _hsvToRgb( (a, (xpart pc_sAutoSV), (ypart pc_sAutoSV)) )
431 enddef;
432
433 vardef _hsvToRgb(expr c) =
434 save h,s,v, H,o, b, d, pc, sc;
435 numeric h,s,v,H, o, b, d, pc, sc ;
436
437 h := redpart c ;
438 s := greenpart c ;
439 v := bluepart c ;
440
441 H := _normalizeAngle(h) / 60 ;
442 pc := floor H ;
443 sc := H - pc ;
444 o := (1 - s)*v ;
445 b := (1 - (sc*s) )*v;
446 d := (1 - ((1-sc)*s) )*v ;
447
448 if (pc = 0) or (pc = 6):
449 (v,d,o)
450 elseif pc = 1:
451 (b,v,o)
452 elseif pc = 2:
453 (o,v,d)
454 elseif pc = 3:
455 (o,b,v)
456 elseif pc = 4:
457 (d,o,v)
458 else:
459 (v,o,b)
460 fi
461 enddef;
462
463 vardef _rgbToHsv (expr c) =
464 save r,g,b,h,s,v, _r, _g, _b, min, max ;
465 numeric r,g,b,h,s,v, _r, _g, _b, min, max ;
466
467 r := redpart c ;
468 g := greenpart c ;
469 b := bluepart c ;
470
471 min := r ; max := r ;
472
473 if (g < min): min := g ; fi ;
474 if (g > max): max := g ; fi ;
475 if (b < min): min := b ; fi ;
476 if (b > max): max := b ; fi ;
477
478 if max > 0:
479 s := (max - min)/max ; v := max ;
480 else:
481 s := 0; v := 0 ;
482 fi ;
483
484
485 if (s = 0):
486 h := 0 ; % is undefined
487 else:
488 _r := (max - r)/(max - min) ;
489 _g := (max - g)/(max - min) ;
490 _b := (max - b)/(max - min) ;
491
492 if ((r = max ) and (g = min)): h := 5 + _b ;
493 elseif ((r = max ) and (g <> min)): h := 1 - _g ;
494 elseif ((g = max ) and (b = min)): h := 1 + _r ;
495 elseif ((g = max ) and (b <> min)): h := 3 - _b ;
496 elseif (r = max): h := 3 + _g ;
497 else: h := 5 - _r ;
498 fi ;
499 fi ;
500 h := 60 * h ;
501 (h,s,v)
502 enddef;
503 % === pattern routines === (pc-pattr.mp)
504
505 %\
506 % vars
507 %\
508 numeric pc_patternType[] ;
509 color pc_patternFGColor[], pc_patternBGColor[] ;
510 pair pc_patternDimen[] ;
511
512 %\
513 % routines
514 %\
515
516 %C called via Segment();
517 vardef _applyColorOrPattern (expr c) =
518 if color c:
519 c
520 else:
521 if ( numeric c ):
522 if known pc_patternType[c]:
523 (-1,-2,c)
524 else:
525 errmessage "Pattern number " & (decimal c) & " not defined" ;
526 (-1,-1,-1)
527 fi
528 else:
529 show c ;
530 hide(errmessage "Not a valid pattern number.") ;
531 (-1,-1,-1)
532 fi
533 fi
534 enddef;
535
536 %C defines a pattern with number n, of type t
537 %C with foreground color f and background color b
538 %C with dimensions (pair) d
539 def DefinePattern (expr n, t, b, f, d) =
540 pc_patternType[n] := t ;
541 pc_patternFGColor[n] := f ;
542 pc_patternBGColor[n] := b ;
543 pc_patternDimen[n] := d ;
544 enddef;
545
546
547 %C returns a picture including the pattern
548 %C for segment s in path p
549 vardef _makePattern (expr s, p) =
550 save o, u, type, spacing, dimen, b, xs, ys, q ;
551 pair o, u ; numeric type, spacing, dimen ;
552 picture b ;
553 b := nullpicture ;
554 type := pc_patternType[(bluepart pc_xFill[s])] ;
555 dimen := ypart pc_patternDimen[(bluepart pc_xFill[s])] ;
556 spacing := xpart pc_patternDimen[(bluepart pc_xFill[s])] ;
557 o := ulcorner p ;
558 u := lrcorner p ;
559
560 if type = 0:
561 b := PrivatePattern (o, u, spacing, dimen) ;
562 elseif type = 1:
563 numeric ys ; path line ;
564 ys := ( ypart o ) + ypart (o - u ) - spacing ;
565 forever:
566 exitif ( ys <= ypart u ) ;
567 line := (xpart o, ys)--(xpart u, ys - ypart(o-u)) ;
568 addto b doublepath line withpen pencircle scaled dimen ;
569 ys := ys - spacing ;
570 endfor;
571 elseif type = 2:
572 numeric ys ; path line ;
573 ys := ( ypart o ) + ypart (o - u ) - spacing ;
574 forever:
575 exitif ( ys <= ypart u ) ;
576 line := (xpart o, ys - ypart(o-u) )--(xpart u, ys ) ;
577 addto b doublepath line withpen pencircle scaled dimen ;
578 ys := ys - spacing ;
579 endfor;
580 elseif type = 3:
581 numeric ys ; path line ;
582 ys := ( ypart o ) + ypart (o - u ) - spacing ;
583 forever:
584 exitif ( ys <= ypart u ) ;
585 line := (xpart o, ys - ypart(o-u) )--(xpart u, ys ) ;
586 addto b doublepath line withpen pencircle scaled dimen ;
587 line := (xpart o, ys )--(xpart u, ys - ypart(o-u) ) ;
588 addto b doublepath line withpen pencircle scaled dimen ;
589 ys := ys - spacing ;
590 endfor;
591 elseif type = 4:
592 numeric ys ; path line ;
593 ys := ( ypart o ) - spacing ;
594 forever:
595 exitif ( ys <= ypart u ) ;
596 line := (xpart o, ys )--(xpart u, ys ) ;
597 addto b doublepath line withpen pencircle scaled dimen ;
598 ys := ys - spacing ;
599 endfor;
600 elseif type = 5:
601 numeric xs ; path line ;
602 xs := ( xpart o ) - spacing ;
603 forever:
604 exitif ( xs >= xpart u ) ;
605 line := (xs, ypart o)--(xs, ypart u) ;
606 addto b doublepath line withpen pencircle scaled dimen ;
607 xs := xs + spacing ;
608 endfor;
609 elseif type = 6:
610 numeric xs, ys ; path line ;
611 xs := ( xpart o ) - spacing ;
612 forever:
613 exitif ( xs >= xpart u ) ;
614 line := (xs, ypart o)--(xs, ypart u) ;
615 addto b doublepath line withpen pencircle scaled dimen ;
616 xs := xs + spacing ;
617 endfor;
618 ys := ( ypart o ) - spacing ;
619 forever:
620 exitif ( ys <= ypart u ) ;
621 line := (xpart o, ys )--(xpart u, ys ) ;
622 addto b doublepath line withpen pencircle scaled dimen ;
623 ys := ys - spacing ;
624 endfor;
625 elseif type = 7:
626 numeric xs, ys ;
627 xs := ( xpart o ) ;
628 forever:
629 exitif ( xs >= xpart u ) ;
630 ys := ( ypart o ) ;
631 forever:
632 exitif ( ys <= ypart u ) ;
633 addto b doublepath (xs,ys) withpen pencircle scaled dimen ;
634 ys := ys - spacing ;
635 endfor;
636 xs := xs + spacing ;
637 endfor;
638 elseif type = 8:
639 numeric xs, ys ;
640 xs := ( xpart o ) ;
641 forever:
642 exitif ( xs >= xpart u ) ;
643 ys := ( ypart o ) ;
644 forever:
645 exitif ( ys <= ypart u ) ;
646 addto b doublepath (xs,ys) withpen pencircle scaled dimen ;
647 addto b doublepath (xs+.5*spacing,ys+.5*spacing) withpen pencircle scaled dimen ;
648 ys := ys - spacing ;
649 endfor;
650 xs := xs + spacing ;
651 endfor;
652 elseif type = 9:
653 numeric xs, ys ; path q[] ;
654 q1 := (0,0){curl 0}..(0.25*spacing, -0.2*spacing )..{curl 0}(0.5*spacing, 0) ;
655 q2 := q1..(q1 yscaled -1 shifted (.5*spacing, 0)) ;
656 xs := ( xpart o ) ;
657 forever:
658 exitif ( xs >= xpart u ) ;
659 ys := ( ypart o ) ;
660 forever:
661 exitif ( ys <= ypart u ) ;
662 addto b doublepath (q[2] shifted (xs,ys)) withpen pencircle scaled dimen ;
663 ys := ys - spacing ;
664 endfor;
665 xs := xs + spacing ;
666 endfor;
667 elseif type = 10:
668 numeric xs, ys ; path q ;
669 q := (0,0)--(0.5*spacing, -0.5*spacing )--(0,-spacing) ;
670 xs := ( xpart o ) ;
671 forever:
672 exitif ( xs >= xpart u ) ;
673 ys := ( ypart o ) ;
674 forever:
675 exitif ( ys <= ypart u ) ;
676 addto b doublepath (q shifted (xs,ys)) withpen pencircle scaled dimen ;
677 ys := ys - spacing ;
678 endfor;
679 xs := xs + spacing ;
680 endfor;
681 else:
682 errmessage "pattern type " & (decimal type) & " not defined."
683 b := nullpicture;
684 fi;
685 clip b to p ;
686 b
687 enddef;
688
689 vardef PrivatePattern (expr ulc, lrc, spc, lwd) =
690 save pic, cntr, c, k ;
691 picture pic ; pic := nullpicture ;
692 pair cntr ;
693 numeric c ;
694 path k ;
695
696 cntr := 0.5*( lrc + ulc ) ;
697
698 c := ((xpart (ulc - cntr))++(ypart (ulc - cntr))) / spc ;
699 c := ( floor c ) ;
700
701 for i=1 upto c:
702 k := fullcircle scaled (i*2*spc) shifted cntr ;
703 addto pic doublepath k withpen pencircle scaled lwd ;
704 endfor;
705 pic
706 enddef;
707
708 % === label commands === (pc-label.mp)
709
710 %\
711 % vars
712 %\
713
714
715 %C
716 color name, percent, value ;
717 name := (-1,-4,1) ; percent := (-1,-4,2) ; value := (-1,-4,3) ;
718 %C additional label positioning parameters
719 pair laboff.auto; laboff.auto=(-3,-3) ;
720 labxf.auto=0; labyf.auto=0;
721
722 %\
723 % text / labels / legend
724 %\
725
726 def Label = draw _theLabel enddef ;
727
728 vardef _theLabel@#(text t)(text d)(expr sp, sh) =
729 save pic ; picture pic ; pic := nullpicture ;
730 for S=t:
731 if S=0:
732 for i=1 upto pc_Count:
733 if pc_xState[i] = 1:
734 addto pic also _makeSegLabel.@#(i,sp,sh)(d) ;
735 fi;
736 endfor;
737 else:
738 if (S > 0) and (S <= pc_Count):
739 if pc_xState[S]= 1 :
740 addto pic also _makeSegLabel.@#(S,sp,sh)(d) ;
741 fi;
742 else:
743 message "Label: segment " & (decimal S) & " not defined.";
744 fi;
745 fi;
746 endfor;
747 pic
748 enddef;
749
750
751
752 vardef _makeSegLabel@# (expr s, sp, sh)(text dat) =
753 save d , t, p, o , pic, l;
754 numeric l;
755 pair p, o ;
756 picture t, pic ; pic := nullpicture ;
757 string d ; d := "" ;
758
759 for f=dat:
760 if color f:
761 if (bluepart f = 1 ):% name of segment
762 d := d & pc_n_Pre & pc_n_Label[s] & pc_n_Post ;
763 elseif (bluepart f = 2 ):% percent value
764 d := d & pc_p_Pre &
765 _numericToString( 100*(pc_xValue[s]/pc_Sum), pc_sPrecision) &
766 pc_p_Post ;
767 elseif (bluepart f = 3 ):%segment value
768 d := d & pc_v_Pre & pc_v_Label[s] & pc_v_Post ;
769 fi;
770 elseif string f:% free string
771 d := d & f ;
772 else:
773 errmessage "Not a valid label-data: "; show f ;
774 fi;
775 endfor;
776
777 t := _makeText(d) ;
778
779 if pair sh :
780 if sh = (0,0):
781 o := SegmentPoint(s,(0,0)) ;
782 p := SegmentPoint(s,sp) ;
783 addto pic doublepath p withpen currentpen ;
784 else:
785 o := SegmentPoint(s,sp) ;
786 p := o shifted sh ;
787 addto pic doublepath o--p withpen currentpen ;
788 fi;
789 else:
790 p := SegmentPoint(s,sp) ;
791 o := SegmentPoint(s,(0,0)) ;
792
793 fi;
794
795 if laboff.@# = (-3,-3): % auto justification
796
797 l := _normalizeAngle( angle(p-o) ) ;
798 laboff.auto := ((cosd l),(sind l)) ;
799 labxf.auto := ((cosd l) - 1) / -2 ;
800 labyf.auto := ((sind l) - 1) / -2 ;
801
802 if (not pair sh) and ((xpart sp) >= 1):
803 if (sind _normalizeAngle( angle(p-o) )) < 0 :
804 p := p + (0,-pc_Depth) ;
805 fi;
806 fi;
807
808 fi;
809 addto pic also thelabel.@#(t,p) ;
810
811 laboff.auto := (-3,-3) ;
812 pic
813 enddef;
814
815
816 %\
817 % help functions
818 %\
819
820 vardef _numericToString (expr n , c) =
821 save l, s ;
822 string l, s ;l := "";
823 if (c < 0):
824 s := decimal n ;
825 for i=1 upto (length s):
826 if ((substring (i-1,i) of s) = "."):
827 l := l & pc_sDelimiter;
828 else:
829 l := l & (substring (i-1,i) of s);
830 fi;
831 endfor;
832 elseif ( (c=3) and (n>32.767) ):
833 hide(message("Warning: Decrease precision for value " & decimal n & " to 2.")) ;
834 s := decimal ( round(n*(10**(c-1))) ) ;
835 l := substring (0,(length s)-2) of s &
836 pc_sDelimiter &
837 substring ((length s)-2,length s) of s ;
838 else:
839 s := decimal ( round(n*(10**c)) ) ;
840 if (c>0):
841 l := substring (0,(length s)-c) of s &
842 pc_sDelimiter &
843 substring ((length s)-c,length s) of s ;
844 else:
845 l := substring (0,(length s)-c) of s ;
846 fi ;
847 fi;
848 l
849 enddef;
850
851 % === text routines === (pc-text.mp)
852 %\
853 % vars
854 %\
855 string pc_TeXFile ;
856 pc_TeXFile := jobname & ".pct" ;
857
858
859 %\
860 % TeX
861 %\
862
863 vardef _makeText primary s =
864 if ( pc_sTextScheme > 0 ) :
865 write "verbatimtex" to pc_TeXFile ;
866 if ( pc_sTextScheme = 3 ):
867 write "%&latex" to pc_TeXFile ;
868 else:
869 write pc_sTeXFormat to pc_TeXFile ;
870 fi;
871 if ( pc_sTextScheme > 1 ):
872 write "\documentclass{minimal}" to pc_TeXFile ;
873 write pc_sTeXSettings to pc_TeXFile ;
874 write "\begin{document}" to pc_TeXFile ;
875 else:
876 write pc_sTeXSettings to pc_TeXFile ;
877 fi;
878 write "etex" to pc_TeXFile ;
879 write "btex "&s&" etex" to pc_TeXFile;
880 write EOF to pc_TeXFile;
881 scantokens ("input " & pc_TeXFile )
882 else:
883 s infont defaultfont scaled defaultscale
884 fi
885 enddef;
886
887 % === support routines === (pc-supp.mp)
888 %\
889 % vars / internals
890 %\
891 pair inwards, outwards ;
892 inwards := (.66, .5) ; outwards := (1.05, .5) ;
893
894 %\
895 % macros
896 %\
897 vardef SegmentPoint (expr s, t) =
898 save p, a ; pair p ;
899 numeric a ;
900 if (( ( xpart t) >=0 ) and
901 ( ( ypart t) >=0 ) and
902 ( ( ypart t) <=1 ) ):
903 a := pc_xStart[s] + (ypart t)*(pc_xEnd[s] - pc_xStart[s]) ;
904 p := ( ( xpart t)*R*(cosd a) , ( xpart t)*R*(sind a)*(cosd pc_View) ) ;
905 p := p + _calcSegmentShift(s) ;
906 else :
907 errmessage "SegmentPoint: point definition for segment " & (decimal s) & " is not valid. Substituted with default." ;
908 p := SegmentPoint(s,(1,0.5)) ;
909 fi ;
910 p
911 enddef;
912
913 vardef _calcSegmentShift(expr s) =
914 save o, x, y ;
915 numeric o, x, y ;
916 o := R*(pc_Offset + pc_xOffset[s]) ;
917 x := o*(cosd (0.5*(pc_xStart[s] + pc_xEnd[s]))) ;
918 y := o*(sind (0.5*(pc_xStart[s] + pc_xEnd[s])))*(cosd pc_View) ;
919 (x,y) + pc_Centre
920 enddef;
921
922 vardef _normalizeAngle (expr a) =
923 if a > 360:
924 a - 360
925 elseif a < 0:
926 360 + a
927 else:
928 a
929 fi
930 enddef;
931
932 vardef _nextSegment(expr s) =
933 if s < pc_Count:
934 if pc_xState[s+1] >= 0:
935 s+1
936 else:
937 _nextSegment(s+1)
938 fi
939 else:
940 if pc_xState[1] >= 0:
941 1
942 else:
943 _nextSegment(1)
944 fi
945 fi
946 enddef;
947
948 vardef _prevSegment(expr s) =
949 if s = 1:
950 if pc_xState[pc_Count] >= 0:
951 pc_Count
952 else:
953 _prevSegment(pc_Count)
954 fi
955 else:
956 if pc_xState[s-1] >= 0:
957 s-1
958 else:
959 _prevSegment(s-1)
960 fi
961 fi
962 enddef;
963
964 % === setup routines === (pc-setup.mp)
965
966 %\
967 % vars
968 %\
969
970 boolean pc_sGrayscale ; pc_sGrayscale := false ;
971 pair pc_sAutoSV ; pc_sAutoSV := (1,1) ;
972 pair pc_sShadeSV ; pc_sShadeSV := (.4,.3) ;
973
974 numeric pc_sPrecision ; pc_sPrecision := 0 ;
975 string pc_sDelimiter ; pc_sDelimiter := "." ;
976
977 string pc_sTeXFormat, pc_sTeXSettings ;
978 pc_sTeXFormat := "" ; pc_sTeXSettings := "" ;
979 numeric pc_sTextScheme ; pc_sTextScheme := 0 ;
980
981
982 string pc_p_Pre, pc_p_Post, pc_v_Pre, pc_v_Post,
983 pc_n_Pre, pc_n_Post ;
984 pc_p_Pre := "" ; pc_p_Post := "" ;
985 pc_v_Pre := "" ; pc_v_Post := "" ;
986 pc_n_Pre := "" ; pc_n_Post := "" ;
987
988
989 color this ; this := (-9,-9,-9) ;
990
991 vardef _isThis (expr t) =
992 if color t :
993 if t = this:
994 true
995 else:
996 false
997 fi
998 else:
999 false
1000 fi
1001 enddef;
1002
1003 def SetupColors (expr a, s, g) =
1004 if not _isThis(a) : pc_sAutoSV := a ; fi ;
1005 if not _isThis(s) : pc_sShadeSV := s ; fi ;
1006 if not _isThis(g) : pc_sGrayscale := g ; fi ;
1007 enddef;
1008
1009 def SetupNumbers (expr p, d) =
1010 if not _isThis(p) : pc_sPrecision := p ; fi ;
1011 if not _isThis(d) : pc_sDelimiter := d ; fi ;
1012 enddef;
1013
1014 def SetupText (expr l, f, s) =
1015 if not _isThis(l) : pc_sTextScheme := l ; fi ;
1016 if not _isThis(f) : pc_sTeXFormat := f ; fi ;
1017 if not _isThis(s) : pc_sTeXSettings := s ; fi ;
1018 enddef;
1019
1020 def SetupPercent (expr r, o) =
1021 if not _isThis(r) : pc_p_Pre := r ; fi ;
1022 if not _isThis(o) : pc_p_Post := o ; fi ;
1023 enddef;
1024
1025 def SetupValue (expr r, o) =
1026 if not _isThis(r) : pc_v_Pre := r ; fi ;
1027 if not _isThis(o) : pc_v_Post := o ; fi ;
1028 enddef;
1029
1030 def SetupName (expr r, o) =
1031 if not _isThis(r) : pc_n_Pre := r ; fi ;
1032 if not _isThis(o) : pc_n_Post := o ; fi ;
1033 enddef;
1034
1035 %C only activate bounding box code if PiechartBBox variable
1036 %C is set by user
1037 if not known PiechartBBox: endinput ; fi;
1038 % === Bounding Box === (pc-bbox.mp)
1039 %\
1040 % vars
1041 %\
1042
1043 pair pc_bULC, pc_bLRC ;
1044 string pc_BBoxFile ; pc_BBoxFile := jobname & ".pcb" ;
1045
1046 %\
1047 % init
1048 %\
1049 begingroup;
1050 save L ; string L ;
1051 L := readfrom pc_BBoxFile ;
1052 if ( L = EOF ): % file not readable
1053 pc_bULC := origin ; pc_bLRC := origin ;
1054 else:
1055 scantokens L ;
1056 L := readfrom pc_BBoxFile ;
1057 scantokens L ;
1058 fi ;
1059 endgroup;
1060
1061 %\
1062 % macros
1063 %\
1064
1065 extra_endfig := extra_endfig & "_pcFitBBox ;" ;
1066
1067 %C merges bbox size of current picture with
1068 %C the size saved in variables pc_bULC/LRC to obtain
1069 %C ths size of a maximum bbox
1070 def _pcFitBBox =
1071 begingroup;
1072 save L, YO, YU, XR, XL ; string L ;
1073 numeric YO, YU, XR, XL ;
1074
1075
1076 if (xpart (ulcorner currentpicture)) < (xpart pc_bULC) :
1077 XL := xpart ( ulcorner currentpicture ) ;
1078 else:
1079 XL := xpart pc_bULC ;
1080 fi ;
1081 if (ypart (ulcorner currentpicture)) > (ypart pc_bULC) :
1082 YO := ypart ( ulcorner currentpicture ) ;
1083 else:
1084 YO := ypart pc_bULC ;
1085 fi ;
1086 if (xpart ( lrcorner currentpicture)) > (xpart pc_bLRC) :
1087 XR := xpart ( lrcorner currentpicture ) ;
1088 else:
1089 XR := xpart pc_bLRC ;
1090 fi ;
1091 if (ypart ( lrcorner currentpicture)) < (ypart pc_bLRC) :
1092 YU := ypart ( lrcorner currentpicture ) ;
1093 else:
1094 YU := ypart pc_bLRC ;
1095 fi ;
1096
1097 setbounds currentpicture to (XL,YO)--(XR,YO)--(XR,YU)--(XL,YU)--cycle ;
1098 write "pc_bULC := ( " &
1099 decimal (xpart (ulcorner currentpicture)) & " , " &
1100 decimal (ypart (ulcorner currentpicture)) & " )" to pc_BBoxFile ;
1101 write "pc_bLRC := ( " &
1102 decimal (xpart (lrcorner currentpicture)) & " , " &
1103 decimal (ypart (lrcorner currentpicture)) & " )" to pc_BBoxFile ;
1104 write EOF to pc_BBoxFile ;
1105
1106 pc_bULC := (XL, YO) ; pc_bLRC := (XR, YU) ;
1107 endgroup;
1108 enddef;