1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2011 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
draw_segment(cairo_t * cr,double x0,double y0,double radius,double start_angle,double segment_angle,const struct xrchart_colour * colour)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
xrchart_draw_piechart(const struct chart_item * chart_item,cairo_t * cr,struct xrchart_geometry * geom)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->axis[SCALE_ABSCISSA].data_max + geom->axis[SCALE_ORDINATE].data_min) / 2.0 ;
61 centre_y = (geom->axis[SCALE_ORDINATE].data_max + geom->axis[SCALE_ORDINATE].data_min) / 2.0 ;
62
63 left_label = geom->axis[SCALE_ORDINATE].data_min + (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min)/10.0;
64 right_label = geom->axis[SCALE_ABSCISSA].data_max - (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min)/10.0;
65
66 radius = MIN (5.0 / 12.0 * (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min),
67 1.0 / 4.0 * (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ORDINATE].data_min));
68
69 xrchart_write_title (cr, geom, "%s", chart_item_get_title (chart_item));
70
71 total_magnitude = 0.0;
72 for (i = 0; i < pie->n_slices; i++)
73 total_magnitude += pie->slices[i].magnitude;
74
75
76 /* Draw the segments */
77 angle = 0.0;
78 for (i = 0; i < pie->n_slices ; ++i)
79 {
80 const double segment_angle =
81 pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
82
83 /* Fill the segment */
84 draw_segment (cr,
85 centre_x, centre_y, radius,
86 angle, segment_angle,
87 &data_colour[i % XRCHART_N_COLOURS]);
88
89 angle += segment_angle;
90 }
91
92
93 /* Now add the labels.
94 Don't put this in the loop above; the labels must
95 be put in last, otherwise the segment fill could
96 obscure them.
97 */
98 angle = 0.0;
99 for (i = 0; i < pie->n_slices ; ++i)
100 {
101 const double segment_angle =
102 pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
103
104 const double label_x = centre_x +
105 radius * cos (angle + segment_angle/2.0);
106
107 const double label_y = centre_y +
108 radius * sin (angle + segment_angle/2.0);
109
110 if (label_x < centre_x)
111 {
112 cairo_move_to (cr, label_x, label_y);
113 cairo_line_to (cr, left_label, label_y);
114 cairo_stroke (cr);
115 cairo_move_to (cr, left_label, label_y + 5);
116 xrchart_label (cr, 'l', 'x', geom->font_size,
117 ds_cstr (&pie->slices[i].label));
118 }
119 else
120 {
121 cairo_move_to (cr, label_x, label_y);
122 cairo_line_to (cr, right_label, label_y);
123 cairo_stroke (cr);
124 cairo_move_to (cr, right_label, label_y + 5);
125 xrchart_label (cr, 'r', 'x', geom->font_size,
126 ds_cstr (&pie->slices[i].label));
127 }
128
129 angle += segment_angle;
130 }
131
132 /* Draw an outline to the pie */
133 cairo_arc (cr, centre_x, centre_y, radius, 0, 2 * M_PI);
134 cairo_stroke (cr);
135 }
136
137