Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / output / charts / piechart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 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 <stdlib.h>
22
23 #include "libpspp/cast.h"
24 #include "libpspp/str.h"
25 #include "output/chart-item-provider.h"
26
27 #include "gl/xalloc.h"
28
29 /* Creates and returns a chart that will render a piechart with
30    the given TITLE and the N_SLICES described in SLICES. */
31 struct chart_item *
32 piechart_create (const char *title, const struct slice *slices, int n_slices)
33 {
34   struct piechart *pie;
35   int i;
36
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++)
41     {
42       const struct slice *src = &slices[i];
43       struct slice *dst = &pie->slices[i];
44
45       ds_init_string (&dst->label, &src->label);
46       dst->magnitude = src->magnitude;
47     }
48   pie->n_slices = n_slices;
49   return &pie->chart_item;
50 }
51
52 static void
53 piechart_destroy (struct chart_item *chart_item)
54 {
55   struct piechart *pie = to_piechart (chart_item);
56   int i;
57
58   for (i = 0; i < pie->n_slices; i++)
59     {
60       struct slice *slice = &pie->slices[i];
61       ds_destroy (&slice->label);
62     }
63   free (pie->slices);
64   free (pie);
65 }
66
67 const struct chart_item_class piechart_class =
68   {
69     piechart_destroy
70   };