e33fd086704b4d6e342cb283a9464af1d74ab487
[pspp] / src / output / charts / piechart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include <output/charts/piechart.h>
21
22 #include <assert.h>
23 #include <float.h>
24 #include <gsl/gsl_math.h>
25 #include <math.h>
26 #include <stdio.h>
27
28 #include <data/value-labels.h>
29 #include <libpspp/str.h>
30 #include <output/charts/plot-chart.h>
31 #include <output/chart-provider.h>
32
33 #include "minmax.h"
34
35 struct piechart
36   {
37     struct chart chart;
38     char *title;
39     struct slice *slices;
40     int n_slices;
41   };
42
43 static const struct chart_class piechart_class;
44
45 /* Draw a single slice of the pie */
46 static void
47 draw_segment(plPlotter *,
48              double centre_x, double centre_y,
49              double radius,
50              double start_angle, double segment_angle,
51              const struct chart_colour *) ;
52
53
54
55 /* Creates and returns a chart that will render a piechart with
56    the given TITLE and the N_SLICES described in SLICES. */
57 struct chart *
58 piechart_create (const char *title, const struct slice *slices, int n_slices)
59 {
60   struct piechart *pie;
61   int i;
62
63   pie = xmalloc (sizeof *pie);
64   chart_init (&pie->chart, &piechart_class);
65   pie->title = xstrdup (title);
66   pie->slices = xnmalloc (n_slices, sizeof *pie->slices);
67   for (i = 0; i < n_slices; i++)
68     {
69       const struct slice *src = &slices[i];
70       struct slice *dst = &pie->slices[i];
71
72       ds_init_string (&dst->label, &src->label);
73       dst->magnitude = src->magnitude;
74     }
75   pie->n_slices = n_slices;
76   return &pie->chart;
77 }
78
79 static void
80 piechart_draw (const struct chart *chart, plPlotter *lp,
81                struct chart_geometry *geom)
82 {
83   struct piechart *pie = (struct piechart *) chart;
84   double total_magnitude;
85   double left_label, right_label;
86   double centre_x, centre_y;
87   double radius;
88   double angle;
89   int i;
90
91   left_label = geom->data_left + (geom->data_right - geom->data_left)/10.0;
92   right_label = geom->data_right - (geom->data_right - geom->data_left)/10.0;
93
94   centre_x = (geom->data_right + geom->data_left) / 2.0 ;
95   centre_y = (geom->data_top + geom->data_bottom) / 2.0 ;
96
97   radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
98                 1.0 / 4.0 * (geom->data_right - geom->data_left));
99
100   chart_write_title (lp, geom, "%s", pie->title);
101
102   total_magnitude = 0.0;
103   for (i = 0; i < pie->n_slices; i++)
104     total_magnitude += pie->slices[i].magnitude;
105
106   angle = 0.0;
107   for (i = 0; i < pie->n_slices ; ++i )
108     {
109       const double segment_angle =
110         pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
111
112       const double label_x = centre_x -
113         radius * sin(angle + segment_angle/2.0);
114
115       const double label_y = centre_y +
116         radius * cos(angle + segment_angle/2.0);
117
118       /* Fill the segment */
119       draw_segment (lp,
120                     centre_x, centre_y, radius,
121                     angle, segment_angle,
122                     &data_colour[i % N_CHART_COLOURS]);
123
124       /* Now add the labels */
125       if ( label_x < centre_x )
126         {
127           pl_line_r (lp, label_x, label_y, left_label, label_y );
128           pl_moverel_r (lp, 0, 5);
129           pl_alabel_r (lp, 0, 0, ds_cstr (&pie->slices[i].label));
130         }
131       else
132         {
133           pl_line_r (lp, label_x, label_y, right_label, label_y);
134           pl_moverel_r (lp, 0, 5);
135           pl_alabel_r (lp, 'r', 0, ds_cstr (&pie->slices[i].label));
136         }
137
138       angle += segment_angle;
139     }
140
141   /* Draw an outline to the pie */
142   pl_filltype_r (lp,0);
143   pl_fcircle_r (lp, centre_x, centre_y, radius);
144 }
145
146 /* Fill a segment with the current fill colour */
147 static void
148 fill_segment(plPlotter *lp,
149              double x0, double y0,
150              double radius,
151              double start_angle, double segment_angle)
152 {
153
154   const double start_x  = x0 - radius * sin(start_angle);
155   const double start_y  = y0 + radius * cos(start_angle);
156
157   const double stop_x   =
158     x0 - radius * sin(start_angle + segment_angle);
159
160   const double stop_y   =
161     y0 + radius * cos(start_angle + segment_angle);
162
163   assert(segment_angle <= 2 * M_PI);
164   assert(segment_angle >= 0);
165
166   if ( segment_angle > M_PI )
167     {
168       /* Then we must draw it in two halves */
169       fill_segment(lp, x0, y0, radius, start_angle, segment_angle / 2.0 );
170       fill_segment(lp, x0, y0, radius, start_angle + segment_angle / 2.0,
171                    segment_angle / 2.0 );
172     }
173   else
174     {
175       pl_move_r(lp, x0, y0);
176
177       pl_cont_r(lp, stop_x, stop_y);
178       pl_cont_r(lp, start_x, start_y);
179
180       pl_arc_r(lp,
181                x0, y0,
182                stop_x, stop_y,
183                start_x, start_y
184                );
185
186       pl_endpath_r(lp);
187     }
188 }
189
190 /* Draw a single slice of the pie */
191 static void
192 draw_segment(plPlotter *lp,
193              double x0, double y0,
194              double radius,
195              double start_angle, double segment_angle,
196              const struct chart_colour *colour)
197 {
198   const double start_x  = x0 - radius * sin(start_angle);
199   const double start_y  = y0 + radius * cos(start_angle);
200
201   pl_savestate_r(lp);
202
203   pl_savestate_r(lp);
204   pl_color_r(lp, colour->red * 257, colour->green * 257, colour->blue * 257);
205
206   pl_pentype_r(lp,1);
207   pl_filltype_r(lp,1);
208
209   fill_segment(lp, x0, y0, radius, start_angle, segment_angle);
210   pl_restorestate_r(lp);
211
212   /* Draw line dividing segments */
213   pl_pentype_r(lp, 1);
214   pl_fline_r(lp, x0, y0, start_x, start_y);
215
216
217   pl_restorestate_r(lp);
218 }
219
220 static void
221 piechart_destroy (struct chart *chart)
222 {
223   struct piechart *pie = (struct piechart *) chart;
224   int i;
225
226   free (pie->title);
227   for (i = 0; i < pie->n_slices; i++)
228     {
229       struct slice *slice = &pie->slices[i];
230       ds_destroy (&slice->label);
231     }
232   free (pie->slices);
233   free (pie);
234 }
235
236 static const struct chart_class piechart_class =
237   {
238     piechart_draw,
239     piechart_destroy
240   };