22201e4e4e4daa3c6caf8f2f805aa243abb5382b
[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   if ( ! ch ) 
57     return ;
58
59   pl_savestate_r(ch->lp);
60
61   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
62   pl_alabel_r(ch->lp,0,'t',label);
63
64   pl_restorestate_r(ch->lp);
65
66 }
67
68
69
70 /* Write the ordinate label */
71 void 
72 chart_write_ylabel(struct chart *ch, const char *label)
73 {
74   if ( ! ch ) 
75     return ;
76
77   pl_savestate_r(ch->lp);
78
79   pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
80   pl_textangle_r(ch->lp, 90);
81   pl_alabel_r(ch->lp, 0, 0, label);
82
83   pl_restorestate_r(ch->lp);
84 }
85
86
87
88 static void
89 write_legend(struct chart *chart, const char *heading, 
90              int n)
91 {
92   int ds;
93
94   if ( ! chart ) 
95     return ;
96
97
98   pl_savestate_r(chart->lp);
99
100   pl_filltype_r(chart->lp,1);
101
102   pl_move_r(chart->lp, chart->legend_left, 
103             chart->data_bottom + chart->font_size * n * 1.5);
104
105   pl_alabel_r(chart->lp,0,'b',heading);
106
107   for (ds = 0 ; ds < n ; ++ds ) 
108     {
109       pl_fmove_r(chart->lp,
110                  chart->legend_left,
111                  chart->data_bottom + chart->font_size * ds  * 1.5);
112
113       pl_savestate_r(chart->lp);    
114       pl_fillcolorname_r(chart->lp,data_colour[ds]);
115       pl_fboxrel_r (chart->lp,
116                     0,0,
117                     chart->font_size, chart->font_size);
118       pl_restorestate_r(chart->lp);    
119
120       pl_fmove_r(chart->lp,
121                  chart->legend_left + chart->font_size * 1.5,
122                  chart->data_bottom + chart->font_size * ds  * 1.5);
123
124       pl_alabel_r(chart->lp,'l','b',dataset[ds].label);
125     }
126
127
128   pl_restorestate_r(chart->lp);    
129 }
130
131
132 /* Plot a data point */
133 void
134 chart_datum(struct chart *ch, int dataset UNUSED, double x, double y)
135 {
136
137   if ( ! ch ) 
138     return ;
139
140
141
142   const double x_pos = 
143     (x - ch->x_min) * ch->abscissa_scale + ch->data_left ; 
144
145   const double y_pos = 
146     (y - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
147
148   pl_savestate_r(ch->lp);    
149   
150   pl_fmarker_r(ch->lp, x_pos, y_pos, 6, 15);
151
152   pl_restorestate_r(ch->lp);    
153
154 }
155
156 /* Draw a line with slope SLOPE and intercept INTERCEPT.
157    between the points limit1 and limit2.
158    If lim_dim is CHART_DIM_Y then the limit{1,2} are on the 
159    y axis otherwise the x axis
160 */
161 void
162 chart_line(struct chart *ch, double slope, double intercept, 
163            double limit1, double limit2, enum CHART_DIM lim_dim)
164 {
165   double x1, y1;
166   double x2, y2 ;
167
168   if ( ! ch ) 
169     return ;
170
171
172   if ( lim_dim == CHART_DIM_Y ) 
173     {
174       x1 = ( limit1 - intercept ) / slope ;
175       x2 = ( limit2 - intercept ) / slope ;
176       y1 = limit1;
177       y2 = limit2;
178     }
179   else
180     {
181       x1 = limit1;
182       x2 = limit2;
183       y1 = slope * x1 + intercept;
184       y2 = slope * x2 + intercept;
185     }
186
187   y1 = (y1 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
188   y2 = (y2 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
189   x1 = (x1 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
190   x2 = (x2 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
191
192   pl_savestate_r(ch->lp);    
193
194   pl_fline_r(ch->lp, x1, y1, x2, y2);
195
196   pl_restorestate_r(ch->lp);    
197   
198 }