X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fcharts%2Fbarchart.c;h=1b946fddffb780a0cd636eb8b3ff15742f4153f3;hb=f20d313de2b85419c3e2e22a78cdcdce499af43d;hp=f3b10011cc959700a156d8b042f820aa9c07f246;hpb=bd17d2af982332ee1791998361b1ac6731fe14fa;p=pspp diff --git a/src/output/charts/barchart.c b/src/output/charts/barchart.c index f3b10011cc..1b946fddff 100644 --- a/src/output/charts/barchart.c +++ b/src/output/charts/barchart.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2004, 2009 Free Software Foundation, Inc. + Copyright (C) 2015 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -14,239 +14,376 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ - #include -#include -#include -#include -#include -#include -#include -#include - -#define CATAGORIES 6 -#define SUB_CATAGORIES 3 - -static const double x_min = 0; -static const double x_max = 15.0; +#include "output/charts/barchart.h" +#include "output/charts/piechart.h" -static const char *cat_labels[] = - { - "Age", - "Intelligence", - "Wealth", - "Emotional", - "cat 5", - "cat 6", - "cat 7", - "cat 8", - "cat 9", - "cat 10", - "cat 11" - }; +#include +#include "libpspp/cast.h" +#include "libpspp/str.h" +#include "libpspp/array.h" +#include "output/chart-provider.h" +#include "gl/xalloc.h" +#include "data/variable.h" +#include "data/settings.h" +#include "language/stats/freq.h" -/* Subcatagories */ -static const double data1[] = +static int +compare_category_3way (const void *a_, const void *b_, const void *bc_) { - 28,83, - 34, - 29,13, - 9,4, - 3,3, - 2,0, - 1,0, - 0, - 1,1 -}; - - -static const double data2[] = -{ - 45,13, - 9,4, - 3,43, - 2,0, - 1,20, - 0,0, - 1,1, - 0,0 -}; - -static const double data3[] = - { - 23,18, - 0, 45,23, 9, 40, 24,4, 8 - }; + const struct category *const*a = a_; + const struct category *const*b = b_; + const struct barchart *bc = bc_; + return value_compare_3way (&(*a)->val, &(*b)->val, var_get_width (bc->var[1])); +} -static const char subcat_name[]="Gender"; +static int +compare_category_by_index_3way (const void *a_, const void *b_, + const void *unused UNUSED) +{ + const struct category *const*a = a_; + const struct category *const*b = b_; -struct subcat { - const double *data; - const char *label; -}; + if ( (*a)->idx < (*b)->idx) + return -1; -static const struct subcat sub_catagory[SUB_CATAGORIES] = - { - {data1, "male"}, - {data2, "female"}, - {data3, "47xxy"} - }; + return ((*a)->idx > (*b)->idx); +} +static unsigned int +hash_freq_2level_ptr (const void *a_, const void *bc_) +{ + const struct freq *const *ap = a_; + const struct barchart *bc = bc_; + size_t hash = value_hash (&(*ap)->values[0], bc->widths[0], 0); -static const double y_min = 0; -static const double y_max = 120.0; -static const double y_tick = 20.0; + if (bc->n_vars > 1) + hash = value_hash (&(*ap)->values[1], bc->widths[1], hash); + return hash; +} -static void write_legend(struct chart *chart) ; +static int +compare_freq_2level_ptr_3way (const void *a_, const void *b_, const void *bc_) +{ + const struct freq *const *ap = a_; + const struct freq *const *bp = b_; + const struct barchart *bc = bc_; + const int level0 = value_compare_3way (&(*ap)->values[0], &(*bp)->values[0], bc->widths[0]); -void -draw_barchart(struct chart *ch, const char *title, - const char *xlabel, const char *ylabel, enum bar_opts opt) -{ - double d; - int i; + if (level0 == 0 && bc->n_vars > 1) + return value_compare_3way (&(*ap)->values[1], &(*bp)->values[1], bc->widths[1]); - double interval_size = fabs(ch->data_right - ch->data_left) / ( CATAGORIES ); + return level0; +} - double bar_width = interval_size / 1.1 ; +/* Print out a textual representation of a barchart. + This is intended only for testing, and not as a means + of visualising the data. +*/ +static void +barchart_dump (const struct barchart *bc, FILE *fp) +{ + fprintf (fp, "Graphic: Barchart\n"); + fprintf (fp, "Percentage: %d\n", bc->percent); + fprintf (fp, "Total Categories: %d\n", bc->n_nzcats); + fprintf (fp, "Primary Categories: %d\n", bc->n_pcats); + fprintf (fp, "Largest Category: %g\n", bc->largest); + fprintf (fp, "Total Count: %g\n", bc->total_count); - double ordinate_scale = fabs(ch->data_top - ch->data_bottom) / - fabs(y_max - y_min) ; + fprintf (fp, "Y Label: \"%s\"\n", bc->ylabel); - if ( opt != BAR_STACKED ) - bar_width /= SUB_CATAGORIES; + fprintf (fp, "Categorical Variables:\n"); + for (int i = 0; i < bc->n_vars; ++i) + { + fprintf (fp, " Var: \"%s\"\n", var_get_name (bc->var[i])); + } - /* Move to data bottom-left */ - pl_move_r(ch->lp, ch->data_left, ch->data_bottom); + fprintf (fp, "Categories:\n"); + struct category *cat; + struct category **cats = XCALLOC (hmap_count (&bc->primaries), struct category *); + int i = 0; + HMAP_FOR_EACH (cat, struct category, node, &bc->primaries) + { + cats[i++] = cat; + } + /* HMAP_FOR_EACH is not guaranteed to iterate in any particular order. So + we must sort here before we output the results. */ + sort (cats, i, sizeof (struct category *), compare_category_by_index_3way, bc); + for (i = 0; i < hmap_count (&bc->primaries); ++i) + { + const struct category *c = cats[i]; + fprintf (fp, " %d \"%s\"\n", c->idx, ds_cstr (&c->label)); + } + free (cats); - pl_savestate_r(ch->lp); - pl_filltype_r(ch->lp,1); + if (bc->ss) + { + fprintf (fp, "Sub-categories:\n"); + for (int i = 0; i < bc->n_nzcats / bc->n_pcats; ++i) + { + const struct category *cat = bc->ss[i]; + fprintf (fp, " %d \"%s\"\n", cat->idx, ds_cstr(&cat->label)); + } + } - /* Draw the data */ - for (i = 0 ; i < CATAGORIES ; ++i ) + fprintf (fp, "All Categories:\n"); + for (int i = 0; i < bc->n_nzcats; ++i) { - int sc; - double ystart=0.0; - double x = i * interval_size; + const struct freq *frq = bc->cats[i]; + fprintf (fp, "Count: %g; ", frq->count); - pl_savestate_r(ch->lp); + struct string s = DS_EMPTY_INITIALIZER; + var_append_value_name (bc->var[0], &frq->values[0], &s); - draw_tick (ch, TICK_ABSCISSA, x + (interval_size/2 ), "%s", - cat_labels[i]); + fprintf (fp, "Cat: \"%s\"", ds_cstr (&s)); + ds_clear (&s); - for(sc = 0 ; sc < SUB_CATAGORIES ; ++sc ) + if (bc->ss) { + var_append_value_name (bc->var[1], &frq->values[1], &s); + fprintf (fp, ", \"%s\"", ds_cstr (&s)); + } + ds_destroy (&s); + fputc ('\n', fp); + } - pl_savestate_r(ch->lp); - pl_fillcolorname_r(ch->lp,data_colour[sc % N_CHART_COLOURS]); + fputc ('\n', fp); +} - switch ( opt ) - { - case BAR_GROUPED: - pl_fboxrel_r(ch->lp, - x + (sc * bar_width ), 0, - x + (sc + 1) * bar_width, - sub_catagory[sc].data[i] * ordinate_scale ); - break; +/* Creates and returns a chart that will render a barchart with + the given TITLE and the N_CATS described in CATS. - case BAR_STACKED: + VAR is an array containing the categorical variables, and N_VAR + the number of them. N_VAR must be exactly 1 or 2. - pl_fboxrel_r(ch->lp, - x, ystart, - x + bar_width, - ystart + sub_catagory[sc].data[i] * ordinate_scale ); + CATS are the counts of the values of those variables. N_CATS is the + number of distinct values. +*/ +struct chart * +barchart_create (const struct variable **var, int n_vars, + const char *ylabel, bool percent, + struct freq *const *cats, int n_cats) +{ + int i; - ystart += sub_catagory[sc].data[i] * ordinate_scale ; + const int pidx = 0; + const int sidx = 1; - break; - default: - break; - } - pl_restorestate_r(ch->lp); - } + int width = var_get_width (var[pidx]); - pl_restorestate_r(ch->lp); - } - pl_restorestate_r(ch->lp); + assert (n_vars >= 1 && n_vars <= 2); - for ( d = y_min; d <= y_max ; d += y_tick ) - { + struct barchart *bar = XZALLOC (struct barchart); + bar->percent = percent; + bar->var = var; + bar->n_vars = n_vars; + bar->n_nzcats = n_cats; + chart_init (&bar->chart, &barchart_class, var_to_string (var[pidx])); - draw_tick (ch, TICK_ORDINATE, - (d - y_min ) * ordinate_scale, "%g", d); + bar->largest = -1; + bar->ylabel = strdup (ylabel); - } + { + int idx = 0; + hmap_init (&bar->primaries); + + /* + Iterate the categories and create a hash table of the primary categories. + We need to do this to find out how many there are and to cache the labels. + */ + for (i = 0; i < n_cats; i++) + { + const struct freq *src = cats[i]; + size_t hash = value_hash (&src->values[pidx], width, 0); - /* Write the abscissa label */ - pl_move_r(ch->lp,ch->data_left, ch->abscissa_top); - pl_alabel_r(ch->lp,0,'t',xlabel); + struct category *foo; + int flag = 0; + HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->primaries) + { + if (value_equal (&foo->val, &src->values[pidx], width)) + { + flag = 1; + break; + } + } + if (!flag) + { + struct category *s = XZALLOC (struct category); + s->idx = idx++; + s->width = var_get_width (var[pidx]); + value_init (&s->val, s->width); + value_copy (&s->val, &src->values[pidx], s->width); + ds_init_empty (&s->label); + var_append_value_name (var[pidx], &s->val, &s->label); + + hmap_insert (&bar->primaries, &s->node, hash); + } + } - /* Write the ordinate label */ - pl_savestate_r(ch->lp); - pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right); - pl_textangle_r(ch->lp,90); - pl_alabel_r(ch->lp,0,0,ylabel); - pl_restorestate_r(ch->lp); + bar->n_pcats = hmap_count (&bar->primaries); + } + if (n_vars > 1) + { + hmap_init (&bar->secondaries); + int idx = 0; + /* Iterate the categories, and create a hash table of secondary categories */ + for (i = 0; i < n_cats; i++) + { + struct freq *src = cats[i]; - chart_write_title(ch, "%s", title); + struct category *foo; + int flag = 0; + size_t hash = value_hash (&src->values[sidx], var_get_width (var[sidx]), 0); + HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bar->secondaries) + { + if (value_equal (&foo->val, &src->values[sidx], var_get_width (var[sidx]))) + { + flag = 1; + break; + } + } - write_legend(ch); + if (!flag) + { + struct category *s = XZALLOC (struct category); + s->idx = idx++; + s->width = var_get_width (var[sidx]); + value_init (&s->val, s->width); + value_copy (&s->val, &src->values[sidx], var_get_width (var[sidx])); + ds_init_empty (&s->label); + var_append_value_name (var[sidx], &s->val, &s->label); + + hmap_insert (&bar->secondaries, &s->node, hash); + bar->ss = xrealloc (bar->ss, idx * sizeof *bar->ss); + bar->ss[idx - 1] = s; + } + } + int n_category = hmap_count (&bar->secondaries); -} + sort (bar->ss, n_category, sizeof *bar->ss, + compare_category_3way, bar); + } + /* Deep copy. Not necessary for cmd line, but essential for the GUI, + since an expose callback will access these structs which may not + exist. + */ + bar->cats = xcalloc (n_cats, sizeof *bar->cats); + bar->widths[0] = var_get_width (bar->var[0]); + if (n_vars > 1) + bar->widths[1] = var_get_width (bar->var[1]); + { + struct hmap level2table; + hmap_init (&level2table); + int x = 0; + + for (i = 0; i < n_cats; i++) + { + struct freq *c = cats[i]; + + struct freq *foo; + bool flag = false; + size_t hash = hash_freq_2level_ptr (&c, bar); + HMAP_FOR_EACH_WITH_HASH (foo, struct freq, node, hash, &level2table) + { + if (0 == compare_freq_2level_ptr_3way (&foo, &c, bar)) + { + foo->count += c->count; + bar->total_count += c->count; + + if (foo->count > bar->largest) + bar->largest = foo->count; + + flag = true; + break; + } + } + + if (!flag) + { + struct freq *aggregated_freq = freq_clone (c, n_vars, bar->widths); + hmap_insert (&level2table, &aggregated_freq->node, hash); + + if (c->count > bar->largest) + bar->largest = aggregated_freq->count; + + bar->total_count += c->count; + bar->cats[x++] = aggregated_freq; + } + } + + bar->n_nzcats = hmap_count (&level2table); + hmap_destroy (&level2table); + } + + sort (bar->cats, bar->n_nzcats, sizeof *bar->cats, + compare_freq_2level_ptr_3way, bar); + + if (settings_get_testing_mode ()) + barchart_dump (bar, stdout); + + return &bar->chart; +} static void -write_legend(struct chart *chart) +destroy_cat_map (struct hmap *m) { - int sc; + struct category *foo = NULL; + struct category *next = NULL; + HMAP_FOR_EACH_SAFE (foo, next, struct category, node, m) + { + value_destroy (&foo->val, foo->width); - pl_savestate_r(chart->lp); + ds_destroy (&foo->label); + free (foo); + } - pl_filltype_r(chart->lp,1); + hmap_destroy (m); +} - pl_move_r(chart->lp, chart->legend_left, - chart->data_bottom + chart->font_size * SUB_CATAGORIES * 1.5); +static void +barchart_destroy (struct chart *chart) +{ + struct barchart *bar = to_barchart (chart); - pl_alabel_r(chart->lp,0,'b',subcat_name); + int i; - for (sc = 0 ; sc < SUB_CATAGORIES ; ++sc ) + destroy_cat_map (&bar->primaries); + if (bar->ss) { - pl_fmove_r(chart->lp, - chart->legend_left, - chart->data_bottom + chart->font_size * sc * 1.5); - - pl_savestate_r(chart->lp); - pl_fillcolorname_r(chart->lp,data_colour[sc]); - pl_fboxrel_r (chart->lp, - 0,0, - chart->font_size, chart->font_size); - pl_restorestate_r(chart->lp); - - pl_fmove_r(chart->lp, - chart->legend_left + chart->font_size * 1.5, - chart->data_bottom + chart->font_size * sc * 1.5); - - pl_alabel_r(chart->lp,'l','b',sub_catagory[sc].label); + destroy_cat_map (&bar->secondaries); } + for (i = 0; i < bar->n_nzcats; i++) + { + freq_destroy (bar->cats[i], bar->n_vars, bar->widths); + } - pl_restorestate_r(chart->lp); + free (bar->cats); + free (bar->ylabel); + free (bar->ss); + free (bar); } + +const struct chart_class barchart_class = + { + barchart_destroy + };