dabf61b05036861d84b6307ab4fec151a3833ef6
[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-item.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 };
33
34
35 struct barchart
36   {
37     struct chart_item chart_item;
38
39     /* The categories */
40     struct freq **cats;
41
42     /* The total number of categories (regardless of level) */
43     int n_nzcats;
44
45     /* The number of primary categories */
46     int n_pcats;
47
48     /* The largest count of all the categories */
49     double largest;
50
51     /* The label for the ordinate (vertical axis) */
52     char *ylabel;
53
54     /* The variables holding the categorical values */
55     const struct variable **var;
56     int n_vars;
57
58     int widths[2];
59
60     /* A hash table of struct category indexed by VAL */
61     struct hmap primaries;
62
63     /* A hash table of struct category indexed by VAL */
64     struct hmap secondaries;
65
66
67     /* A array of pointers to the members of the above hmap, 
68        sorted by VAL */
69     struct category **ss;
70   };
71
72
73 struct variable;
74 struct freq;
75
76 struct chart_item *barchart_create (const struct variable **, int n_vars,
77                                     const char *ylabel,
78                                     struct freq *const *, int n_cats);
79 \f
80 /* This boilerplate for barchart, a subclass of chart_item, was
81    autogenerated by mk-class-boilerplate. */
82
83 #include <assert.h>
84 #include "libpspp/cast.h"
85
86 extern const struct chart_item_class barchart_class;
87
88 /* Returns true if SUPER is a barchart, otherwise false. */
89 static inline bool
90 is_barchart (const struct chart_item *super)
91 {
92   return super->class == &barchart_class;
93 }
94
95 /* Returns SUPER converted to barchart.  SUPER must be a barchart, as
96    reported by is_barchart. */
97 static inline struct barchart *
98 to_barchart (const struct chart_item *super)
99 {
100   assert (is_barchart (super));
101   return UP_CAST (super, struct barchart, chart_item);
102 }
103
104 /* Returns INSTANCE converted to chart_item. */
105 static inline struct chart_item *
106 barchart_super (const struct barchart *instance)
107 {
108   return CONST_CAST (struct chart_item *, &instance->chart_item);
109 }
110
111 /* Increments INSTANCE's reference count and returns INSTANCE. */
112 static inline struct barchart *
113 barchart_ref (const struct barchart *instance)
114 {
115   return to_barchart (chart_item_ref (&instance->chart_item));
116 }
117
118 /* Decrements INSTANCE's reference count, then destroys INSTANCE if
119    the reference count is now zero. */
120 static inline void
121 barchart_unref (struct barchart *instance)
122 {
123   chart_item_unref (&instance->chart_item);
124 }
125
126 /* Returns true if INSTANCE's reference count is greater than 1,
127    false otherwise. */
128 static inline bool
129 barchart_is_shared (const struct barchart *instance)
130 {
131   return chart_item_is_shared (&instance->chart_item);
132 }
133
134 static inline void
135 barchart_submit (struct barchart *instance)
136 {
137   chart_item_submit (&instance->chart_item);
138 }
139 \f
140 #endif /* output/charts/barchart.h */