Instead of making system or portable file readers responsible for
[pspp-builds.git] / src / histogram.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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #include <stdio.h>
21 #include <plot.h>
22 #include <stdarg.h>
23 #include <math.h>
24 #include "hash.h"
25
26 #include "var.h"
27 #include "chart.h"
28
29 /* Number of bins in which to divide data */
30 #define BINS 15
31
32 /* The approximate no of ticks on the y axis */
33 #define YTICKS 10
34
35 #define M_PI ( 22.0 / 7.0 )
36
37
38 static double gaussian(double x, double mu, double sigma ) ;
39
40
41 static double
42 gaussian(double x, double mu, double sigma )
43 {
44   return (exp( - (( x - mu )* (x - mu) / (2.0 * sigma * sigma)  ))
45     / ( sigma * sqrt( M_PI * 2.0) ))  ;
46 }
47
48
49
50 /* Write the legend of the chart */
51 static void
52 write_legend(struct chart *ch, struct normal_curve *norm)
53 {
54   char buf[100];
55   pl_savestate_r(ch->lp);
56
57   sprintf(buf,"N = %.2f",norm->N);
58   pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
59   pl_alabel_r(ch->lp,0,'b',buf);
60
61   sprintf(buf,"Mean = %.1f",norm->mean);
62   pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
63   pl_alabel_r(ch->lp,0,'b',buf);
64
65   sprintf(buf,"Std. Dev = %.2f",norm->stddev);
66   pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
67   pl_alabel_r(ch->lp,0,'b',buf);
68
69   pl_restorestate_r(ch->lp);    
70 }
71
72
73
74
75 /* Draw a histogram.
76    If show_normal is non zero then superimpose a normal curve
77 */
78 void
79 draw_histogram(struct chart *ch, 
80                const struct variable *var,
81                const struct freq_tab *frq_tab,
82                const char *title, 
83                struct normal_curve *norm,
84                int show_normal)
85 {
86
87   double d;
88   int count;
89
90   double x_min = DBL_MAX;
91   double x_max = -DBL_MAX;
92   double y_min = DBL_MAX;
93   double y_max = -DBL_MAX;
94   
95   double y_tick ;
96
97
98   double ordinate_values[BINS];
99
100   struct hsh_iterator hi;
101   struct hsh_table *fh = frq_tab->data;
102   struct freq *frq;
103
104   double interval_size = fabs(ch->data_right - ch->data_left) / ( BINS );
105
106   /* Find out the extremes of the x value 
107      FIXME: These don't need to be calculated here, since the 
108      calling routine should know them */
109
110   for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) ) 
111     {
112       if ( frq->v.f < x_min ) x_min = frq->v.f ;
113       if ( frq->v.f > x_max ) x_max = frq->v.f ;
114     }
115
116   double x_interval = fabs(x_max - x_min) / ( BINS - 1);
117
118   
119   double abscissa_scale = 
120     fabs( (ch->data_right -  ch->data_left) / (x_max - x_min) ) ;
121
122
123   /* Find out the bin values */
124   for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
125     {
126
127       double y = 0;
128
129       for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) ) 
130         {
131           if ( frq->v.f >= d && frq->v.f < d + x_interval )
132             y += frq->c;
133         }
134
135       ordinate_values[count] = y ;
136
137       if ( y > y_max ) y_max = y ;
138       if ( y < y_min ) y_min = y;
139     }
140
141   y_tick = chart_rounded_tick( ( y_max - y_min ) / (double) (YTICKS - 1));
142
143
144   y_min = floor( y_min / y_tick ) * y_tick ;
145   y_max = ceil( y_max / y_tick ) * y_tick ;
146
147   double ordinate_scale =
148     fabs(ch->data_top -  ch->data_bottom) / fabs(y_max - y_min) ;
149   
150
151   /* Move to data bottom-left */
152   pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
153
154   pl_savestate_r(ch->lp);
155   pl_fillcolorname_r(ch->lp, ch->fill_colour); 
156   pl_filltype_r(ch->lp,1);
157
158   /* Draw the histogram */
159   for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
160     {
161       const double x = count * interval_size ;
162       pl_savestate_r(ch->lp);
163
164       draw_tick (ch, TICK_ABSCISSA, x + (interval_size / 2.0 ), "%.1f",d);
165
166       pl_fboxrel_r(ch->lp, x, 0, x + interval_size, 
167                    ordinate_values[count] * ordinate_scale );
168
169       pl_restorestate_r(ch->lp);
170
171     }
172   pl_restorestate_r(ch->lp);
173
174   /* Put the y axis on */
175   for ( d = y_min; d <= y_max ; d += y_tick )
176     {
177       draw_tick (ch, TICK_ORDINATE, (d - y_min ) * ordinate_scale, "%g", d);
178     }
179
180   /* Write the abscissa label */
181   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
182   pl_alabel_r(ch->lp,0,'t',var->label ? var->label : var->name);
183
184  
185   /* Write the ordinate label */
186   pl_savestate_r(ch->lp);
187   pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right);
188   pl_textangle_r(ch->lp,90);
189   pl_alabel_r(ch->lp,0,0,"Frequency");
190
191   pl_restorestate_r(ch->lp);
192
193
194   chart_write_title(ch, title);
195
196
197   /* Write the legend */
198   write_legend(ch,norm);
199
200
201   if ( show_normal  )
202   {
203     /* Draw the normal curve */
204     double d;
205
206     pl_move_r(ch->lp, ch->data_left, ch->data_bottom);    
207     for( d = ch->data_left; 
208          d <= ch->data_right ; 
209          d += (ch->data_right - ch->data_left) / 100.0)
210       {
211         const double x = (d  - ch->data_left  - interval_size / 2.0  ) / 
212           abscissa_scale  + x_min ; 
213         
214         pl_fcont_r(ch->lp,  d,
215                    ch->data_bottom +
216                    norm->N * gaussian(x, norm->mean, norm->stddev) 
217                    * ordinate_scale);
218
219       }
220     pl_endpath_r(ch->lp);
221   }
222
223 }
224
225