FREQUENCIES BARCHART: Honour then PERCENT option
[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 (bc->percent)
71     xrchart_write_yscale (cr, geom, 0, bc->largest * 100.0 / bc->total_count );
72   else
73     xrchart_write_yscale (cr, geom, 0, bc->largest);
74
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);
77
78   double x_pos = 0.5 * width;
79   union value *prev = NULL;
80
81   if (bc->ss)
82     {
83       const int blob_size = 13;
84       const int height = blob_size * (hmap_count (&bc->secondaries) * 2);
85       
86       cairo_rectangle (cr,
87                        geom->axis[SCALE_ABSCISSA].data_max + 10,
88                        geom->axis[SCALE_ORDINATE].data_max - height,
89                        100, height);
90
91       cairo_stroke (cr);
92
93       int ypos = blob_size * 1.5;
94       for (i = 0 ; i < hmap_count (&bc->secondaries) ; ++i)
95         {
96           const struct category *foo = bc->ss[i];
97
98           cairo_move_to (cr, 
99                          geom->axis[SCALE_ABSCISSA].data_max + (1.5 * blob_size) + 20,
100                          geom->axis[SCALE_ORDINATE].data_max - ypos);
101           
102           xrchart_label (cr, 'l', 'b', geom->font_size, ds_cstr (&foo->label));
103
104           cairo_rectangle (cr,
105                            geom->axis[SCALE_ABSCISSA].data_max + 20,
106                            geom->axis[SCALE_ORDINATE].data_max - ypos,
107                            blob_size, blob_size);
108
109           cairo_save (cr);
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);
115
116           cairo_restore (cr);
117           
118           cairo_stroke (cr);
119
120           ypos += blob_size * 2;
121         }
122     }
123
124   int n_last_cat = 0;
125   for (i = 0; i < bc->n_nzcats; i++)
126     {
127       double height = geom->axis[SCALE_ORDINATE].scale * bc->cats[i]->count;
128       if (bc->percent)
129         height *= 100.0  /  bc->total_count ;
130
131       if (prev && !value_equal (prev, &bc->cats[i]->values[0], bc->widths[0]))
132         {
133           abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
134
135           x_pos += width;
136           n_last_cat = 0;
137         }
138
139       cairo_rectangle (cr,
140                        geom->axis[SCALE_ABSCISSA].data_min + x_pos,
141                        geom->axis[SCALE_ORDINATE].data_min,
142                        width, height);
143       cairo_save (cr);
144
145
146       int cidx = 0;
147       if (bc->ss)
148         {
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)
152             {
153               if (value_equal (&foo->val, &bc->cats[i]->values[1], bc->widths[1]))
154                 {
155                   cidx = foo->idx;
156                   break;
157                 }
158             }
159         }
160
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);
166
167       cairo_restore (cr);
168       cairo_stroke (cr);
169
170       x_pos += width;
171
172       prev = &bc->cats[i]->values[0];
173       n_last_cat ++;
174     }
175
176   abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
177 }
178