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 "output/chart-item-provider.h"
27 #include "gl/xalloc.h"
29 /* Creates and returns a chart that will render a piechart with
30 the given TITLE and the N_SLICES described in SLICES. */
32 piechart_create (const char *title, const struct slice *slices, int n_slices)
37 pie = xmalloc (sizeof *pie);
38 chart_item_init (&pie->chart_item, &piechart_class, title);
39 pie->slices = xnmalloc (n_slices, sizeof *pie->slices);
40 for (i = 0; i < n_slices; i++)
42 const struct slice *src = &slices[i];
43 struct slice *dst = &pie->slices[i];
45 ds_init_string (&dst->label, &src->label);
46 dst->magnitude = src->magnitude;
48 pie->n_slices = n_slices;
49 return &pie->chart_item;
53 piechart_destroy (struct chart_item *chart_item)
55 struct piechart *pie = to_piechart (chart_item);
58 for (i = 0; i < pie->n_slices; i++)
60 struct slice *slice = &pie->slices[i];
61 ds_destroy (&slice->label);
67 const struct chart_item_class piechart_class =