1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by John Darrington <john@darrington.wattle.id.au>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 /* If you add/modify any public symbols in this file, don't forget to
21 change the stubs in dummy-chart.c */
28 #include <gsl/gsl_histogram.h>
29 #include <gsl/gsl_randist.h>
37 /* Write the legend of the chart */
39 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
42 pl_savestate_r(ch->lp);
44 sprintf(buf,"N = %.2f",norm->N);
45 pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
46 pl_alabel_r(ch->lp,0,'b',buf);
48 sprintf(buf,"Mean = %.1f",norm->mean);
49 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
50 pl_alabel_r(ch->lp,0,'b',buf);
52 sprintf(buf,"Std. Dev = %.2f",norm->stddev);
53 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
54 pl_alabel_r(ch->lp,0,'b',buf);
56 pl_restorestate_r(ch->lp);
59 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
63 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
69 const size_t bins = gsl_histogram_bins(hist);
70 const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
71 const double width = (ch->data_right - ch->data_left) / (double) bins ;
74 assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
76 assert( upper >= lower);
78 height = gsl_histogram_get(hist, bar) *
79 (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
81 pl_savestate_r(ch->lp);
82 pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
83 pl_fillcolorname_r(ch->lp, ch->fill_colour);
84 pl_filltype_r(ch->lp,1);
89 x_pos + width, height);
91 pl_restorestate_r(ch->lp);
95 snprintf(buf,5,"%g",(upper + lower) / 2.0);
96 draw_tick(ch, TICK_ABSCISSA,
97 x_pos + width / 2.0, buf);
106 histogram_plot(const gsl_histogram *hist,
107 const char *factorname,
108 const struct normal_curve *norm, short show_normal)
115 bins = gsl_histogram_bins(hist);
118 chart_write_title(ch, _("HISTOGRAM"));
120 chart_write_ylabel(ch, _("Frequency"));
121 chart_write_xlabel(ch, factorname);
123 chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
125 for ( i = 0 ; i < bins ; ++i )
126 hist_draw_bar(ch, hist, i);
128 histogram_write_legend(ch, norm);
132 /* Draw the normal curve */
135 double x_min, x_max, not_used ;
136 double abscissa_scale ;
137 double ordinate_scale ;
140 gsl_histogram_get_range(hist, 0, &x_min, ¬_used);
141 range = not_used - x_min;
142 gsl_histogram_get_range(hist, bins - 1, ¬_used, &x_max);
143 assert(range == x_max - not_used);
145 abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
146 ordinate_scale = (ch->data_top - ch->data_bottom) /
147 gsl_histogram_max_val(hist) ;
149 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
150 for( d = ch->data_left;
151 d <= ch->data_right ;
152 d += (ch->data_right - ch->data_left) / 100.0)
154 const double x = (d - ch->data_left) / abscissa_scale + x_min ;
155 const double y = norm->N * range *
156 gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
158 pl_fcont_r(ch->lp, d, ch->data_bottom + y * ordinate_scale);
161 pl_endpath_r(ch->lp);