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 compare_category_by_index_3way (const void *a_, const void *b_,
48 const void *unused UNUSED)
50 const struct category *const*a = a_;
51 const struct category *const*b = b_;
53 if ( (*a)->idx < (*b)->idx)
56 return ((*a)->idx > (*b)->idx);
60 hash_freq_2level_ptr (const void *a_, const void *bc_)
62 const struct freq *const *ap = a_;
63 const struct barchart *bc = bc_;
65 size_t hash = value_hash (&(*ap)->values[0], bc->widths[0], 0);
68 hash = value_hash (&(*ap)->values[1], bc->widths[1], hash);
75 compare_freq_2level_ptr_3way (const void *a_, const void *b_, const void *bc_)
77 const struct freq *const *ap = a_;
78 const struct freq *const *bp = b_;
79 const struct barchart *bc = bc_;
81 const int level0 = value_compare_3way (&(*ap)->values[0], &(*bp)->values[0], bc->widths[0]);
83 if (level0 == 0 && bc->n_vars > 1)
84 return value_compare_3way (&(*ap)->values[1], &(*bp)->values[1], bc->widths[1]);
89 /* Print out a textual representation of a barchart.
90 This is intended only for testing, and not as a means
91 of visualising the data.
94 barchart_dump (const struct barchart *bc, FILE *fp)
96 fprintf (fp, "Graphic: Barchart\n");
97 fprintf (fp, "Percentage: %d\n", bc->percent);
98 fprintf (fp, "Total Categories: %d\n", bc->n_nzcats);
99 fprintf (fp, "Primary Categories: %d\n", bc->n_pcats);
100 fprintf (fp, "Largest Category: %g\n", bc->largest);
101 fprintf (fp, "Total Count: %g\n", bc->total_count);
103 fprintf (fp, "Y Label: \"%s\"\n", bc->ylabel);
105 fprintf (fp, "Categorical Variables:\n");
106 for (int i = 0; i < bc->n_vars; ++i)
108 fprintf (fp, " Var: \"%s\"\n", var_get_name (bc->var[i]));
111 fprintf (fp, "Categories:\n");
112 struct category *cat;
113 struct category **cats = XCALLOC (hmap_count (&bc->primaries), struct category *);
115 HMAP_FOR_EACH (cat, struct category, node, &bc->primaries)
119 /* HMAP_FOR_EACH is not guaranteed to iterate in any particular order. So
120 we must sort here before we output the results. */
121 sort (cats, i, sizeof (struct category *), compare_category_by_index_3way, bc);
122 for (i = 0; i < hmap_count (&bc->primaries); ++i)
124 const struct category *c = cats[i];
125 fprintf (fp, " %d \"%s\"\n", c->idx, ds_cstr (&c->label));
131 fprintf (fp, "Sub-categories:\n");
132 for (int i = 0; i < bc->n_nzcats / bc->n_pcats; ++i)
134 const struct category *cat = bc->ss[i];
135 fprintf (fp, " %d \"%s\"\n", cat->idx, ds_cstr(&cat->label));
139 fprintf (fp, "All Categories:\n");
140 for (int i = 0; i < bc->n_nzcats; ++i)
142 const struct freq *frq = bc->cats[i];
143 fprintf (fp, "Count: %g; ", frq->count);
145 struct string s = DS_EMPTY_INITIALIZER;
146 var_append_value_name (bc->var[0], &frq->values[0], &s);
148 fprintf (fp, "Cat: \"%s\"", ds_cstr (&s));
153 var_append_value_name (bc->var[1], &frq->values[1], &s);
154 fprintf (fp, ", \"%s\"", ds_cstr (&s));
164 /* Creates and returns a chart that will render a barchart with
165 the given TITLE and the N_CATS described in CATS.
167 VAR is an array containing the categorical variables, and N_VAR
168 the number of them. N_VAR must be exactly 1 or 2.
170 CATS are the counts of the values of those variables. N_CATS is the
171 number of distinct values.
174 barchart_create (const struct variable **var, int n_vars,
175 const char *ylabel, bool percent,
176 struct freq *const *cats, int n_cats)
178 struct barchart *bar;
185 int width = var_get_width (var[pidx]);
187 assert (n_vars >= 1 && n_vars <= 2);
189 bar = xzalloc (sizeof *bar);
190 bar->percent = percent;
192 bar->n_vars = n_vars;
193 bar->n_nzcats = n_cats;
194 chart_item_init (&bar->chart_item, &barchart_class, var_to_string (var[pidx]));
197 bar->ylabel = strdup (ylabel);
201 hmap_init (&bar->primaries);
204 Iterate the categories and create a hash table of the primary categories.
205 We need to do this to find out how many there are and to cache the labels.
207 for (i = 0; i < n_cats; i++)
209 const struct freq *src = cats[i];
210 size_t hash = value_hash (&src->values[pidx], width, 0);
212 struct category *foo;
214 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->primaries)
216 if (value_equal (&foo->val, &src->values[pidx], width))
225 struct category *s = xzalloc (sizeof *s);
227 s->width = var_get_width (var[pidx]);
228 value_init (&s->val, s->width);
229 value_copy (&s->val, &src->values[pidx], s->width);
230 ds_init_empty (&s->label);
231 var_append_value_name (var[pidx], &s->val, &s->label);
233 hmap_insert (&bar->primaries, &s->node, hash);
237 bar->n_pcats = hmap_count (&bar->primaries);
242 hmap_init (&bar->secondaries);
244 /* Iterate the categories, and create a hash table of secondary categories */
245 for (i = 0; i < n_cats; i++)
247 struct freq *src = cats[i];
249 struct category *foo;
251 size_t hash = value_hash (&src->values[sidx], var_get_width (var[sidx]), 0);
252 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->secondaries)
254 if (value_equal (&foo->val, &src->values[sidx], var_get_width (var[sidx])))
263 struct category *s = xzalloc (sizeof *s);
265 s->width = var_get_width (var[sidx]);
266 value_init (&s->val, s->width);
267 value_copy (&s->val, &src->values[sidx], var_get_width (var[sidx]));
268 ds_init_empty (&s->label);
269 var_append_value_name (var[sidx], &s->val, &s->label);
271 hmap_insert (&bar->secondaries, &s->node, hash);
272 bar->ss = xrealloc (bar->ss, idx * sizeof *bar->ss);
273 bar->ss[idx - 1] = s;
277 int n_category = hmap_count (&bar->secondaries);
279 sort (bar->ss, n_category, sizeof *bar->ss,
280 compare_category_3way, bar);
284 /* Deep copy. Not necessary for cmd line, but essential for the GUI,
285 since an expose callback will access these structs which may not
288 bar->cats = xcalloc (n_cats, sizeof *bar->cats);
290 bar->widths[0] = var_get_width (bar->var[0]);
292 bar->widths[1] = var_get_width (bar->var[1]);
295 struct hmap level2table;
296 hmap_init (&level2table);
299 for (i = 0; i < n_cats; i++)
301 struct freq *c = cats[i];
305 size_t hash = hash_freq_2level_ptr (&c, bar);
306 HMAP_FOR_EACH_WITH_HASH (foo, struct freq, node, hash, &level2table)
308 if (0 == compare_freq_2level_ptr_3way (&foo, &c, bar))
310 foo->count += c->count;
311 bar->total_count += c->count;
313 if (foo->count > bar->largest)
314 bar->largest = foo->count;
323 struct freq *aggregated_freq = freq_clone (c, n_vars, bar->widths);
324 hmap_insert (&level2table, &aggregated_freq->node, hash);
326 if (c->count > bar->largest)
327 bar->largest = aggregated_freq->count;
329 bar->total_count += c->count;
330 bar->cats[x++] = aggregated_freq;
334 bar->n_nzcats = hmap_count (&level2table);
335 hmap_destroy (&level2table);
338 sort (bar->cats, bar->n_nzcats, sizeof *bar->cats,
339 compare_freq_2level_ptr_3way, bar);
341 if (settings_get_testing_mode ())
342 barchart_dump (bar, stdout);
344 return &bar->chart_item;
348 destroy_cat_map (struct hmap *m)
350 struct category *foo = NULL;
351 struct category *next = NULL;
352 HMAP_FOR_EACH_SAFE (foo, next, struct category, node, m)
354 value_destroy (&foo->val, foo->width);
356 ds_destroy (&foo->label);
364 barchart_destroy (struct chart_item *chart_item)
366 struct barchart *bar = to_barchart (chart_item);
370 destroy_cat_map (&bar->primaries);
373 destroy_cat_map (&bar->secondaries);
376 for (i = 0; i < bar->n_nzcats; i++)
378 freq_destroy (bar->cats[i], bar->n_vars, bar->widths);
387 const struct chart_item_class barchart_class =