output-item: Collapse the inheritance hierarchy into a single struct.
[pspp] / src / output / charts / barchart.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2015 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 #ifndef BARCHART_H
18 #define BARCHART_H
19
20 #include "libpspp/str.h"
21 #include "libpspp/hmap.h"
22 #include "data/value.h"
23 #include "output/chart.h"
24
25
26 struct category
27 {
28   struct hmap_node node;
29   int idx;                     /* Unique zero based index */
30   struct string label;         /* The label to be displayed for this category */
31   union value val;             /* The value of this category */
32   int width;                   /* The width of VAL */
33 };
34
35
36 struct barchart
37   {
38     struct chart chart;
39
40     /* Should the chart be displayed as percentages */
41     bool percent;
42
43     /* The categories */
44     struct freq **cats;
45
46     /* The total number of categories (regardless of level) */
47     int n_nzcats;
48
49     /* The number of primary categories */
50     int n_pcats;
51
52     /* The largest count of all the categories */
53     double largest;
54
55     /* The sum of all the counts */
56     double total_count;
57
58     /* The label for the ordinate (vertical axis) */
59     char *ylabel;
60
61     /* The variables holding the categorical values */
62     const struct variable **var;
63     int n_vars;
64
65     int widths[2];
66
67     /* A hash table of struct category indexed by VAL */
68     struct hmap primaries;
69
70     /* A hash table of struct category indexed by VAL */
71     struct hmap secondaries;
72
73     /* A array of pointers to the members of the above hmap,
74        (the secondaries) sorted by VAL */
75     struct category **ss;
76   };
77
78
79 struct variable;
80 struct freq;
81
82 struct chart *barchart_create (const struct variable **, int n_vars,
83                                     const char *ylabel, bool percent,
84                                     struct freq *const *, int n_cats);
85 \f
86 /* This boilerplate for barchart, a subclass of chart, was
87    autogenerated by mk-class-boilerplate. */
88
89 #include <assert.h>
90 #include "libpspp/cast.h"
91
92 extern const struct chart_class barchart_class;
93
94 /* Returns true if SUPER is a barchart, otherwise false. */
95 static inline bool
96 is_barchart (const struct chart *super)
97 {
98   return super->class == &barchart_class;
99 }
100
101 /* Returns SUPER converted to barchart.  SUPER must be a barchart, as
102    reported by is_barchart. */
103 static inline struct barchart *
104 to_barchart (const struct chart *super)
105 {
106   assert (is_barchart (super));
107   return UP_CAST (super, struct barchart, chart);
108 }
109
110 /* Returns INSTANCE converted to chart. */
111 static inline struct chart *
112 barchart_super (const struct barchart *instance)
113 {
114   return CONST_CAST (struct chart *, &instance->chart);
115 }
116
117 /* Increments INSTANCE's reference count and returns INSTANCE. */
118 static inline struct barchart *
119 barchart_ref (const struct barchart *instance)
120 {
121   return to_barchart (chart_ref (&instance->chart));
122 }
123
124 /* Decrements INSTANCE's reference count, then destroys INSTANCE if
125    the reference count is now zero. */
126 static inline void
127 barchart_unref (struct barchart *instance)
128 {
129   chart_unref (&instance->chart);
130 }
131
132 /* Returns true if INSTANCE's reference count is greater than 1,
133    false otherwise. */
134 static inline bool
135 barchart_is_shared (const struct barchart *instance)
136 {
137   return chart_is_shared (&instance->chart);
138 }
139
140 static inline void
141 barchart_submit (struct barchart *instance)
142 {
143   chart_submit (&instance->chart);
144 }
145 \f
146 #endif /* output/charts/barchart.h */