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