Added the permissions command.
[pspp-builds.git] / 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 /* Set the scale for the abscissa */
67 void 
68 chart_write_xscale(struct chart *ch, double min, double max, double tick)
69 {
70   double x;
71
72   ch->x_max = ceil( max / tick ) * tick ; 
73   ch->x_min = floor ( min / tick ) * tick ;
74
75   ch->abscissa_scale = fabs(ch->data_right - ch->data_left) / 
76     fabs(ch->x_max - ch->x_min);
77
78   for(x = ch->x_min ; x <= ch->x_max; x += tick )
79       draw_tick (ch, TICK_ABSCISSA, (x - ch->x_min) * ch->abscissa_scale, "%g", x);
80
81 }
82
83
84 /* Set the scale for the ordinate */
85 void 
86 chart_write_yscale(struct chart *ch, double min, double max, double tick)
87 {
88   double y;
89
90   ch->y_max = ceil( max / tick ) * tick ; 
91   ch->y_min = floor ( min / tick ) * tick ;
92
93   ch->ordinate_scale = 
94     fabs(ch->data_top -  ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
95
96   for(y = ch->y_min ; y <= ch->y_max; y += tick )
97     {
98     draw_tick (ch, TICK_ORDINATE, 
99                (y - ch->y_min) * ch->ordinate_scale, "%g", y);
100     }
101
102 }
103
104
105
106 /* Write the ordinate label */
107 void 
108 chart_write_ylabel(struct chart *ch, const char *label)
109 {
110   pl_savestate_r(ch->lp);
111
112   pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
113   pl_textangle_r(ch->lp, 90);
114   pl_alabel_r(ch->lp, 0, 0, label);
115
116   pl_restorestate_r(ch->lp);
117 }
118
119
120
121 static void
122 write_legend(struct chart *chart, const char *heading, 
123              int n)
124 {
125   int ds;
126
127   pl_savestate_r(chart->lp);
128
129   pl_filltype_r(chart->lp,1);
130
131   pl_move_r(chart->lp, chart->legend_left, 
132             chart->data_bottom + chart->font_size * n * 1.5);
133
134   pl_alabel_r(chart->lp,0,'b',heading);
135
136   for (ds = 0 ; ds < n ; ++ds ) 
137     {
138       pl_fmove_r(chart->lp,
139                  chart->legend_left,
140                  chart->data_bottom + chart->font_size * ds  * 1.5);
141
142       pl_savestate_r(chart->lp);    
143       pl_fillcolorname_r(chart->lp,data_colour[ds]);
144       pl_fboxrel_r (chart->lp,
145                     0,0,
146                     chart->font_size, chart->font_size);
147       pl_restorestate_r(chart->lp);    
148
149       pl_fmove_r(chart->lp,
150                  chart->legend_left + chart->font_size * 1.5,
151                  chart->data_bottom + chart->font_size * ds  * 1.5);
152
153       pl_alabel_r(chart->lp,'l','b',dataset[ds].label);
154     }
155
156
157   pl_restorestate_r(chart->lp);    
158 }
159
160
161 /* Plot a data point */
162 void
163 chart_datum(struct chart *ch, int dataset UNUSED, double x, double y)
164 {
165   const double x_pos = 
166     (x - ch->x_min) * ch->abscissa_scale + ch->data_left ; 
167
168   const double y_pos = 
169     (y - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
170
171
172   pl_savestate_r(ch->lp);    
173   
174   pl_fmarker_r(ch->lp, x_pos, y_pos, 6, 15);
175
176   pl_restorestate_r(ch->lp);    
177
178 }
179
180 /* Draw a line with slope SLOPE and intercept INTERCEPT.
181    between the points limit1 and limit2.
182    If lim_dim is CHART_DIM_Y then the limit{1,2} are on the 
183    y axis otherwise the x axis
184 */
185 void
186 chart_line(struct chart *ch, double slope, double intercept, 
187            double limit1, double limit2, enum CHART_DIM lim_dim)
188 {
189   double x1, y1;
190   double x2, y2 ;
191
192   if ( lim_dim == CHART_DIM_Y ) 
193     {
194       x1 = ( limit1 - intercept ) / slope ;
195       x2 = ( limit2 - intercept ) / slope ;
196       y1 = limit1;
197       y2 = limit2;
198     }
199   else
200     {
201       x1 = limit1;
202       x2 = limit2;
203       y1 = slope * x1 + intercept;
204       y2 = slope * x2 + intercept;
205     }
206
207   y1 = (y1 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
208   y2 = (y2 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
209   x1 = (x1 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
210   x2 = (x2 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
211
212   pl_savestate_r(ch->lp);    
213
214   pl_fline_r(ch->lp, x1, y1, x2, y2);
215
216   pl_restorestate_r(ch->lp);    
217   
218 }