Debian Codegraph @

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

raw
1 %H
2 %H piechartMP example - world energy consumption 2000
3 %H (extended MetaFun version)
4 %H Jens-Uwe Morawski, 2002
5 %H
6
7
8 %I this MetaPost example requires the MetaFun package
9 %I (see piechartMP documentation)
10 %I The purpose of this example is to show how pie-charts
11 %I can be placed in a figure using the pc_Centre variable.
12 %I wec-mfun.pdf in the same directory contains the final
13 %I figure.
14
15
16 %D This example uses the 'externalfigure' macro from
17 %D the MetaFun package. If piechartMP is loaded after
18 %D MetaFun then piechertMP detects MetaFun and will
19 %D draw segment outsides as gradients.
20 %D Since here many charts will be drawn it can cause
21 %D an overflow of the MetaPost buffer-size,
22 %D therefore piechertMP is loaded prior MetaFun.
23 %D If the buffer-size of your MetaPost installation is
24 %D large enough then you can load piechartMP after
25 %D MetaFun and segment outsides will be drawn as gradients.
26 input piechartmp ;
27 input metafun ;
28
29 %D Helvetica-Bold
30 % This should work on most systems. If not, comment the next line.
31 defaultfont := "phvb8r" ;
32 defaultscale := 0.7 ;
33
34 %D
35 Scale := 5mm ;
36
37 %D define some colors
38 color oil_color, gas_color, coal_color, nuclear_color, water_color ;
39 oil_color := (0.4, 0 , 0 ) ;
40 gas_color := (0 , 0.5, 0 ) ;
41 coal_color := (0.5, 0.3, 0 ) ;
42 nuclear_color := (0.8, 0.8, 0.2) ;
43 water_color := (0.3, 0.3, 1.0) ;
44
45
46 %D define some points to place the charts
47 pair place[] ;
48 place[1] := (2cm,5cm) ;% North America
49 place[2] := (7.5cm,3.5cm) ;% Asia
50 place[3] := (5cm,5.3cm) ;% Europe
51 place[4] := (6.5cm,5.8cm) ;% CIS
52 place[5] := (5.6cm,4.2cm) ;% Middle East
53 place[6] := (2.5cm,2.7cm) ;% South America
54 place[7] := (5cm,3cm) ;% Africa
55
56
57 %D
58 def AChart (expr Size, Name, Place, Water, Nuclear, Coal, Gas, Oil) =
59 begingroup;
60 save label_pnt, Radius ;
61 pair label_pnt ; numeric Radius ;
62
63 % the 'Size' is the absolute energy consumption
64 % that means the 'Size' is proportional to the area
65 % of the pie-chart and not the radius.
66 Radius := (sqrt Size)*Scale ;
67
68 ResetSegments ; % documented in piechartMP Internals
69
70 if Water>0:
71 Segment(Water, "", water_color) ;
72 fi;
73 if Nuclear>0:
74 Segment(Nuclear, "", nuclear_color) ;
75 fi;
76 if Coal>0:
77 Segment(Coal, "", coal_color) ;
78 fi;
79 if Gas>0:
80 Segment(Gas, "", gas_color) ;
81 fi;
82 if Oil>0:
83 Segment(Oil, "", oil_color) ;
84 fi;
85
86 pc_Centre := place[Place] ; % documented in piechartMP Internals
87 PieChart(Radius, 0.3, 65, 90, 0) ;
88 label_pnt := SegmentPoint(1,(1,0));
89 label.top (Name & "; " & (decimal Size), label_pnt) withcolor white;
90
91 endgroup;
92 enddef;
93
94
95 beginfig(1);
96
97 %D 'externalfigure' is a macro from the MetaFun package.
98 %D It embeds the external JPG-Background, the file worldmap.jpg
99 %D should be available from the same location as this
100 %D MP file.
101 externalfigure "worldmap.jpg" xscaled 10cm yscaled 7cm ;
102
103 AChart (3.8, "North-America", 1, 2, 6, 25, 32, 35 ) ;
104 AChart (3.4, "Asia/Oceania", 2, 2, 5, 46, 10, 38 ) ;
105 AChart (2.6, "Europe", 3, 4, 16, 18, 20, 42 ) ;
106 AChart (1.3, "CIS", 4, 2, 6, 18, 50, 24 ) ;
107 AChart (0.5, "Middle East", 5, 0, 0, 0, 35, 65 ) ;
108 AChart (0.5, "South-America",6 , 15, 0, 5, 15, 65 ) ;
109 AChart (0.4, "Africa",7 , 0, 0, 37, 18, 45 ) ;
110
111
112 %D the legend
113 x := 3mm ;
114 y := 2.5cm ;
115 fill fullcircle scaled 2mm shifted (x,y) withcolor oil_color;
116 label.rt ("Oil", (x+2mm,y)) ;
117 fill fullcircle scaled 2mm shifted (x,(y-5mm)) withcolor gas_color;
118 label.rt ("Gas", (x+2mm,y-5mm)) ;
119 fill fullcircle scaled 2mm shifted (x,(y-1cm)) withcolor coal_color;
120 label.rt ("Coal", (x+2mm,y-1cm)) ;
121 fill fullcircle scaled 2mm shifted (x,(y-1.5cm)) withcolor nuclear_color;
122 label.rt ("Nuclear Power", (x+2mm,y-1.5cm)) ;
123 fill fullcircle scaled 2mm shifted (x,(y-2cm)) withcolor water_color;
124 label.rt ("Water Power", (x+2mm,y-2cm)) ;
125
126
127 %D some labels
128 label.top ("(in billion t MCU)", (7cm,0)) ;
129 defaultfont:="ptmbi8r" ; defaultscale:= 1.2;
130 label("World Energy Consumption 2000", origin)
131 rotated 90 shifted (9.5cm,3cm) withcolor white;
132
133
134 %D Next follows the grid I have used to determine the co-ordinates
135 %D saved in place[]. Now it is not drawn because of the if..fi
136 %D statement.
137 if false:
138 for i=1 upto 10:
139 draw (origin--(0,7cm)) shifted (i*1cm,0) withcolor white ;
140 endfor;
141 for i=1 upto 7:
142 draw (origin--(10cm,0)) shifted (0,i*1cm) withcolor white ;
143 endfor;
144 fi;
145 endfig;
146 end