1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2015, 2016 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"
23 #include "data/variable.h"
25 #include "output/cairo-chart.h"
27 #include "gl/minmax.h"
30 #define _(msgid) gettext (msgid)
35 abscissa_label (const struct barchart *bc, cairo_t *cr,
36 struct xrchart_geometry *geom,
37 const union value *prev,
42 struct category *foo = NULL;
43 size_t hash = value_hash (prev, bc->widths[0], 0);
44 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bc->primaries)
46 if (value_equal (&foo->val, prev, bc->widths[0]))
50 draw_tick (cr, geom, SCALE_ABSCISSA, false,
51 x_pos - (width * n_last_cat) / 2.0,
52 "%s", ds_cstr (&foo->label));
59 xrchart_draw_barchart (const struct chart_item *chart_item, cairo_t *cr,
60 struct xrchart_geometry *geom)
62 struct barchart *bc = to_barchart (chart_item);
65 xrchart_write_title (cr, geom, _("Bar Chart"));
67 xrchart_write_ylabel (cr, geom, bc->ylabel);
68 xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
71 xrchart_write_yscale (cr, geom, 0, bc->largest * 100.0 / bc->total_count );
73 xrchart_write_yscale (cr, geom, 0, bc->largest);
75 const double abscale = geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min;
76 const double width = abscale / (double) (bc->n_nzcats + bc->n_pcats);
78 double x_pos = 0.5 * width;
79 union value *prev = NULL;
83 const int blob_size = 13;
84 const int height = blob_size * (hmap_count (&bc->secondaries) * 2);
87 geom->axis[SCALE_ABSCISSA].data_max + 10,
88 geom->axis[SCALE_ORDINATE].data_max - height,
93 int ypos = blob_size * 1.5;
94 for (i = 0 ; i < hmap_count (&bc->secondaries) ; ++i)
96 const struct category *foo = bc->ss[i];
99 geom->axis[SCALE_ABSCISSA].data_max + (1.5 * blob_size) + 20,
100 geom->axis[SCALE_ORDINATE].data_max - ypos);
102 xrchart_label (cr, 'l', 'b', geom->font_size, ds_cstr (&foo->label));
105 geom->axis[SCALE_ABSCISSA].data_max + 20,
106 geom->axis[SCALE_ORDINATE].data_max - ypos,
107 blob_size, blob_size);
110 cairo_set_source_rgb (cr,
111 data_colour[foo->idx].red / 255.0,
112 data_colour[foo->idx].green / 255.0,
113 data_colour[foo->idx].blue / 255.0);
114 cairo_fill_preserve (cr);
120 ypos += blob_size * 2;
125 for (i = 0; i < bc->n_nzcats; i++)
127 double height = geom->axis[SCALE_ORDINATE].scale * bc->cats[i]->count;
129 height *= 100.0 / bc->total_count ;
131 if (prev && !value_equal (prev, &bc->cats[i]->values[0], bc->widths[0]))
133 abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
140 geom->axis[SCALE_ABSCISSA].data_min + x_pos,
141 geom->axis[SCALE_ORDINATE].data_min,
149 struct category *foo;
150 size_t hash = value_hash (&bc->cats[i]->values[1], bc->widths[1], 0);
151 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bc->secondaries)
153 if (value_equal (&foo->val, &bc->cats[i]->values[1], bc->widths[1]))
161 cairo_set_source_rgb (cr,
162 data_colour[cidx].red / 255.0,
163 data_colour[cidx].green / 255.0,
164 data_colour[cidx].blue / 255.0);
165 cairo_fill_preserve (cr);
172 prev = &bc->cats[i]->values[0];
176 abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);