4fd67aaf0bb915a1555f00452417ad1882f98484
[pspp-builds.git] / src / output / charts / plot-chart.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 #include <config.h>
21
22 #include <stdio.h>
23 #include <plot.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <float.h>
28 #include <assert.h>
29 #include <math.h>
30
31 #include <output/charts/plot-chart.h>
32
33 #include <math/chart-geometry.h>
34
35
36
37 #include <libpspp/str.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <output/manager.h>
41 #include <output/output.h>
42
43
44 const char *const data_colour[] = {
45   "brown",
46   "red",
47   "orange",
48   "yellow",
49   "green",
50   "blue",
51   "violet",
52   "grey",
53   "pink"
54 };
55
56
57
58 /* Draw a tick mark at position
59    If label is non zero, then print it at the tick mark
60 */
61 void
62 draw_tick(struct chart *chart, 
63           enum tick_orientation orientation, 
64           double position, 
65           const char *label, ...)
66 {
67   const int tickSize = 10;
68
69   assert(chart);
70
71   pl_savestate_r(chart->lp);
72
73   pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
74
75   if ( orientation == TICK_ABSCISSA ) 
76     pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
77   else if (orientation == TICK_ORDINATE ) 
78       pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
79   else
80     NOT_REACHED ();
81
82   if ( label ) {
83     char buf[10];
84     va_list ap;
85     va_start(ap,label);
86     vsnprintf(buf,10,label,ap);
87
88     if ( orientation == TICK_ABSCISSA ) 
89       pl_alabel_r(chart->lp, 'c','t', buf);
90     else if (orientation == TICK_ORDINATE ) 
91       {
92         if ( fabs(position) < DBL_EPSILON )
93             pl_moverel_r(chart->lp, 0, 10);
94
95         pl_alabel_r(chart->lp, 'r','c', buf);
96       }
97
98     va_end(ap);
99   }
100     
101   pl_restorestate_r(chart->lp);
102 }
103
104
105 /* Write the title on a chart*/
106 void  
107 chart_write_title(struct chart *chart, const char *title, ...)
108 {
109   va_list ap;
110   char buf[100];
111
112   if ( ! chart ) 
113           return ;
114
115   pl_savestate_r(chart->lp);
116   pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
117   pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
118
119   va_start(ap,title);
120   vsnprintf(buf,100,title,ap);
121   pl_alabel_r(chart->lp,0,0,buf);
122   va_end(ap);
123
124   pl_restorestate_r(chart->lp);
125 }
126
127
128 /* Set the scale for the abscissa */
129 void 
130 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
131 {
132   double x;
133
134   const double tick_interval = 
135     chart_rounded_tick( (max - min) / (double) ticks);
136
137   assert ( ch );
138
139
140   ch->x_max = ceil( max / tick_interval ) * tick_interval ; 
141   ch->x_min = floor ( min / tick_interval ) * tick_interval ;
142
143
144   ch->abscissa_scale = fabs(ch->data_right - ch->data_left) / 
145     fabs(ch->x_max - ch->x_min);
146
147   for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
148     {
149       draw_tick (ch, TICK_ABSCISSA, 
150                  (x - ch->x_min) * ch->abscissa_scale, "%g", x);
151     }
152
153 }
154
155
156 /* Set the scale for the ordinate */
157 void 
158 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
159 {
160   double y;
161
162   const double tick_interval = 
163     chart_rounded_tick( (smax - smin) / (double) ticks);
164
165   if ( !ch ) 
166           return;
167
168   ch->y_max = ceil  ( smax / tick_interval ) * tick_interval ; 
169   ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
170
171   ch->ordinate_scale = 
172     fabs(ch->data_top -  ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
173
174   for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
175     {
176     draw_tick (ch, TICK_ORDINATE, 
177                (y - ch->y_min) * ch->ordinate_scale, "%g", y);
178     }
179 }
180
181
182 /* Write the abscissa label */
183 void 
184 chart_write_xlabel(struct chart *ch, const char *label)
185 {
186   if ( ! ch ) 
187     return ;
188
189   pl_savestate_r(ch->lp);
190
191   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
192   pl_alabel_r(ch->lp,0,'t',label);
193
194   pl_restorestate_r(ch->lp);
195
196 }
197
198
199
200 /* Write the ordinate label */
201 void 
202 chart_write_ylabel(struct chart *ch, const char *label)
203 {
204   if ( ! ch ) 
205     return ;
206
207   pl_savestate_r(ch->lp);
208
209   pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
210   pl_textangle_r(ch->lp, 90);
211   pl_alabel_r(ch->lp, 0, 0, label);
212
213   pl_restorestate_r(ch->lp);
214 }