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