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 *chart, cairo_t *cr,
60 struct xrchart_geometry *geom)
62 struct barchart *bc = to_barchart (chart);
65 xrchart_write_title (cr, geom, _("Bar Chart"));
67 xrchart_write_ylabel (cr, geom, bc->ylabel);
68 xrchart_write_xlabel (cr, geom, chart_get_title (chart));
70 if (! xrchart_write_yscale (cr, geom, 0,
71 bc->percent ? bc->largest * 100.0 / bc->total_count : bc->largest))
74 const double abscale = geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min;
75 const double width = abscale / (double) (bc->n_nzcats + bc->n_pcats);
77 double x_pos = 0.5 * width;
78 union value *prev = NULL;
82 const int blob_size = 13;
83 const int height = blob_size * (hmap_count (&bc->secondaries) * 2);
86 geom->axis[SCALE_ABSCISSA].data_max + 10,
87 geom->axis[SCALE_ORDINATE].data_max - height,
92 int ypos = blob_size * 1.5;
93 for (i = 0 ; i < hmap_count (&bc->secondaries) ; ++i)
95 const struct category *foo = bc->ss[i];
98 geom->axis[SCALE_ABSCISSA].data_max + (1.5 * blob_size) + 20,
99 geom->axis[SCALE_ORDINATE].data_max - ypos);
101 xrchart_label (cr, 'l', 'b', geom->font_size, ds_cstr (&foo->label));
104 geom->axis[SCALE_ABSCISSA].data_max + 20,
105 geom->axis[SCALE_ORDINATE].data_max - ypos,
106 blob_size, blob_size);
109 cairo_set_source_rgb (cr,
110 data_colour[foo->idx].red / 255.0,
111 data_colour[foo->idx].green / 255.0,
112 data_colour[foo->idx].blue / 255.0);
113 cairo_fill_preserve (cr);
119 ypos += blob_size * 2;
124 for (i = 0; i < bc->n_nzcats; i++)
126 double height = geom->axis[SCALE_ORDINATE].scale * bc->cats[i]->count;
128 height *= 100.0 / bc->total_count ;
130 if (prev && !value_equal (prev, &bc->cats[i]->values[0], bc->widths[0]))
132 abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
139 geom->axis[SCALE_ABSCISSA].data_min + x_pos,
140 geom->axis[SCALE_ORDINATE].data_min,
148 struct category *foo;
149 size_t hash = value_hash (&bc->cats[i]->values[1], bc->widths[1], 0);
150 HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bc->secondaries)
152 if (value_equal (&foo->val, &bc->cats[i]->values[1], bc->widths[1]))
160 cairo_set_source_rgb (cr,
161 data_colour[cidx].red / 255.0,
162 data_colour[cidx].green / 255.0,
163 data_colour[cidx].blue / 255.0);
164 cairo_fill_preserve (cr);
171 prev = &bc->cats[i]->values[0];
175 abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);