1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/charts/plot-hist.h"
21 #include <gsl/gsl_randist.h>
23 #include "data/val-type.h"
24 #include "output/cairo-chart.h"
26 #include "gl/xvasprintf.h"
29 #define _(msgid) gettext (msgid)
31 /* Write the legend of the chart */
33 histogram_write_legend (cairo_t *cr, const struct xrchart_geometry *geom,
34 double n, double mean, double stddev)
36 double y = geom->axis[SCALE_ORDINATE].data_min;
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;
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;
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);
69 hist_draw_bar (cairo_t *cr, const struct xrchart_geometry *geom,
70 const gsl_histogram *h, int bar, bool label)
76 const size_t bins = gsl_histogram_bins (h);
79 (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) *
83 (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / (double) bins ;
85 assert ( 0 == gsl_histogram_get_range (h, bar, &lower, &upper));
87 assert ( upper >= lower);
89 height = geom->axis[SCALE_ORDINATE].scale * gsl_histogram_get (h, bar);
92 geom->axis[SCALE_ABSCISSA].data_min + x_pos,
93 geom->axis[SCALE_ORDINATE].data_min,
96 cairo_set_source_rgb (cr,
97 geom->fill_colour.red / 255.0,
98 geom->fill_colour.green / 255.0,
99 geom->fill_colour.blue / 255.0);
100 cairo_fill_preserve (cr);
105 draw_tick (cr, geom, SCALE_ABSCISSA, bins > 10,
106 x_pos + width / 2.0, "%g", (upper + lower) / 2.0);
110 xrchart_draw_histogram (const struct chart_item *chart_item, cairo_t *cr,
111 struct xrchart_geometry *geom)
113 struct histogram_chart *h = to_histogram_chart (chart_item);
117 xrchart_write_title (cr, geom, _("HISTOGRAM"));
119 xrchart_write_ylabel (cr, geom, _("Frequency"));
120 xrchart_write_xlabel (cr, geom, chart_item_get_title (chart_item));
122 if (h->gsl_hist == NULL)
124 /* Probably all values are SYSMIS. */
128 bins = gsl_histogram_bins (h->gsl_hist);
130 xrchart_write_yscale (cr, geom, 0, gsl_histogram_max_val (h->gsl_hist), 5);
132 for (i = 0; i < bins; i++)
134 hist_draw_bar (cr, geom, h->gsl_hist, i, true);
137 histogram_write_legend (cr, geom, h->n, h->mean, h->stddev);
140 && h->n != SYSMIS && h->mean != SYSMIS && h->stddev != SYSMIS)
142 /* Draw the normal curve */
144 double x_min, x_max, not_used;
145 double abscissa_scale;
146 double ordinate_scale;
149 gsl_histogram_get_range (h->gsl_hist, 0, &x_min, ¬_used);
150 range = not_used - x_min;
151 gsl_histogram_get_range (h->gsl_hist, bins - 1, ¬_used, &x_max);
153 abscissa_scale = (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / (x_max - x_min);
154 ordinate_scale = (geom->axis[SCALE_ORDINATE].data_max - geom->axis[SCALE_ORDINATE].data_min) /
155 gsl_histogram_max_val (h->gsl_hist);
157 cairo_move_to (cr, geom->axis[SCALE_ABSCISSA].data_min, geom->axis[SCALE_ORDINATE].data_min);
158 for (d = geom->axis[SCALE_ABSCISSA].data_min;
159 d <= geom->axis[SCALE_ABSCISSA].data_max;
160 d += (geom->axis[SCALE_ABSCISSA].data_max - geom->axis[SCALE_ABSCISSA].data_min) / 100.0)
162 const double x = (d - geom->axis[SCALE_ABSCISSA].data_min) / abscissa_scale + x_min;
163 const double y = h->n * range *
164 gsl_ran_gaussian_pdf (x - h->mean, h->stddev);
166 cairo_line_to (cr, d, geom->axis[SCALE_ORDINATE].data_min + y * ordinate_scale);