1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2009 Free Software Foundation, Inc.
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.
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.
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/>. */
20 #include <output/charts/piechart.h>
24 #include <gsl/gsl_math.h>
28 #include <data/value-labels.h>
29 #include <libpspp/str.h>
30 #include <output/charts/plot-chart.h>
31 #include <output/chart-provider.h>
43 static const struct chart_class piechart_class;
45 /* Draw a single slice of the pie */
47 draw_segment(cairo_t *,
48 double centre_x, double centre_y,
50 double start_angle, double segment_angle,
51 const struct chart_colour *) ;
55 /* Creates and returns a chart that will render a piechart with
56 the given TITLE and the N_SLICES described in SLICES. */
58 piechart_create (const char *title, const struct slice *slices, int n_slices)
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++)
69 const struct slice *src = &slices[i];
70 struct slice *dst = &pie->slices[i];
72 ds_init_string (&dst->label, &src->label);
73 dst->magnitude = src->magnitude;
75 pie->n_slices = n_slices;
80 piechart_draw (const struct chart *chart, cairo_t *cr,
81 struct chart_geometry *geom)
83 struct piechart *pie = (struct piechart *) chart;
84 double total_magnitude;
85 double left_label, right_label;
86 double centre_x, centre_y;
91 centre_x = (geom->data_right + geom->data_left) / 2.0 ;
92 centre_y = (geom->data_top + geom->data_bottom) / 2.0 ;
94 left_label = geom->data_left + (geom->data_right - geom->data_left)/10.0;
95 right_label = geom->data_right - (geom->data_right - geom->data_left)/10.0;
97 radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
98 1.0 / 4.0 * (geom->data_right - geom->data_left));
100 radius = MIN (5.0 / 12.0 * (geom->data_top - geom->data_bottom),
101 1.0 / 4.0 * (geom->data_right - geom->data_left));
103 chart_write_title (cr, geom, "%s", pie->title);
105 total_magnitude = 0.0;
106 for (i = 0; i < pie->n_slices; i++)
107 total_magnitude += pie->slices[i].magnitude;
110 for (i = 0; i < pie->n_slices ; ++i )
112 const double segment_angle =
113 pie->slices[i].magnitude / total_magnitude * 2 * M_PI ;
115 const double label_x = centre_x -
116 radius * sin(angle + segment_angle/2.0);
118 const double label_y = centre_y +
119 radius * cos(angle + segment_angle/2.0);
121 /* Fill the segment */
123 centre_x, centre_y, radius,
124 angle, segment_angle,
125 &data_colour[i % N_CHART_COLOURS]);
127 /* Now add the labels */
128 if ( label_x < centre_x )
130 cairo_move_to (cr, label_x, label_y);
131 cairo_line_to (cr, left_label, label_y);
133 cairo_move_to (cr, left_label, label_y + 5);
134 chart_label (cr, 'l', 'x', ds_cstr (&pie->slices[i].label));
138 cairo_move_to (cr, label_x, label_y);
139 cairo_line_to (cr, right_label, label_y);
141 cairo_move_to (cr, right_label, label_y + 5);
142 chart_label (cr, 'r', 'x', ds_cstr (&pie->slices[i].label));
145 angle += segment_angle;
148 /* Draw an outline to the pie */
149 cairo_arc (cr, centre_x, centre_y, radius, 0, 2 * M_PI);
153 /* Draw a single slice of the pie */
155 draw_segment(cairo_t *cr,
156 double x0, double y0,
158 double start_angle, double segment_angle,
159 const struct chart_colour *colour)
161 cairo_move_to (cr, x0, y0);
162 cairo_arc (cr, x0, y0, radius, start_angle, start_angle + segment_angle);
163 cairo_line_to (cr, x0, y0);
165 cairo_set_source_rgb (cr,
167 colour->green / 255.0,
168 colour->blue / 255.0);
169 cairo_fill_preserve (cr);
175 piechart_destroy (struct chart *chart)
177 struct piechart *pie = (struct piechart *) chart;
181 for (i = 0; i < pie->n_slices; i++)
183 struct slice *slice = &pie->slices[i];
184 ds_destroy (&slice->label);
190 static const struct chart_class piechart_class =