Added npplot and detrended np plots
[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 char *title, 
82                struct normal_curve *norm,
83                int show_normal)
84 {
85
86   double d;
87   int count;
88
89   double x_min = DBL_MAX;
90   double x_max = -DBL_MAX;
91   double y_min = DBL_MAX;
92   double y_max = -DBL_MAX;
93   
94   double y_tick ;
95
96
97   double ordinate_values[BINS];
98
99   const struct freq_tab *frq_tab = &var->p.frq.tab ;
100
101   struct hsh_iterator hi;
102   struct hsh_table *fh = frq_tab->data;
103   struct freq *frq;
104
105   double interval_size = fabs(ch->data_right - ch->data_left) / ( BINS );
106
107   /* Find out the extremes of the x value 
108      FIXME: These don't need to be calculated here, since the 
109      calling routine should know them */
110
111   for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) ) 
112     {
113       if ( frq->v.f < x_min ) x_min = frq->v.f ;
114       if ( frq->v.f > x_max ) x_max = frq->v.f ;
115     }
116
117   double x_interval = fabs(x_max - x_min) / ( BINS - 1);
118
119   
120   double abscissa_scale = 
121     fabs( (ch->data_right -  ch->data_left) / (x_max - x_min) ) ;
122
123
124   /* Find out the bin values */
125   for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
126     {
127
128       double y = 0;
129
130       for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) ) 
131         {
132           if ( frq->v.f >= d && frq->v.f < d + x_interval )
133             y += frq->c;
134         }
135
136       ordinate_values[count] = y ;
137
138       if ( y > y_max ) y_max = y ;
139       if ( y < y_min ) y_min = y;
140     }
141
142   y_tick = chart_rounded_tick( ( y_max - y_min ) / (double) (YTICKS - 1));
143
144
145   y_min = floor( y_min / y_tick ) * y_tick ;
146   y_max = ceil( y_max / y_tick ) * y_tick ;
147
148   double ordinate_scale =
149     fabs(ch->data_top -  ch->data_bottom) / fabs(y_max - y_min) ;
150   
151
152   /* Move to data bottom-left */
153   pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
154
155   pl_savestate_r(ch->lp);
156   pl_fillcolorname_r(ch->lp, ch->fill_colour); 
157   pl_filltype_r(ch->lp,1);
158
159   /* Draw the histogram */
160   for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
161     {
162       const double x = count * interval_size ;
163       pl_savestate_r(ch->lp);
164
165       draw_tick (ch, TICK_ABSCISSA, x + (interval_size / 2.0 ), "%.1f",d);
166
167       pl_fboxrel_r(ch->lp, x, 0, x + interval_size, 
168                    ordinate_values[count] * ordinate_scale );
169
170       pl_restorestate_r(ch->lp);
171
172     }
173   pl_restorestate_r(ch->lp);
174
175   /* Put the y axis on */
176   for ( d = y_min; d <= y_max ; d += y_tick )
177     {
178       draw_tick (ch, TICK_ORDINATE, (d - y_min ) * ordinate_scale, "%g", d);
179     }
180
181   /* Write the abscissa label */
182   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
183   pl_alabel_r(ch->lp,0,'t',var->label ? var->label : var->name);
184
185  
186   /* Write the ordinate label */
187   pl_savestate_r(ch->lp);
188   pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right);
189   pl_textangle_r(ch->lp,90);
190   pl_alabel_r(ch->lp,0,0,"Frequency");
191
192   pl_restorestate_r(ch->lp);
193
194
195   chart_write_title(ch, title);
196
197
198   /* Write the legend */
199   write_legend(ch,norm);
200
201
202   if ( show_normal  )
203   {
204     /* Draw the normal curve */
205     double d;
206
207     pl_move_r(ch->lp, ch->data_left, ch->data_bottom);    
208     for( d = ch->data_left; 
209          d <= ch->data_right ; 
210          d += (ch->data_right - ch->data_left) / 100.0)
211       {
212         const double x = (d  - ch->data_left  - interval_size / 2.0  ) / 
213           abscissa_scale  + x_min ; 
214         
215         pl_fcont_r(ch->lp,  d,
216                    ch->data_bottom +
217                    norm->N * gaussian(x, norm->mean, norm->stddev) 
218                    * ordinate_scale);
219
220       }
221     pl_endpath_r(ch->lp);
222   }
223
224 }
225
226