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