Change how checking for missing values works.
[pspp] / 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 "data/variable.h"
26 #include "output/chart-provider.h"
27
28 #include "gl/xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34
35 /* Creates and returns a chart that will render a piechart with
36    the of VAR and the N_SLICES described in SLICES. */
37 struct chart *
38 piechart_create (const struct variable *var, const struct freq *slices, int n_slices)
39 {
40   struct piechart *pie;
41   int i;
42
43   pie = xmalloc (sizeof *pie);
44   chart_init (&pie->chart, &piechart_class, var_to_string (var));
45   pie->slices = xnmalloc (n_slices, sizeof *pie->slices);
46   for (i = 0; i < n_slices; i++)
47     {
48       const struct freq *src = &slices[i];
49       struct slice *dst = &pie->slices[i];
50
51       ds_init_empty (&dst->label);
52
53       if (var_is_value_missing (var, &src->values[0]))
54         ds_assign_cstr (&dst->label, _("*MISSING*"));
55       else
56         var_append_value_name (var, &src->values[0], &dst->label);
57
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;
64     }
65   pie->n_slices = n_slices;
66   return &pie->chart;
67 }
68
69 static void
70 piechart_destroy (struct chart *chart)
71 {
72   struct piechart *pie = to_piechart (chart);
73   int i;
74
75   for (i = 0; i < pie->n_slices; i++)
76     {
77       struct slice *slice = &pie->slices[i];
78       ds_destroy (&slice->label);
79     }
80   free (pie->slices);
81   free (pie);
82 }
83
84 const struct chart_class piechart_class =
85   {
86     piechart_destroy
87   };