Piecharts: Remove trailing whitespace from slice labels
[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 "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
47       /* Chomp any whitespace from the RHS of the label.
48          Doing this ensures that those labels to the right
49          of the pie, appear right justified. */
50       ds_rtrim (&dst->label, ss_cstr (" \t"));
51       dst->magnitude = src->magnitude;
52     }
53   pie->n_slices = n_slices;
54   return &pie->chart_item;
55 }
56
57 static void
58 piechart_destroy (struct chart_item *chart_item)
59 {
60   struct piechart *pie = to_piechart (chart_item);
61   int i;
62
63   for (i = 0; i < pie->n_slices; i++)
64     {
65       struct slice *slice = &pie->slices[i];
66       ds_destroy (&slice->label);
67     }
68   free (pie->slices);
69   free (pie);
70 }
71
72 const struct chart_item_class piechart_class =
73   {
74     piechart_destroy
75   };