bug #62189: Fixed clipped numbers in histogram legend
[pspp] / src / output / charts / plot-hist-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 2014, 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 #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_max - geom->font_size;
39   cairo_save (cr);
40
41   if (mean != SYSMIS)
42     {
43       char *buf = xasprintf (_("Mean"));
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       buf = xasprintf ("%g", mean);
49       cairo_move_to (cr,geom->legend_left, y);
50       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
51       y -= geom->font_size * 2.0;
52       free (buf);
53     }
54
55   if (stddev != SYSMIS)
56     {
57       char *buf = xasprintf (_("Std Dev"));
58       cairo_move_to (cr, geom->legend_left, y);
59       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
60       free (buf);
61       y -= geom->font_size * 1.5;
62       buf = xasprintf ("%g", stddev);
63       cairo_move_to (cr, geom->legend_left, y);
64       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
65       free (buf);
66       y -= geom->font_size * 2.0;
67     }
68
69   if (n != SYSMIS)
70     {
71       char *buf = xasprintf (_("N = %.0f"), n);
72       cairo_move_to (cr, geom->legend_left, y);
73       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
74       free (buf);
75     }
76
77   cairo_restore (cr);
78 }
79
80 static void
81 hist_draw_bar (cairo_t *cr, const struct xrchart_geometry *geom,
82                const gsl_histogram *h, int bar)
83 {
84   double upper;
85   double lower;
86   double height;
87
88   assert (0 == gsl_histogram_get_range (h, bar, &lower, &upper));
89   assert (upper >= lower);
90
91   const double x_pos =
92     (lower - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale
93     +  geom->axis[SCALE_ABSCISSA].data_min;
94   const double width = (upper - lower) * geom->axis[SCALE_ABSCISSA].scale;
95
96   height = geom->axis[SCALE_ORDINATE].scale * gsl_histogram_get (h, bar);
97
98   cairo_rectangle (cr,
99                    x_pos,
100                    geom->axis[SCALE_ORDINATE].data_min,
101                    width, height);
102   cairo_save (cr);
103   cairo_set_source_rgb (cr,
104                         geom->fill_colour.red / 255.0,
105                         geom->fill_colour.green / 255.0,
106                         geom->fill_colour.blue / 255.0);
107   cairo_fill_preserve (cr);
108   cairo_restore (cr);
109   cairo_stroke (cr);
110 }
111
112 void
113 xrchart_draw_histogram (const struct chart *chart, cairo_t *cr,
114                         struct xrchart_geometry *geom)
115 {
116   struct histogram_chart *h = to_histogram_chart (chart);
117   int i;
118   int bins;
119
120   xrchart_write_title (cr, geom, _("HISTOGRAM"));
121
122   xrchart_write_ylabel (cr, geom, _("Frequency"));
123   xrchart_write_xlabel (cr, geom, chart_get_title (chart));
124
125   if (h->gsl_hist == NULL)
126     {
127       /* Probably all values are SYSMIS. */
128       return;
129     }
130
131   if (! xrchart_write_yscale (cr, geom, 0, gsl_histogram_max_val (h->gsl_hist)))
132     return;
133   if (! xrchart_write_xscale (cr, geom, gsl_histogram_min (h->gsl_hist),
134                               gsl_histogram_max (h->gsl_hist)))
135     return;
136
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
141   for (i = 0; i < bins; i++)
142     {
143       hist_draw_bar (cr, geom, h->gsl_hist, i);
144     }
145
146   histogram_write_legend (cr, geom, h->n, h->mean, h->stddev);
147
148   if (h->show_normal
149       && h->n != SYSMIS && h->mean != SYSMIS && h->stddev != SYSMIS)
150     {
151       /* Draw the normal curve */
152       double x_min, x_max;
153       double ordinate_scale;
154       double binwidth;
155       double x;
156
157       gsl_histogram_get_range (h->gsl_hist, 0, &x_min, &x_max);
158       binwidth = x_max - x_min;
159
160       /* The integral over the histogram is binwidth * sum(bin_i), while the integral over the pdf is 1 */
161       /* Therefore the pdf has to be scaled accordingly such that the integrals are equal               */
162       ordinate_scale = binwidth * gsl_histogram_sum(h->gsl_hist);
163
164       /* Clip normal curve to the rectangle formed by the axes. */
165       cairo_save (cr);
166       cairo_rectangle (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min,
167                        geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min,
168                        geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min);
169       cairo_clip (cr);
170
171       cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
172       for (x = geom->axis[SCALE_ABSCISSA].min;
173            x <= geom->axis[SCALE_ABSCISSA].max;
174            x += (geom->axis[SCALE_ABSCISSA].max - geom->axis[SCALE_ABSCISSA].min) / 100.0)
175         {
176           const double y = gsl_ran_gaussian_pdf (x - h->mean, h->stddev) * ordinate_scale;
177           /* Transform to drawing coordinates */
178           const double x_pos = (x - geom->axis[SCALE_ABSCISSA].min) * geom->axis[SCALE_ABSCISSA].scale + geom->axis[SCALE_ABSCISSA].data_min;
179           const double y_pos = (y - geom->axis[SCALE_ORDINATE].min) * geom->axis[SCALE_ORDINATE].scale + geom->axis[SCALE_ORDINATE].data_min;
180           cairo_line_to (cr, x_pos, y_pos);
181         }
182       cairo_stroke (cr);
183
184       cairo_restore (cr);
185     }
186 }