1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2015 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/charts/barchart.h"
20 #include "output/charts/piechart.h"
24 #include "libpspp/cast.h"
25 #include "libpspp/str.h"
26 #include "libpspp/array.h"
27 #include "output/chart-item-provider.h"
29 #include "gl/xalloc.h"
30 #include "data/variable.h"
31 #include "data/settings.h"
32 #include "language/stats/freq.h"
36 compare_category_3way (const void *a_, const void *b_, const void *bc_)
38 const struct category *const*a = a_;
39 const struct category *const*b = b_;
40 const struct barchart *bc = bc_;
42 return value_compare_3way (&(*a)->val, &(*b)->val, var_get_width (bc->var[1]));
47 hash_freq_2level_ptr (const void *a_, const void *bc_)
49 const struct freq *const *ap = a_;
50 const struct barchart *bc = bc_;
52 size_t hash = value_hash (&(*ap)->values[0], bc->widths[0], 0);
55 hash = value_hash (&(*ap)->values[1], bc->widths[1], hash);
62 compare_freq_2level_ptr_3way (const void *a_, const void *b_, const void *bc_)
64 const struct freq *const *ap = a_;
65 const struct freq *const *bp = b_;
66 const struct barchart *bc = bc_;
68 const int level0 = value_compare_3way (&(*ap)->values[0], &(*bp)->values[0], bc->widths[0]);
70 if (level0 == 0 && bc->n_vars > 1)
71 return value_compare_3way (&(*ap)->values[1], &(*bp)->values[1], bc->widths[1]);
76 /* Print out a textual representation of a barchart.
77 This is intended only for testing, and not as a means
78 of visualising the data.
81 barchart_dump (const struct barchart *bc, FILE *fp)
83 fprintf (fp, "Graphic: Barchart\n");
84 fprintf (fp, "Percentage: %d\n", bc->percent);
85 fprintf (fp, "Total Categories: %d\n", bc->n_nzcats);
86 fprintf (fp, "Primary Categories: %d\n", bc->n_pcats);
87 fprintf (fp, "Largest Category: %g\n", bc->largest);
88 fprintf (fp, "Total Count: %g\n", bc->total_count);
90 fprintf (fp, "Y Label: \"%s\"\n", bc->ylabel);
92 fprintf (fp, "Categorical Variables:\n");
93 for (int i = 0; i < bc->n_vars; ++i)
95 fprintf (fp, " Var: \"%s\"\n", var_get_name (bc->var[i]));
98 fprintf (fp, "Categories:\n");
100 HMAP_FOR_EACH (cat, struct category, node, &bc->primaries)
102 fprintf (fp, " %d \"%s\"\n", cat->idx, ds_cstr(&cat->label));
107 fprintf (fp, "Sub-categories:\n");
108 for (int i = 0; i < bc->n_nzcats / bc->n_pcats; ++i)
110 const struct category *cat = bc->ss[i];
111 fprintf (fp, " %d \"%s\"\n", cat->idx, ds_cstr(&cat->label));
115 fprintf (fp, "All Categories:\n");
116 for (int i = 0; i < bc->n_nzcats; ++i)
118 const struct freq *frq = bc->cats[i];
119 fprintf (fp, "Count: %g; ", frq->count);
121 struct string s = DS_EMPTY_INITIALIZER;
122 var_append_value_name (bc->var[0], &frq->values[0], &s);
124 fprintf (fp, "Cat: \"%s\"", ds_cstr (&s));
129 var_append_value_name (bc->var[1], &frq->values[1], &s);
130 fprintf (fp, ", \"%s\"", ds_cstr (&s));
140 /* Creates and returns a chart that will render a barchart with
141 the given TITLE and the N_CATS described in CATS.
143 VAR is an array containing the categorical variables, and N_VAR
144 the number of them. N_VAR must be exactly 1 or 2.
146 CATS are the counts of the values of those variables. N_CATS is the
147 number of distinct values.
150 barchart_create (const struct variable **var, int n_vars,
151 const char *ylabel, bool percent,
152 struct freq *const *cats, int n_cats)
154 struct barchart *bar;
161 int width = var_get_width (var[pidx]);
163 assert (n_vars >= 1);
165 bar = xzalloc (sizeof *bar);
166 bar->percent = percent;
168 bar->n_vars = n_vars;
169 bar->n_nzcats = n_cats;
170 chart_item_init (&bar->chart_item, &barchart_class, var_to_string (var[pidx]));
173 bar->ylabel = strdup (ylabel);
177 hmap_init (&bar->primaries);
180 Iterate the categories and create a hash table of the primary categories.
181 We need to do this to find out how many there are and to cache the labels.
183 for (i = 0; i < n_cats; i++)
185 const struct freq *src = cats[i];
186 size_t hash = value_hash (&src->values[pidx], width, 0);
188 struct category *foo;
190 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->primaries)
192 if (value_equal (&foo->val, &src->values[pidx], width))
201 struct category *s = xzalloc (sizeof *s);
203 s->width = var_get_width (var[pidx]);
204 value_init (&s->val, s->width);
205 value_copy (&s->val, &src->values[pidx], s->width);
206 ds_init_empty (&s->label);
207 var_append_value_name (var[pidx], &s->val, &s->label);
209 hmap_insert (&bar->primaries, &s->node, hash);
213 bar->n_pcats = hmap_count (&bar->primaries);
218 hmap_init (&bar->secondaries);
220 /* Iterate the categories, and create a hash table of secondary categories */
221 for (i = 0; i < n_cats; i++)
223 struct freq *src = cats[i];
225 struct category *foo;
227 size_t hash = value_hash (&src->values[sidx], var_get_width (var[sidx]), 0);
228 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->secondaries)
230 if (value_equal (&foo->val, &src->values[sidx], var_get_width (var[sidx])))
239 struct category *s = xzalloc (sizeof *s);
241 s->width = var_get_width (var[sidx]);
242 value_init (&s->val, s->width);
243 value_copy (&s->val, &src->values[sidx], var_get_width (var[sidx]));
244 ds_init_empty (&s->label);
245 var_append_value_name (var[sidx], &s->val, &s->label);
247 hmap_insert (&bar->secondaries, &s->node, hash);
248 bar->ss = xrealloc (bar->ss, idx * sizeof *bar->ss);
249 bar->ss[idx - 1] = s;
253 int n_category = hmap_count (&bar->secondaries);
255 sort (bar->ss, n_category, sizeof *bar->ss,
256 compare_category_3way, bar);
260 /* Deep copy. Not necessary for cmd line, but essential for the GUI,
261 since an expose callback will access these structs which may not
264 bar->cats = xcalloc (n_cats, sizeof *bar->cats);
266 bar->widths[0] = var_get_width (bar->var[0]);
268 bar->widths[1] = var_get_width (bar->var[1]);
271 struct hmap level2table;
272 hmap_init (&level2table);
275 for (i = 0; i < n_cats; i++)
277 struct freq *c = cats[i];
281 size_t hash = hash_freq_2level_ptr (&c, bar);
282 HMAP_FOR_EACH_WITH_HASH (foo, struct freq, node, hash, &level2table)
284 if (0 == compare_freq_2level_ptr_3way (&foo, &c, bar))
286 foo->count += c->count;
287 bar->total_count += c->count;
289 if (foo->count > bar->largest)
290 bar->largest = foo->count;
299 struct freq *aggregated_freq = freq_clone (c, n_vars, bar->widths);
300 hmap_insert (&level2table, &aggregated_freq->node, hash);
302 if (c->count > bar->largest)
303 bar->largest = aggregated_freq->count;
305 bar->total_count += c->count;
306 bar->cats[x++] = aggregated_freq;
310 bar->n_nzcats = hmap_count (&level2table);
311 hmap_destroy (&level2table);
314 sort (bar->cats, bar->n_nzcats, sizeof *bar->cats,
315 compare_freq_2level_ptr_3way, bar);
317 if (settings_get_testing_mode ())
318 barchart_dump (bar, stdout);
320 return &bar->chart_item;
324 destroy_cat_map (struct hmap *m)
326 struct category *foo = NULL;
327 struct category *next = NULL;
328 HMAP_FOR_EACH_SAFE (foo, next, struct category, node, m)
330 value_destroy (&foo->val, foo->width);
332 ds_destroy (&foo->label);
340 barchart_destroy (struct chart_item *chart_item)
342 struct barchart *bar = to_barchart (chart_item);
346 destroy_cat_map (&bar->primaries);
349 destroy_cat_map (&bar->secondaries);
352 for (i = 0; i < bar->n_nzcats; i++)
354 freq_destroy (bar->cats[i], bar->n_vars, bar->widths);
363 const struct chart_item_class barchart_class =