1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004 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/>. */
23 #include <gsl/gsl_histogram.h>
24 #include <gsl/gsl_randist.h>
27 #include <output/charts/plot-hist.h>
28 #include <output/charts/plot-chart.h>
30 #include <data/variable.h>
31 #include <libpspp/hash.h>
32 #include <output/chart.h>
35 #define _(msgid) gettext (msgid)
37 /* Write the legend of the chart */
39 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
45 pl_savestate_r(ch->lp);
47 sprintf(buf,"N = %.2f",norm->N);
48 pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
49 pl_alabel_r(ch->lp,0,'b',buf);
51 sprintf(buf,"Mean = %.1f",norm->mean);
52 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
53 pl_alabel_r(ch->lp,0,'b',buf);
55 sprintf(buf,"Std. Dev = %.2f",norm->stddev);
56 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
57 pl_alabel_r(ch->lp,0,'b',buf);
59 pl_restorestate_r(ch->lp);
62 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
66 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
77 const size_t bins = gsl_histogram_bins(hist);
78 const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
79 const double width = (ch->data_right - ch->data_left) / (double) bins ;
82 assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
84 assert( upper >= lower);
86 height = gsl_histogram_get(hist, bar) *
87 (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
89 pl_savestate_r(ch->lp);
90 pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
91 pl_fillcolorname_r(ch->lp, ch->fill_colour);
92 pl_filltype_r(ch->lp,1);
97 x_pos + width, height);
99 pl_restorestate_r(ch->lp);
103 snprintf(buf,5,"%g",(upper + lower) / 2.0);
104 draw_tick(ch, TICK_ABSCISSA,
105 x_pos + width / 2.0, buf);
114 histogram_plot(const gsl_histogram *hist,
115 const char *factorname,
116 const struct normal_curve *norm, short show_normal)
124 chart_write_title(ch, _("HISTOGRAM"));
126 chart_write_ylabel(ch, _("Frequency"));
127 chart_write_xlabel(ch, factorname);
129 if ( ! hist ) /* If this happens, probably all values are SYSMIS */
136 bins = gsl_histogram_bins(hist);
139 chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
141 for ( i = 0 ; i < bins ; ++i )
142 hist_draw_bar(ch, hist, i);
144 histogram_write_legend(ch, norm);
148 /* Draw the normal curve */
151 double x_min, x_max, not_used ;
152 double abscissa_scale ;
153 double ordinate_scale ;
156 gsl_histogram_get_range(hist, 0, &x_min, ¬_used);
157 range = not_used - x_min;
158 gsl_histogram_get_range(hist, bins - 1, ¬_used, &x_max);
160 abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
161 ordinate_scale = (ch->data_top - ch->data_bottom) /
162 gsl_histogram_max_val(hist) ;
164 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
165 for( d = ch->data_left;
166 d <= ch->data_right ;
167 d += (ch->data_right - ch->data_left) / 100.0)
169 const double x = (d - ch->data_left) / abscissa_scale + x_min ;
170 const double y = norm->N * range *
171 gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
173 pl_fcont_r(ch->lp, d, ch->data_bottom + y * ordinate_scale);
176 pl_endpath_r(ch->lp);