1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <gsl/gsl_histogram.h>
26 #include <gsl/gsl_randist.h>
29 #include <output/charts/plot-hist.h>
30 #include <output/charts/plot-chart.h>
32 #include <data/variable.h>
33 #include <libpspp/hash.h>
34 #include <output/chart.h>
37 #define _(msgid) gettext (msgid)
39 /* Write the legend of the chart */
41 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
47 pl_savestate_r(ch->lp);
49 sprintf(buf,"N = %.2f",norm->N);
50 pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
51 pl_alabel_r(ch->lp,0,'b',buf);
53 sprintf(buf,"Mean = %.1f",norm->mean);
54 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
55 pl_alabel_r(ch->lp,0,'b',buf);
57 sprintf(buf,"Std. Dev = %.2f",norm->stddev);
58 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
59 pl_alabel_r(ch->lp,0,'b',buf);
61 pl_restorestate_r(ch->lp);
64 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
68 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
79 const size_t bins = gsl_histogram_bins(hist);
80 const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
81 const double width = (ch->data_right - ch->data_left) / (double) bins ;
84 assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
86 assert( upper >= lower);
88 height = gsl_histogram_get(hist, bar) *
89 (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
91 pl_savestate_r(ch->lp);
92 pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
93 pl_fillcolorname_r(ch->lp, ch->fill_colour);
94 pl_filltype_r(ch->lp,1);
99 x_pos + width, height);
101 pl_restorestate_r(ch->lp);
105 snprintf(buf,5,"%g",(upper + lower) / 2.0);
106 draw_tick(ch, TICK_ABSCISSA,
107 x_pos + width / 2.0, buf);
116 histogram_plot(const gsl_histogram *hist,
117 const char *factorname,
118 const struct normal_curve *norm, short show_normal)
126 chart_write_title(ch, _("HISTOGRAM"));
128 chart_write_ylabel(ch, _("Frequency"));
129 chart_write_xlabel(ch, factorname);
131 if ( ! hist ) /* If this happens, probably all values are SYSMIS */
138 bins = gsl_histogram_bins(hist);
141 chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
143 for ( i = 0 ; i < bins ; ++i )
144 hist_draw_bar(ch, hist, i);
146 histogram_write_legend(ch, norm);
150 /* Draw the normal curve */
153 double x_min, x_max, not_used ;
154 double abscissa_scale ;
155 double ordinate_scale ;
158 gsl_histogram_get_range(hist, 0, &x_min, ¬_used);
159 range = not_used - x_min;
160 gsl_histogram_get_range(hist, bins - 1, ¬_used, &x_max);
161 assert(range == x_max - not_used);
163 abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
164 ordinate_scale = (ch->data_top - ch->data_bottom) /
165 gsl_histogram_max_val(hist) ;
167 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
168 for( d = ch->data_left;
169 d <= ch->data_right ;
170 d += (ch->data_right - ch->data_left) / 100.0)
172 const double x = (d - ch->data_left) / abscissa_scale + x_min ;
173 const double y = norm->N * range *
174 gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
176 pl_fcont_r(ch->lp, d, ch->data_bottom + y * ordinate_scale);
179 pl_endpath_r(ch->lp);