Implemented long variable names a la spss V12.
[pspp-builds.git] / src / 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 /* If you add/modify any public symbols in this file, don't forget to
21    change the stubs in dummy-chart.c */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <plot.h>
27 #include <math.h>
28 #include <gsl/gsl_histogram.h>
29 #include <gsl/gsl_randist.h>
30 #include <assert.h>
31
32 #include "hash.h"
33
34 #include "var.h"
35 #include "chart.h"
36
37 /* Write the legend of the chart */
38 void
39 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
40 {
41   char buf[100];
42   if ( !ch )
43           return ;
44
45   pl_savestate_r(ch->lp);
46
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);
50
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);
54
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);
58
59   pl_restorestate_r(ch->lp);    
60 }
61
62 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
63
64
65 static void
66 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
67 {
68   double upper;
69   double lower;
70   double height;
71
72   if ( !ch ) 
73           return ;
74   const size_t bins = gsl_histogram_bins(hist);
75   const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
76   const double width = (ch->data_right - ch->data_left) / (double) bins ;
77
78
79   assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
80
81   assert( upper >= lower);
82
83   height = gsl_histogram_get(hist, bar) * 
84     (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
85
86   pl_savestate_r(ch->lp);
87   pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
88   pl_fillcolorname_r(ch->lp, ch->fill_colour); 
89   pl_filltype_r(ch->lp,1);
90
91
92   pl_fboxrel_r(ch->lp,
93                x_pos, 0,
94                x_pos + width, height);
95
96   pl_restorestate_r(ch->lp);
97
98   {
99   char buf[5];
100   snprintf(buf,5,"%g",(upper + lower) / 2.0);
101   draw_tick(ch, TICK_ABSCISSA,
102             x_pos + width / 2.0, buf);
103   }
104
105 }
106
107
108
109
110 void
111 histogram_plot(const gsl_histogram *hist,
112                const char *factorname,
113                const struct normal_curve *norm, short show_normal)
114 {
115   int i;
116   int bins;
117   
118   struct chart *ch;
119
120   bins = gsl_histogram_bins(hist);
121
122   ch = chart_create();
123   chart_write_title(ch, _("HISTOGRAM"));
124
125   chart_write_ylabel(ch, _("Frequency"));
126   chart_write_xlabel(ch, factorname);
127
128   chart_write_yscale(ch, 0, gsl_histogram_max_val(hist), 5);
129
130   for ( i = 0 ; i < bins ; ++i ) 
131       hist_draw_bar(ch, hist, i);
132
133   histogram_write_legend(ch, norm);
134
135   if ( show_normal  )
136   {
137     /* Draw the normal curve */    
138
139     double d ;
140     double x_min, x_max, not_used ;
141     double abscissa_scale ;
142     double ordinate_scale ;
143     double range ;
144
145     gsl_histogram_get_range(hist, 0, &x_min, &not_used);
146     range = not_used - x_min;
147     gsl_histogram_get_range(hist, bins - 1, &not_used, &x_max);
148     assert(range == x_max - not_used);
149
150     abscissa_scale = (ch->data_right - ch->data_left) / (x_max - x_min);
151     ordinate_scale = (ch->data_top - ch->data_bottom) / 
152       gsl_histogram_max_val(hist) ;
153
154     pl_move_r(ch->lp, ch->data_left, ch->data_bottom);    
155     for( d = ch->data_left; 
156          d <= ch->data_right ; 
157          d += (ch->data_right - ch->data_left) / 100.0)
158       {    
159         const double x = (d - ch->data_left) / abscissa_scale + x_min ; 
160         const double y = norm->N * range * 
161           gsl_ran_gaussian_pdf(x - norm->mean, norm->stddev);
162
163         pl_fcont_r(ch->lp,  d,  ch->data_bottom  + y * ordinate_scale);
164
165       }
166     pl_endpath_r(ch->lp);
167
168   }
169   chart_submit(ch);
170 }
171