Added histograms into examine.
[pspp] / src / cartesian.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
21 #include <math.h>
22 #include "chart.h"
23 #include <assert.h>
24
25
26
27 struct dataset
28 {
29   int n_data;
30   const char *label;
31 };
32
33
34
35
36 #define DATASETS 2
37
38 static const struct dataset dataset[DATASETS] = 
39   {
40     { 13, "male"},
41     { 11, "female"},
42   };
43
44
45
46
47 static void
48 write_legend(struct chart *chart, const char *heading, int n);
49
50
51
52 /* Write the abscissa label */
53 void 
54 chart_write_xlabel(struct chart *ch, const char *label)
55 {
56
57   pl_savestate_r(ch->lp);
58
59   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
60   pl_alabel_r(ch->lp,0,'t',label);
61
62   pl_restorestate_r(ch->lp);
63
64 }
65
66
67
68 /* Write the ordinate label */
69 void 
70 chart_write_ylabel(struct chart *ch, const char *label)
71 {
72   pl_savestate_r(ch->lp);
73
74   pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
75   pl_textangle_r(ch->lp, 90);
76   pl_alabel_r(ch->lp, 0, 0, label);
77
78   pl_restorestate_r(ch->lp);
79 }
80
81
82
83 static void
84 write_legend(struct chart *chart, const char *heading, 
85              int n)
86 {
87   int ds;
88
89   pl_savestate_r(chart->lp);
90
91   pl_filltype_r(chart->lp,1);
92
93   pl_move_r(chart->lp, chart->legend_left, 
94             chart->data_bottom + chart->font_size * n * 1.5);
95
96   pl_alabel_r(chart->lp,0,'b',heading);
97
98   for (ds = 0 ; ds < n ; ++ds ) 
99     {
100       pl_fmove_r(chart->lp,
101                  chart->legend_left,
102                  chart->data_bottom + chart->font_size * ds  * 1.5);
103
104       pl_savestate_r(chart->lp);    
105       pl_fillcolorname_r(chart->lp,data_colour[ds]);
106       pl_fboxrel_r (chart->lp,
107                     0,0,
108                     chart->font_size, chart->font_size);
109       pl_restorestate_r(chart->lp);    
110
111       pl_fmove_r(chart->lp,
112                  chart->legend_left + chart->font_size * 1.5,
113                  chart->data_bottom + chart->font_size * ds  * 1.5);
114
115       pl_alabel_r(chart->lp,'l','b',dataset[ds].label);
116     }
117
118
119   pl_restorestate_r(chart->lp);    
120 }
121
122
123 /* Plot a data point */
124 void
125 chart_datum(struct chart *ch, int dataset UNUSED, double x, double y)
126 {
127   const double x_pos = 
128     (x - ch->x_min) * ch->abscissa_scale + ch->data_left ; 
129
130   const double y_pos = 
131     (y - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
132
133
134   pl_savestate_r(ch->lp);    
135   
136   pl_fmarker_r(ch->lp, x_pos, y_pos, 6, 15);
137
138   pl_restorestate_r(ch->lp);    
139
140 }
141
142 /* Draw a line with slope SLOPE and intercept INTERCEPT.
143    between the points limit1 and limit2.
144    If lim_dim is CHART_DIM_Y then the limit{1,2} are on the 
145    y axis otherwise the x axis
146 */
147 void
148 chart_line(struct chart *ch, double slope, double intercept, 
149            double limit1, double limit2, enum CHART_DIM lim_dim)
150 {
151   double x1, y1;
152   double x2, y2 ;
153
154   if ( lim_dim == CHART_DIM_Y ) 
155     {
156       x1 = ( limit1 - intercept ) / slope ;
157       x2 = ( limit2 - intercept ) / slope ;
158       y1 = limit1;
159       y2 = limit2;
160     }
161   else
162     {
163       x1 = limit1;
164       x2 = limit2;
165       y1 = slope * x1 + intercept;
166       y2 = slope * x2 + intercept;
167     }
168
169   y1 = (y1 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
170   y2 = (y2 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
171   x1 = (x1 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
172   x2 = (x2 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
173
174   pl_savestate_r(ch->lp);    
175
176   pl_fline_r(ch->lp, x1, y1, x2, y2);
177
178   pl_restorestate_r(ch->lp);    
179   
180 }