a500d5bf627933b125a39cbfbbd9a9766970ca8d
[pspp] / src / output / charts / plot-hist.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
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.
9
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.
14
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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20
21 #include <config.h>
22
23 #include <stdio.h>
24 #include <plot.h>
25 #include <math.h>
26 #include <gsl/gsl_histogram.h>
27 #include <gsl/gsl_randist.h>
28 #include <assert.h>
29
30 #include <output/charts/plot-hist.h>
31 #include <output/charts/plot-chart.h>
32
33 #include <data/variable.h>
34 #include <libpspp/hash.h>
35 #include <output/chart.h>
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* Write the legend of the chart */
41 void
42 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
43 {
44   char buf[100];
45   if ( !ch )
46     return ;
47
48   pl_savestate_r(ch->lp);
49
50   sprintf(buf,"N = %.2f",norm->N);
51   pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
52   pl_alabel_r(ch->lp,0,'b',buf);
53
54   sprintf(buf,"Mean = %.1f",norm->mean);
55   pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
56   pl_alabel_r(ch->lp,0,'b',buf);
57
58   sprintf(buf,"Std. Dev = %.2f",norm->stddev);
59   pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
60   pl_alabel_r(ch->lp,0,'b',buf);
61
62   pl_restorestate_r(ch->lp);    
63 }
64
65 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
66
67
68 static void
69 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
70 {
71   if ( !ch ) 
72     return ;
73
74
75   {
76     double upper;
77     double lower;
78     double height;
79
80     const size_t bins = gsl_histogram_bins(hist);
81     const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
82     const double width = (ch->data_right - ch->data_left) / (double) bins ;
83
84
85     assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
86
87     assert( upper >= lower);
88
89     height = gsl_histogram_get(hist, bar) * 
90       (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
91
92     pl_savestate_r(ch->lp);
93     pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
94     pl_fillcolorname_r(ch->lp, ch->fill_colour); 
95     pl_filltype_r(ch->lp,1);
96
97
98     pl_fboxrel_r(ch->lp,
99                  x_pos, 0,
100                  x_pos + width, height);
101
102     pl_restorestate_r(ch->lp);
103
104     {
105       char buf[5];
106       snprintf(buf,5,"%g",(upper + lower) / 2.0);
107       draw_tick(ch, TICK_ABSCISSA,
108                 x_pos + width / 2.0, buf);
109     }
110   }
111 }
112
113
114
115
116 void
117 histogram_plot(const gsl_histogram *hist,
118                const char *factorname,
119                const struct normal_curve *norm, short show_normal)
120 {
121   int i;
122   int bins;
123   
124   struct chart *ch;
125
126   ch = chart_create();
127   chart_write_title(ch, _("HISTOGRAM"));
128
129   chart_write_ylabel(ch, _("Frequency"));
130   chart_write_xlabel(ch, factorname);
131
132   if ( ! hist ) /* If this happens, probably all values are SYSMIS */
133     {
134       chart_submit(ch);
135       return ;
136     }
137   else
138     {
139       bins = gsl_histogram_bins(hist);
140     }
141
142   chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
143
144   for ( i = 0 ; i < bins ; ++i ) 
145       hist_draw_bar(ch, hist, i);
146
147   histogram_write_legend(ch, norm);
148
149   if ( show_normal  )
150   {
151     /* Draw the normal curve */    
152
153     double d ;
154     double x_min, x_max, not_used ;
155     double abscissa_scale ;
156     double ordinate_scale ;
157     double range ;
158
159     gsl_histogram_get_range(hist, 0, &x_min, &not_used);
160     range = not_used - x_min;
161     gsl_histogram_get_range(hist, bins - 1, &not_used, &x_max);
162     assert(range == x_max - not_used);
163
164     abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
165     ordinate_scale = (ch->data_top - ch->data_bottom) / 
166       gsl_histogram_max_val(hist) ;
167
168     pl_move_r(ch->lp, ch->data_left, ch->data_bottom);    
169     for( d = ch->data_left; 
170          d <= ch->data_right ; 
171          d += (ch->data_right - ch->data_left) / 100.0)
172       {    
173         const double x = (d - ch->data_left) / abscissa_scale + x_min ; 
174         const double y = norm->N * range * 
175           gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
176
177         pl_fcont_r(ch->lp,  d,  ch->data_bottom  + y * ordinate_scale);
178
179       }
180     pl_endpath_r(ch->lp);
181
182   }
183   chart_submit(ch);
184 }
185