7e2afee9b022cc621578623c889a474ad7b033dc
[pspp] / src / output / charts / plot-hist-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 2014 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 #include "math/chart-geometry.h"
19 #include "output/charts/plot-hist.h"
20
21 #include <float.h>
22 #include <gsl/gsl_randist.h>
23
24 #include "data/val-type.h"
25 #include "output/cairo-chart.h"
26
27 #include "gl/xvasprintf.h"
28 #include "gl/minmax.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 /* Write the legend of the chart */
34 static void
35 histogram_write_legend (cairo_t *cr, const struct xrchart_geometry *geom,
36                         double n, double mean, double stddev)
37 {
38   double y = geom->axis[SCALE_ORDINATE].data_min;
39   cairo_save (cr);
40
41   if (n != SYSMIS)
42     {
43       char *buf = xasprintf (_("N = %.2f"), n);
44       cairo_move_to (cr, geom->legend_left, y);
45       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
46       y += geom->font_size * 1.5;
47       free (buf);
48     }
49
50   if (mean != SYSMIS)
51     {
52       char *buf = xasprintf (_("Mean = %.1f"), mean);
53       cairo_move_to (cr,geom->legend_left, y);
54       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
55       y += geom->font_size * 1.5;
56       free (buf);
57     }
58
59   if (stddev != SYSMIS)
60     {
61       char *buf = xasprintf (_("Std. Dev = %.2f"), stddev);
62       cairo_move_to (cr, geom->legend_left, y);
63       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
64       free (buf);
65     }
66
67   cairo_restore (cr);
68 }
69
70 static void
71 hist_draw_bar (cairo_t *cr, const struct xrchart_geometry *geom,
72                const gsl_histogram *h, int bar, const char *tick_format_string,
73                const double tickscale, const bool tickoversize)
74 {
75   double upper;
76   double lower;
77   double height;
78
79   const size_t bins = gsl_histogram_bins (h);
80
81   const double x_pos =
82     (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) *
83     bar / (double) bins ;
84
85   const double width =
86     (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / (double) bins ;
87
88   assert ( 0 == gsl_histogram_get_range (h, bar, &lower, &upper));
89
90   assert ( upper >= lower);
91
92   height = geom->axis[SCALE_ORDINATE].scale * gsl_histogram_get (h, bar);
93
94   cairo_rectangle (cr,
95                    geom->axis[SCALE_ABSCISSA].data_min + x_pos,
96                    geom->axis[SCALE_ORDINATE].data_min,
97                    width, height);
98   cairo_save (cr);
99   cairo_set_source_rgb (cr,
100                         geom->fill_colour.red / 255.0,
101                         geom->fill_colour.green / 255.0,
102                         geom->fill_colour.blue / 255.0);
103   cairo_fill_preserve (cr);
104   cairo_restore (cr);
105   cairo_stroke (cr);
106
107   draw_tick (cr, geom, SCALE_ABSCISSA, tickoversize,
108              x_pos + width / 2.0, tick_format_string, (upper+lower)/2.0*tickscale);
109
110 }
111
112 void
113 xrchart_draw_histogram (const struct chart_item *chart_item, cairo_t *cr,
114                         struct xrchart_geometry *geom)
115 {
116   struct histogram_chart *h = to_histogram_chart (chart_item);
117   int i;
118   int bins;
119   char *tick_format_string;
120   char *test_text;
121   double width, left_width, right_width, unused;
122   double tickscale;
123   bool tickoversize;
124
125   xrchart_write_title (cr, geom, _("HISTOGRAM"));
126
127   xrchart_write_ylabel (cr, geom, _("Frequency"));
128   xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
129
130   if (h->gsl_hist == NULL)
131     {
132       /* Probably all values are SYSMIS. */
133       return;
134     }
135
136   xrchart_write_yscale (cr, geom, 0, gsl_histogram_max_val (h->gsl_hist));
137
138   /* Draw the ticks and compute if the rendered tick text is wider than the bin */
139   bins = gsl_histogram_bins (h->gsl_hist);
140   tick_format_string = chart_get_ticks_format (gsl_histogram_max (h->gsl_hist),
141                                                gsl_histogram_min (h->gsl_hist),
142                                                bins,
143                                                &tickscale);
144   test_text = xasprintf(tick_format_string, gsl_histogram_max (h->gsl_hist)*tickscale);
145   xrchart_text_extents (cr, geom, test_text, &right_width, &unused);
146   free(test_text);
147   test_text = xasprintf(tick_format_string, gsl_histogram_min (h->gsl_hist)*tickscale);
148   xrchart_text_extents (cr, geom, test_text, &left_width, &unused);
149   free(test_text);
150   width = MAX(left_width, right_width);
151   tickoversize = width > 0.9 *
152     ((double)(geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min))/bins;
153   for (i = 0; i < bins; i++)
154     {
155       hist_draw_bar (cr, geom, h->gsl_hist, i, tick_format_string, tickscale, tickoversize);
156     }
157   free(tick_format_string);
158
159   histogram_write_legend (cr, geom, h->n, h->mean, h->stddev);
160
161   if (h->show_normal
162       && h->n != SYSMIS && h->mean != SYSMIS && h->stddev != SYSMIS)
163     {
164       /* Draw the normal curve */
165       double d;
166       double x_min, x_max, not_used;
167       double abscissa_scale;
168       double ordinate_scale;
169       double range;
170
171       gsl_histogram_get_range (h->gsl_hist, 0, &x_min, &not_used);
172       range = not_used - x_min;
173       gsl_histogram_get_range (h->gsl_hist, bins - 1, &not_used, &x_max);
174
175       abscissa_scale = (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / (x_max - x_min);
176       ordinate_scale = (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min) /
177         gsl_histogram_max_val (h->gsl_hist);
178
179       cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
180       for (d = geom->axis[SCALE_ABSCISSA].data_min;
181            d <= geom->axis[SCALE_ABSCISSA].data_max;
182            d += (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / 100.0)
183         {
184           const double x = (d - geom->axis[SCALE_ABSCISSA].data_min) / abscissa_scale + x_min;
185           const double y = h->n * range *
186             gsl_ran_gaussian_pdf (x - h->mean, h->stddev);
187
188           cairo_line_to (cr, d, geom->axis[SCALE_ORDINATE].data_min  + y * ordinate_scale);
189
190         }
191       cairo_stroke (cr);
192     }
193 }