Debian Codegraph @

texmf-dist/doc/metapost/piechartmp/examples/wec.mp

raw
1 %H
2 %H piechartMP example - world energy consumption 2000
3 %H Jens-Uwe Morawski, 2002
4 %H
5
6
7 %I The piechartMP documentation includes many useful
8 %I examples which not require MetaPost experience.
9 %I In contrast this example employs many MetaPost
10 %I commands and definitions. Thus it is not meant
11 %I for piechart "beginners".
12 %I The chief purpose of this example is to show
13 %I how you can draw pie-charts with different data-sets
14 %I using ResetSegments and how to save them in
15 %I MetaPost pictures.
16 %I The file wec.pdf in the same directory is the
17 %I result of this MP-file.
18
19 input piechartmp ;
20
21 %D Helvetica-Bold
22 % This should work on most systems. If not, comment the next line.
23 defaultfont := "phvb8r" ;
24
25 %D
26 pair NameShift ; NameShift := (-3.5cm,0) ;
27 Scale := 8mm ;
28
29 %D define some colors
30 color oil_color, gas_color, coal_color, nuclear_color, water_color ;
31 oil_color := (0.4, 0 , 0 ) ;
32 gas_color := (0 , 0.5, 0 ) ;
33 coal_color := (0.5, 0.3, 0 ) ;
34 nuclear_color := (0.8, 0.8, 0.2) ;
35 water_color := (0.3, 0.3, 1.0) ;
36
37
38 ChartCounter := 0 ;
39 picture Charts[] ;
40
41 %D
42 def AChart (expr Size, Name, Water, Nuclear, Coal, Gas, Oil) =
43 begingroup;
44 save s , Radius ; string s ;
45
46 % the 'Size' is the absolute energy consumption
47 % that means the 'Size' is proportional to the area
48 % of the pie-chart and not the radius.
49 Radius := (sqrt Size)*Scale ;
50
51
52 s := Name & "; " & (decimal Size) & " billion t MCU" ;
53
54 ResetSegments ; % not documented in piechartMP doc
55
56 if Water>0:
57 Segment(Water, "", water_color) ;
58 fi;
59 if Nuclear>0:
60 Segment(Nuclear, "", nuclear_color) ;
61 fi;
62 if Coal>0:
63 Segment(Coal, "", coal_color) ;
64 fi;
65 if Gas>0:
66 Segment(Gas, "", gas_color) ;
67 fi;
68 if Oil>0:
69 Segment(Oil, "", oil_color) ;
70 fi;
71
72 PieChart(Radius, 0.3, 65, 90, 0) ;
73 label.lft (s, NameShift) ;
74 ChartCounter := ChartCounter + 1 ;
75 Charts[ChartCounter] := currentpicture ;
76 currentpicture := nullpicture ;
77 endgroup;
78 enddef;
79
80 def ShipoutCharts =
81 begingroup;
82 save h, t ; numeric h, t ;
83 h := 0 ; t := 0 ;
84 for i=1 upto ChartCounter:
85 t := ypart(ulcorner currentpicture) ;
86 h := -1*(ypart(lrcorner Charts[i])) ;
87 h := h + t + 5mm ;
88 draw Charts[i] shifted (0, (h));
89 endfor;
90 endgroup;
91 enddef;
92
93 beginfig(1);
94 AChart (3.8, "North-America", 2, 6, 25, 32, 35 ) ;
95 AChart (3.4, "Asia/Oceania", 2, 5, 46, 10, 38 ) ;
96 AChart (2.6, "Europe", 4, 16, 18, 20, 42 ) ;
97 AChart (1.3, "CIS", 2, 6, 18, 50, 24 ) ;
98 AChart (0.5, "Middle East", 0, 0, 0, 35, 65 ) ;
99 AChart (0.5, "South-America", 15, 0, 5, 15, 65 ) ;
100 AChart (0.4, "Africa", 0, 0, 37, 18, 45 ) ;
101 ShipoutCharts ;
102
103 x := 5mm+(xpart(lrcorner currentpicture)) ;
104 y := ypart(ulcorner currentpicture) ;
105
106 fill fullcircle scaled 5mm shifted (x,y) withcolor oil_color;
107 label.rt ("Oil", (x+5mm,y)) ;
108 fill fullcircle scaled 5mm shifted (x,(y-1cm)) withcolor gas_color;
109 label.rt ("Gas", (x+5mm,y-1cm)) ;
110 fill fullcircle scaled 5mm shifted (x,(y-2cm)) withcolor coal_color;
111 label.rt ("Coal", (x+5mm,y-2cm)) ;
112 fill fullcircle scaled 5mm shifted (x,(y-3cm)) withcolor nuclear_color;
113 label.rt ("Nuclear Power", (x+5mm,y-3cm)) ;
114 fill fullcircle scaled 5mm shifted (x,(y-4cm)) withcolor water_color;
115 label.rt ("Water Power", (x+5mm,y-4cm)) ;
116
117 %D
118 defaultfont:="ptmbi8r" ; defaultscale:= 2;
119 label.top("World Energy Consumption 2000", (0, y+5mm)) ;
120
121 endfig;
122 end