1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2009, 2011 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/>. */
19 #include "output/charts/piechart.h"
23 #include "libpspp/cast.h"
24 #include "libpspp/str.h"
25 #include "data/variable.h"
26 #include "output/chart-item-provider.h"
28 #include "gl/xalloc.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
35 /* Creates and returns a chart that will render a piechart with
36 the of VAR and the N_SLICES described in SLICES. */
38 piechart_create (const struct variable *var, const struct freq *slices, int n_slices)
43 pie = xmalloc (sizeof *pie);
44 chart_item_init (&pie->chart_item, &piechart_class, var_to_string (var));
45 pie->slices = xnmalloc (n_slices, sizeof *pie->slices);
46 for (i = 0; i < n_slices; i++)
48 const struct freq *src = &slices[i];
49 struct slice *dst = &pie->slices[i];
51 ds_init_empty (&dst->label);
53 if ( var_is_value_missing (var, &src->values[0], MV_ANY))
54 ds_assign_cstr (&dst->label, _("*MISSING*"));
56 var_append_value_name (var, &src->values[0], &dst->label);
58 /* Chomp any whitespace from the RHS of the label.
59 Doing this ensures that those labels to the right
60 of the pie, appear right justified. */
61 ds_rtrim (&dst->label, ss_cstr (" \t"));
62 ds_ltrim (&dst->label, ss_cstr (" \t"));
63 dst->magnitude = src->count;
65 pie->n_slices = n_slices;
66 return &pie->chart_item;
70 piechart_destroy (struct chart_item *chart_item)
72 struct piechart *pie = to_piechart (chart_item);
75 for (i = 0; i < pie->n_slices; i++)
77 struct slice *slice = &pie->slices[i];
78 ds_destroy (&slice->label);
84 const struct chart_item_class piechart_class =