00b33fc6b0f182b5fa7f92b6dd97c3e0d5f6af5a
[pspp] / src / output / charts / barchart-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2015, 2016 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "output/charts/barchart.h"
20 #include "output/charts/piechart.h"
21
22 #include <math.h>
23 #include "data/variable.h"
24
25 #include "output/cairo-chart.h"
26
27 #include "gl/minmax.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32
33
34 static void
35 abscissa_label (const struct barchart *bc, cairo_t *cr,
36                 struct xrchart_geometry *geom,
37                 const union value *prev,
38                 double x_pos,
39                 double width,
40                 int n_last_cat)
41 {
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)
45     {
46       if (value_equal (&foo->val, prev, bc->widths[0]))
47         break;
48     }
49
50   draw_tick (cr, geom, SCALE_ABSCISSA, false,
51              x_pos - (width * n_last_cat) / 2.0,
52              "%s",  ds_cstr (&foo->label));
53 }
54
55
56
57
58 void
59 xrchart_draw_barchart (const struct chart_item *chart_item, cairo_t *cr,
60                        struct xrchart_geometry *geom)
61 {
62   struct barchart *bc = to_barchart (chart_item);
63   int i;
64
65   xrchart_write_title (cr, geom, _("Bar Chart"));
66
67   xrchart_write_ylabel (cr, geom, bc->ylabel);
68   xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
69
70   if (! xrchart_write_yscale (cr, geom, 0,
71                               bc->percent ? bc->largest * 100.0 / bc->total_count : bc->largest))
72     return;
73
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);
76
77   double x_pos = 0.5 * width;
78   union value *prev = NULL;
79
80   if (bc->ss)
81     {
82       const int blob_size = 13;
83       const int height = blob_size * (hmap_count (&bc->secondaries) * 2);
84
85       cairo_rectangle (cr,
86                        geom->axis[SCALE_ABSCISSA].data_max + 10,
87                        geom->axis[SCALE_ORDINATE].data_max - height,
88                        100, height);
89
90       cairo_stroke (cr);
91
92       int ypos = blob_size * 1.5;
93       for (i = 0 ; i < hmap_count (&bc->secondaries) ; ++i)
94         {
95           const struct category *foo = bc->ss[i];
96
97           cairo_move_to (cr,
98                          geom->axis[SCALE_ABSCISSA].data_max + (1.5 * blob_size) + 20,
99                          geom->axis[SCALE_ORDINATE].data_max - ypos);
100
101           xrchart_label (cr, 'l', 'b', geom->font_size, ds_cstr (&foo->label));
102
103           cairo_rectangle (cr,
104                            geom->axis[SCALE_ABSCISSA].data_max + 20,
105                            geom->axis[SCALE_ORDINATE].data_max - ypos,
106                            blob_size, blob_size);
107
108           cairo_save (cr);
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);
114
115           cairo_restore (cr);
116
117           cairo_stroke (cr);
118
119           ypos += blob_size * 2;
120         }
121     }
122
123   int n_last_cat = 0;
124   for (i = 0; i < bc->n_nzcats; i++)
125     {
126       double height = geom->axis[SCALE_ORDINATE].scale * bc->cats[i]->count;
127       if (bc->percent)
128         height *= 100.0  /  bc->total_count ;
129
130       if (prev && !value_equal (prev, &bc->cats[i]->values[0], bc->widths[0]))
131         {
132           abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
133
134           x_pos += width;
135           n_last_cat = 0;
136         }
137
138       cairo_rectangle (cr,
139                        geom->axis[SCALE_ABSCISSA].data_min + x_pos,
140                        geom->axis[SCALE_ORDINATE].data_min,
141                        width, height);
142       cairo_save (cr);
143
144
145       int cidx = 0;
146       if (bc->ss)
147         {
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)
151             {
152               if (value_equal (&foo->val, &bc->cats[i]->values[1], bc->widths[1]))
153                 {
154                   cidx = foo->idx;
155                   break;
156                 }
157             }
158         }
159
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);
165
166       cairo_restore (cr);
167       cairo_stroke (cr);
168
169       x_pos += width;
170
171       prev = &bc->cats[i]->values[0];
172       n_last_cat ++;
173     }
174
175   abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
176 }
177