397715ff386d38884ce4ca9838ec39a1bb648c74
[pspp] / src / output / charts / barchart-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2015 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   xrchart_write_yscale (cr, geom, 0, bc->largest);
71
72   const double abscale = geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min;
73   const double width = abscale / (double) (bc->n_nzcats + bc->n_pcats);
74
75   double x_pos = 0.5 * width;
76   union value *prev = NULL;
77
78   if (bc->ss)
79     {
80       const int blob_size = 13;
81       const int height = blob_size * (hmap_count (&bc->secondaries) * 2);
82       
83       cairo_rectangle (cr,
84                        geom->axis[SCALE_ABSCISSA].data_max + 10,
85                        geom->axis[SCALE_ORDINATE].data_max - height,
86                        100, height);
87
88       cairo_stroke (cr);
89
90       int ypos = blob_size * 1.5;
91       for (i = 0 ; i < hmap_count (&bc->secondaries) ; ++i)
92         {
93           const struct category *foo = bc->ss[i];
94
95           cairo_move_to (cr, 
96                          geom->axis[SCALE_ABSCISSA].data_max + (1.5 * blob_size) + 20,
97                          geom->axis[SCALE_ORDINATE].data_max - ypos);
98           
99           xrchart_label (cr, 'l', 'b', geom->font_size, ds_cstr (&foo->label));
100
101           cairo_rectangle (cr,
102                            geom->axis[SCALE_ABSCISSA].data_max + 20,
103                            geom->axis[SCALE_ORDINATE].data_max - ypos,
104                            blob_size, blob_size);
105
106           cairo_save (cr);
107           cairo_set_source_rgb (cr,
108                                 data_colour[foo->idx].red / 255.0,
109                                 data_colour[foo->idx].green / 255.0,
110                                 data_colour[foo->idx].blue / 255.0);
111           cairo_fill_preserve (cr);
112
113           cairo_restore (cr);
114           
115           cairo_stroke (cr);
116
117           ypos += blob_size * 2;
118         }
119     }
120
121   int n_last_cat = 0;
122   for (i = 0; i < bc->n_nzcats; i++)
123     {
124       double height = geom->axis[SCALE_ORDINATE].scale * bc->cats[i]->count;
125
126       if (prev && !value_equal (prev, &bc->cats[i]->values[0], bc->widths[0]))
127         {
128           abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
129
130           x_pos += width;
131           n_last_cat = 0;
132         }
133
134       cairo_rectangle (cr,
135                        geom->axis[SCALE_ABSCISSA].data_min + x_pos,
136                        geom->axis[SCALE_ORDINATE].data_min,
137                        width, height);
138       cairo_save (cr);
139
140
141       int cidx = 0;
142       if (bc->ss)
143         {
144           struct category *foo;
145           size_t hash = value_hash (&bc->cats[i]->values[1], bc->widths[1], 0);
146           HMAP_FOR_EACH_WITH_HASH (foo, struct category, node, hash, &bc->secondaries)
147             {
148               if (value_equal (&foo->val, &bc->cats[i]->values[1], bc->widths[1]))
149                 {
150                   cidx = foo->idx;
151                   break;
152                 }
153             }
154         }
155
156       cairo_set_source_rgb (cr,
157                             data_colour[cidx].red / 255.0,
158                             data_colour[cidx].green / 255.0,
159                             data_colour[cidx].blue / 255.0);
160       cairo_fill_preserve (cr);
161
162       cairo_restore (cr);
163       cairo_stroke (cr);
164
165       x_pos += width;
166
167       prev = &bc->cats[i]->values[0];
168       n_last_cat ++;
169     }
170
171   abscissa_label (bc, cr, geom, prev, x_pos, width, n_last_cat);
172 }
173