3ba3f001c5959dec57748e61a22487cb39b5c130
[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
24 #include "output/cairo-chart.h"
25
26 #include "gl/minmax.h"
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31
32 static void
33 draw_bar (cairo_t *cr, const struct xrchart_geometry *geom,
34           const struct barchart *bc, int bar)
35 {
36   const double width =
37     (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) 
38     / 
39     (double) bc->n_bars ;
40
41   const double x_pos =
42     (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) *
43     bar 
44     / 
45     (double) bc->n_bars ;
46
47
48   double height = geom->axis[SCALE_ORDINATE].scale * bc->bars[bar].magnitude;
49
50   cairo_rectangle (cr,
51                    geom->axis[SCALE_ABSCISSA].data_min + x_pos + width * 0.1,
52                    geom->axis[SCALE_ORDINATE].data_min,
53                    width * 0.8, height);
54   cairo_save (cr);
55   cairo_set_source_rgb (cr,
56                         geom->fill_colour.red / 255.0,
57                         geom->fill_colour.green / 255.0,
58                         geom->fill_colour.blue / 255.0);
59   cairo_fill_preserve (cr);
60   cairo_restore (cr);
61   cairo_stroke (cr);
62
63   draw_tick (cr, geom, SCALE_ABSCISSA, true,
64              x_pos + width / 2.0, "%s", ds_cstr (&bc->bars[bar].label));
65 }
66
67
68 void
69 xrchart_draw_barchart (const struct chart_item *chart_item, cairo_t *cr,
70                        struct xrchart_geometry *geom)
71 {
72   struct barchart *bc = to_barchart (chart_item);
73   int i;
74
75   xrchart_write_title (cr, geom, _("BARCHART"));
76
77   xrchart_write_ylabel (cr, geom, bc->ylabel);
78   xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
79
80   xrchart_write_yscale (cr, geom, 0, bc->largest);
81
82   for (i = 0; i < bc->n_bars; i++)
83     {
84       draw_bar (cr, geom, bc, i);
85     }
86 }
87