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