1 //      Pie chart demo.
2 //
3 
4 import std.string;
5 import std.stdio;
6 import std.math;
7 
8 import plplot;
9 
10 string[] text = [ "Maurice",
11                   "Geoffrey",
12                   "Alan",
13                   "Rafael",
14                   "Vince" ];
15 
16 //--------------------------------------------------------------------------
17 // main
18 //
19 // Does a simple pie chart.
20 //--------------------------------------------------------------------------
main(char[][]args)21 int main( char[][] args )
22 {
23     // Parse and process command line arguments
24     plparseopts( args, PL_PARSE_FULL );
25 
26     // Initialize plplot
27     plinit();
28 
29     pladv( 0 );
30 
31     // Ensure window has aspect ratio of one so circle is
32     // plotted as a circle.
33     plvasp( 1.0 );
34     plwind( 0.0, 10.0, 0.0, 10.0 );
35     plcol0( 2 );
36 
37     // n.b. all theta quantities scaled by 2*M_PI/500 to be integers to avoid
38     // floating point logic problems.
39     int     theta0 = 0;
40     int     dthet  = 1;
41     int     theta1, theta;
42     PLFLT   just, dx, dy;
43     PLFLT[] per = [ 10.0, 32.0, 12.0, 30.0, 16.0 ];
44     PLFLT[] x, y;
45     for ( size_t i = 0; i < 5; i++ )
46     {
47         size_t j = 0;
48 
49         // n.b. the theta quantities multiplied by 2*M_PI/500 afterward so
50         // in fact per is interpreted as a percentage.
51         theta1 = cast(int) ( theta0 + 5.0 * per[i] );
52         if ( i == 4 )
53             theta1 = 500;
54 
55         // set size of data arrays
56         x.length = y.length = ( theta1 - theta0 ) / dthet + 2;
57         x[j]     = 5.0;
58         y[j++]   = 5.0;
59 
60         for ( theta = theta0; theta <= theta1; theta += dthet )
61         {
62             x[j]   = 5.0 + 3.0 * cos( ( 2.0 * PI / 500. ) * theta );
63             y[j++] = 5.0 + 3.0 * sin( ( 2.0 * PI / 500. ) * theta );
64         }
65 
66         plcol0( cast(PLINT) ( i + 1 ) );
67         plpsty( ( i + 3 ) % 8 + 1 );
68         plfill( x, y );
69         plcol0( 1 );
70         plline( x, y );
71         just = ( 2.0 * PI / 500.0 ) * ( theta0 + theta1 ) / 2.0;
72         dx   = 0.25 * cos( just );
73         dy   = 0.25 * sin( just );
74         if ( ( theta0 + theta1 ) < 250 || ( theta0 + theta1 ) > 750 )
75             just = 0.0;
76         else
77             just = 1.0;
78 
79         plptex( x[j / 2] + dx, y[j / 2] + dy, 1.0, 0.0, just, text[i] );
80         theta0 = theta - dthet;
81     }
82     plfont( 2 );
83     plschr( 0., 1.3 );
84     plptex( 5.0, 9.0, 1.0, 0.0, 0.5, "Percentage of Sales" );
85 
86     // Don't forget to call PLEND to finish off!
87     plend();
88     return 0;
89 }
90