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-provider.h"
29 #include "gl/xalloc.h"
30 #include "data/variable.h"
31 #include "data/settings.h"
32 #include "language/commands/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)
184 int width = var_get_width (var[pidx]);
186 assert (n_vars >= 1 && n_vars <= 2);
188 struct barchart *bar = XZALLOC (struct barchart);
189 bar->percent = percent;
191 bar->n_vars = n_vars;
192 bar->n_nzcats = n_cats;
193 chart_init (&bar->chart, &barchart_class, var_to_string (var[pidx]));
196 bar->ylabel = strdup (ylabel);
200 hmap_init (&bar->primaries);
203 Iterate the categories and create a hash table of the primary categories.
204 We need to do this to find out how many there are and to cache the labels.
206 for (i = 0; i < n_cats; i++)
208 const struct freq *src = cats[i];
209 size_t hash = value_hash (&src->values[pidx], width, 0);
211 struct category *foo;
213 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->primaries)
215 if (value_equal (&foo->val, &src->values[pidx], width))
224 struct category *s = XZALLOC (struct category);
226 s->width = var_get_width (var[pidx]);
227 value_init (&s->val, s->width);
228 value_copy (&s->val, &src->values[pidx], s->width);
229 ds_init_empty (&s->label);
230 var_append_value_name (var[pidx], &s->val, &s->label);
232 hmap_insert (&bar->primaries, &s->node, hash);
236 bar->n_pcats = hmap_count (&bar->primaries);
241 hmap_init (&bar->secondaries);
243 /* Iterate the categories, and create a hash table of secondary categories */
244 for (i = 0; i < n_cats; i++)
246 struct freq *src = cats[i];
248 struct category *foo;
250 size_t hash = value_hash (&src->values[sidx], var_get_width (var[sidx]), 0);
251 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->secondaries)
253 if (value_equal (&foo->val, &src->values[sidx], var_get_width (var[sidx])))
262 struct category *s = XZALLOC (struct category);
264 s->width = var_get_width (var[sidx]);
265 value_init (&s->val, s->width);
266 value_copy (&s->val, &src->values[sidx], var_get_width (var[sidx]));
267 ds_init_empty (&s->label);
268 var_append_value_name (var[sidx], &s->val, &s->label);
270 hmap_insert (&bar->secondaries, &s->node, hash);
271 bar->ss = xrealloc (bar->ss, idx * sizeof *bar->ss);
272 bar->ss[idx - 1] = s;
276 int n_category = hmap_count (&bar->secondaries);
278 sort (bar->ss, n_category, sizeof *bar->ss,
279 compare_category_3way, bar);
283 /* Deep copy. Not necessary for cmd line, but essential for the GUI,
284 since an expose callback will access these structs which may not
287 bar->cats = xcalloc (n_cats, sizeof *bar->cats);
289 bar->widths[0] = var_get_width (bar->var[0]);
291 bar->widths[1] = var_get_width (bar->var[1]);
294 struct hmap level2table;
295 hmap_init (&level2table);
298 for (i = 0; i < n_cats; i++)
300 struct freq *c = cats[i];
304 size_t hash = hash_freq_2level_ptr (&c, bar);
305 HMAP_FOR_EACH_WITH_HASH (foo, struct freq, node, hash, &level2table)
307 if (0 == compare_freq_2level_ptr_3way (&foo, &c, bar))
309 foo->count += c->count;
310 bar->total_count += c->count;
312 if (foo->count > bar->largest)
313 bar->largest = foo->count;
322 struct freq *aggregated_freq = freq_clone (c, n_vars, bar->widths);
323 hmap_insert (&level2table, &aggregated_freq->node, hash);
325 if (c->count > bar->largest)
326 bar->largest = aggregated_freq->count;
328 bar->total_count += c->count;
329 bar->cats[x++] = aggregated_freq;
333 bar->n_nzcats = hmap_count (&level2table);
334 hmap_destroy (&level2table);
337 sort (bar->cats, bar->n_nzcats, sizeof *bar->cats,
338 compare_freq_2level_ptr_3way, bar);
340 if (settings_get_testing_mode ())
341 barchart_dump (bar, stdout);
347 destroy_cat_map (struct hmap *m)
349 struct category *foo = NULL;
350 struct category *next = NULL;
351 HMAP_FOR_EACH_SAFE (foo, next, struct category, node, m)
353 value_destroy (&foo->val, foo->width);
355 ds_destroy (&foo->label);
363 barchart_destroy (struct chart *chart)
365 struct barchart *bar = to_barchart (chart);
369 destroy_cat_map (&bar->primaries);
372 destroy_cat_map (&bar->secondaries);
375 for (i = 0; i < bar->n_nzcats; i++)
377 freq_destroy (bar->cats[i], bar->n_vars, bar->widths);
386 const struct chart_class barchart_class =