a70fa174fa7b72511856e67b359a18c12ece6d99
[pspp-builds.git] / src / output / charts / plot-hist-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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/plot-hist.h>
20
21 #include <gsl/gsl_randist.h>
22
23 #include <data/val-type.h>
24 #include <output/cairo-chart.h>
25
26 #include "gl/xvasprintf.h"
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31 /* Write the legend of the chart */
32 static void
33 histogram_write_legend (cairo_t *cr, const struct xrchart_geometry *geom,
34                         double n, double mean, double stddev)
35 {
36   double y = geom->data_bottom;
37   cairo_save (cr);
38
39   if (n != SYSMIS)
40     {
41       char *buf = xasprintf ("N = %.2f", n);
42       cairo_move_to (cr, geom->legend_left, y);
43       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
44       y += geom->font_size * 1.5;
45       free (buf);
46     }
47
48   if (mean != SYSMIS)
49     {
50       char *buf = xasprintf ("Mean = %.1f", mean);
51       cairo_move_to (cr,geom->legend_left, y);
52       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
53       y += geom->font_size * 1.5;
54       free (buf);
55     }
56
57   if (stddev != SYSMIS)
58     {
59       char *buf = xasprintf ("Std. Dev = %.2f", stddev);
60       cairo_move_to (cr, geom->legend_left, y);
61       xrchart_label (cr, 'l', 'b', geom->font_size, buf);
62       free (buf);
63     }
64
65   cairo_restore (cr);
66 }
67
68 static void
69 hist_draw_bar (cairo_t *cr, const struct xrchart_geometry *geom,
70                const gsl_histogram *h, int bar)
71 {
72   double upper;
73   double lower;
74   double height;
75
76   const size_t bins = gsl_histogram_bins (h);
77   const double x_pos = (geom->data_right - geom->data_left) * bar / (double) bins ;
78   const double width = (geom->data_right - geom->data_left) / (double) bins ;
79
80   assert ( 0 == gsl_histogram_get_range (h, bar, &lower, &upper));
81
82   assert ( upper >= lower);
83
84   height = gsl_histogram_get (h, bar) *
85     (geom->data_top - geom->data_bottom) / gsl_histogram_max_val (h);
86
87   cairo_rectangle (cr, geom->data_left + x_pos, geom->data_bottom,
88                    width, height);
89   cairo_save (cr);
90   cairo_set_source_rgb (cr,
91                         geom->fill_colour.red / 255.0,
92                         geom->fill_colour.green / 255.0,
93                         geom->fill_colour.blue / 255.0);
94   cairo_fill_preserve (cr);
95   cairo_restore (cr);
96   cairo_stroke (cr);
97
98   draw_tick (cr, geom, TICK_ABSCISSA,
99              x_pos + width / 2.0, "%g", (upper + lower) / 2.0);
100 }
101
102 void
103 xrchart_draw_histogram (const struct chart_item *chart_item, cairo_t *cr,
104                         struct xrchart_geometry *geom)
105 {
106   struct histogram_chart *h = to_histogram_chart (chart_item);
107   int i;
108   int bins;
109
110   xrchart_write_title (cr, geom, _("HISTOGRAM"));
111
112   xrchart_write_ylabel (cr, geom, _("Frequency"));
113   xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
114
115   if (h->gsl_hist == NULL)
116     {
117       /* Probably all values are SYSMIS. */
118       return;
119     }
120
121   bins = gsl_histogram_bins (h->gsl_hist);
122
123   xrchart_write_yscale (cr, geom, 0, gsl_histogram_max_val (h->gsl_hist), 5);
124
125   for (i = 0; i < bins; i++)
126     hist_draw_bar (cr, geom, h->gsl_hist, i);
127
128   histogram_write_legend (cr, geom, h->n, h->mean, h->stddev);
129
130   if (h->show_normal
131       && h->n != SYSMIS && h->mean != SYSMIS && h->stddev != SYSMIS)
132     {
133       /* Draw the normal curve */
134       double d;
135       double x_min, x_max, not_used;
136       double abscissa_scale;
137       double ordinate_scale;
138       double range;
139
140       gsl_histogram_get_range (h->gsl_hist, 0, &x_min, &not_used);
141       range = not_used - x_min;
142       gsl_histogram_get_range (h->gsl_hist, bins - 1, &not_used, &x_max);
143
144       abscissa_scale = (geom->data_right - geom->data_left) / (x_max - x_min);
145       ordinate_scale = (geom->data_top - geom->data_bottom) /
146         gsl_histogram_max_val (h->gsl_hist);
147
148       cairo_move_to (cr, geom->data_left, geom->data_bottom);
149       for (d = geom->data_left;
150            d <= geom->data_right;
151            d += (geom->data_right - geom->data_left) / 100.0)
152         {
153           const double x = (d - geom->data_left) / abscissa_scale + x_min;
154           const double y = h->n * range *
155             gsl_ran_gaussian_pdf (x - h->mean, h->stddev);
156
157           cairo_line_to (cr, d, geom->data_bottom  + y * ordinate_scale);
158
159         }
160       cairo_stroke (cr);
161     }
162 }