Fixed more gcc 2.95 problems
[pspp-builds.git] / src / piechart.c
1 /* PSPP - draws pie charts of sample statistics
2
3 Copyright (C) 2004 Free Software Foundation, Inc.
4 Written by John Darrington <john@darrington.wattle.id.au>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21
22 #include "config.h"
23 #include "chart.h"
24 #include <float.h>
25 #include <assert.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include "str.h"
29 #include "value-labels.h"
30 #include "misc.h"
31
32
33 /* Pie charts of course need to know Pi :) */
34 #ifndef M_PI
35 #define M_PI ( 22.0 / 7.0 ) 
36 #endif
37
38
39
40 /* Draw a single slice of the pie */
41 static void
42 draw_segment(struct chart *ch, 
43              double centre_x, double centre_y, 
44              double radius,
45              double start_angle, double segment_angle,
46              const char *colour) ;
47
48
49
50 /* Draw a piechart */
51 void
52 piechart_plot(const char *title, const struct slice *slices, int n_slices)
53 {
54   int i;
55   double total_magnetude=0;
56
57   struct chart *ch = chart_create();
58
59   const double left_label = ch->data_left + 
60     (ch->data_right - ch->data_left)/10.0;
61
62   const double right_label = ch->data_right - 
63     (ch->data_right - ch->data_left)/10.0;
64
65   const double centre_x = (ch->data_right + ch->data_left ) / 2.0 ;
66   const double centre_y = (ch->data_top + ch->data_bottom ) / 2.0 ;
67
68   const double radius = min( 
69                             5.0 / 12.0 * (ch->data_top - ch->data_bottom),
70                             1.0 / 4.0 * (ch->data_right - ch->data_left)
71                             );
72
73
74   chart_write_title(ch, title);
75
76   for (i = 0 ; i < n_slices ; ++i ) 
77     total_magnetude += slices[i].magnetude;
78
79   for (i = 0 ; i < n_slices ; ++i ) 
80     {
81       static double angle=0.0;
82
83       const double segment_angle = 
84         slices[i].magnetude / total_magnetude * 2 * M_PI ;
85
86       const double label_x = centre_x - 
87         radius * sin(angle + segment_angle/2.0);
88
89       const double label_y = centre_y + 
90         radius * cos(angle + segment_angle/2.0);
91
92       /* Fill the segment */
93       draw_segment(ch,
94                    centre_x, centre_y, radius, 
95                    angle, segment_angle,
96                    data_colour[i]);
97         
98       /* Now add the labels */
99       if ( label_x < centre_x ) 
100         {
101           pl_line_r(ch->lp, label_x, label_y,
102                     left_label, label_y );
103           pl_moverel_r(ch->lp,0,5);
104           pl_alabel_r(ch->lp,0,0,slices[i].label);
105         }
106       else
107         {
108           pl_line_r(ch->lp, 
109                     label_x, label_y,
110                     right_label, label_y
111                     );
112           pl_moverel_r(ch->lp,0,5);
113           pl_alabel_r(ch->lp,'r',0,slices[i].label);
114         }
115
116       angle += segment_angle;
117
118     }
119
120   /* Draw an outline to the pie */
121   pl_filltype_r(ch->lp,0);
122   pl_fcircle_r (ch->lp, centre_x, centre_y, radius);
123
124   chart_submit(ch);
125 }
126
127 static void
128 fill_segment(struct chart *ch, 
129              double x0, double y0, 
130              double radius,
131              double start_angle, double segment_angle) ;
132
133
134 /* Fill a segment with the current fill colour */
135 static void
136 fill_segment(struct chart *ch, 
137              double x0, double y0, 
138              double radius,
139              double start_angle, double segment_angle)
140 {
141
142   const double start_x  = x0 - radius * sin(start_angle);
143   const double start_y  = y0 + radius * cos(start_angle);
144
145   const double stop_x   = 
146     x0 - radius * sin(start_angle + segment_angle); 
147
148   const double stop_y   = 
149     y0 + radius * cos(start_angle + segment_angle);
150
151   assert(segment_angle <= 2 * M_PI);
152   assert(segment_angle >= 0);
153
154   if ( segment_angle > M_PI ) 
155     {
156       /* Then we must draw it in two halves */
157       fill_segment(ch, x0, y0, radius, start_angle, segment_angle / 2.0 );
158       fill_segment(ch, x0, y0, radius, start_angle + segment_angle / 2.0,
159                    segment_angle / 2.0 );
160     }
161   else
162     {
163       pl_move_r(ch->lp, x0, y0);
164
165       pl_cont_r(ch->lp, stop_x, stop_y);
166       pl_cont_r(ch->lp, start_x, start_y);
167
168       pl_arc_r(ch->lp,
169                x0, y0,
170                stop_x, stop_y,
171                start_x, start_y
172                );
173
174       pl_endpath_r(ch->lp);
175     }
176 }
177
178
179
180 /* Draw a single slice of the pie */
181 static void
182 draw_segment(struct chart *ch, 
183              double x0, double y0, 
184              double radius,
185              double start_angle, double segment_angle, 
186              const char *colour)
187 {
188   const double start_x  = x0 - radius * sin(start_angle);
189   const double start_y  = y0 + radius * cos(start_angle);
190
191   pl_savestate_r(ch->lp);
192
193   pl_savestate_r(ch->lp);
194   pl_colorname_r(ch->lp, colour);
195   
196   pl_pentype_r(ch->lp,1);
197   pl_filltype_r(ch->lp,1);
198
199   fill_segment(ch, x0, y0, radius, start_angle, segment_angle);
200   pl_restorestate_r(ch->lp);
201
202   /* Draw line dividing segments */
203   pl_pentype_r(ch->lp, 1);
204   pl_fline_r(ch->lp, x0, y0, start_x, start_y);
205         
206
207   pl_restorestate_r(ch->lp);
208 }
209