3917e4e14b4833b04c6519a724ce5fe728095410
[pspp-builds.git] / src / output / charts / piechart-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 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 #include <config.h>
18
19 #include <output/charts/piechart.h>
20
21 #include <math.h>
22
23 #include <output/cairo-chart.h>
24
25 #include "gl/minmax.h"
26
27 /* Draw a single slice of the pie */
28 static void
29 draw_segment(cairo_t *cr,
30              double x0, double y0,
31              double radius,
32              double start_angle, double segment_angle,
33              const struct xrchart_colour *colour)
34 {
35   cairo_move_to (cr, x0, y0);
36   cairo_arc (cr, x0, y0, radius, start_angle, start_angle + segment_angle);
37   cairo_line_to (cr, x0, y0);
38   cairo_save (cr);
39   cairo_set_source_rgb (cr,
40                         colour->red / 255.0,
41                         colour->green / 255.0,
42                         colour->blue / 255.0);
43   cairo_fill_preserve (cr);
44   cairo_restore (cr);
45   cairo_stroke (cr);
46 }
47
48 void
49 xrchart_draw_piechart (const struct chart_item *chart_item, cairo_t *cr,
50                        struct xrchart_geometry *geom)
51 {
52   const struct piechart *pie = to_piechart (chart_item);
53   double total_magnitude;
54   double left_label, right_label;
55   double centre_x, centre_y;
56   double radius;
57   double angle;
58   int i;
59
60   centre_x = (geom->data_right + geom->data_left) / 2.0 ;
61   centre_y = (geom->data_top + geom->data_bottom) / 2.0 ;
62
63   left_label = geom->data_left + (geom->data_right - geom->data_left)/10.0;
64   right_label = geom->data_right - (geom->data_right - geom->data_left)/10.0;
65
66   radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
67                 1.0 / 4.0 * (geom->data_right - geom->data_left));
68
69   radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
70                 1.0 / 4.0 * (geom->data_right - geom->data_left));
71
72   xrchart_write_title (cr, geom, "%s", chart_item_get_title (chart_item));
73
74   total_magnitude = 0.0;
75   for (i = 0; i < pie->n_slices; i++)
76     total_magnitude += pie->slices[i].magnitude;
77
78   angle = 0.0;
79   for (i = 0; i < pie->n_slices ; ++i )
80     {
81       const double segment_angle =
82         pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
83
84       const double label_x = centre_x +
85         radius * cos (angle + segment_angle/2.0);
86
87       const double label_y = centre_y +
88         radius * sin (angle + segment_angle/2.0);
89
90       /* Fill the segment */
91       draw_segment (cr,
92                     centre_x, centre_y, radius,
93                     angle, segment_angle,
94                     &data_colour[i % XRCHART_N_COLOURS]);
95
96       /* Now add the labels */
97       if ( label_x < centre_x )
98         {
99           cairo_move_to (cr, label_x, label_y);
100           cairo_line_to (cr, left_label, label_y);
101           cairo_stroke (cr);
102           cairo_move_to (cr, left_label, label_y + 5);
103           xrchart_label (cr, 'l', 'x', geom->font_size,
104                          ds_cstr (&pie->slices[i].label));
105         }
106       else
107         {
108           cairo_move_to (cr, label_x, label_y);
109           cairo_line_to (cr, right_label, label_y);
110           cairo_stroke (cr);
111           cairo_move_to (cr, right_label, label_y + 5);
112           xrchart_label (cr, 'r', 'x', geom->font_size,
113                          ds_cstr (&pie->slices[i].label));
114         }
115
116       angle += segment_angle;
117     }
118
119   /* Draw an outline to the pie */
120   cairo_arc (cr, centre_x, centre_y, radius, 0, 2 * M_PI);
121   cairo_stroke (cr);
122 }
123