3b4f1b377a12a624ceb929d00c617bb4bd581f30
[pspp-builds.git] / src / output / charts / plot-chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdio.h>
20 #include <plot.h>
21 #include <stdarg.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <float.h>
25 #include <assert.h>
26 #include <math.h>
27
28 #include <output/charts/plot-chart.h>
29
30 #include <math/chart-geometry.h>
31
32
33
34 #include <libpspp/str.h>
35 #include <libpspp/assertion.h>
36 #include <output/manager.h>
37 #include <output/output.h>
38
39 #include "xalloc.h"
40
41 const char *const data_colour[N_CHART_COLOURS] =
42   {
43     "brown",
44     "red",
45     "orange",
46     "yellow",
47     "green",
48     "blue",
49     "violet",
50     "grey",
51     "pink"
52   };
53
54
55
56 /* Draw a tick mark at position
57    If label is non zero, then print it at the tick mark
58 */
59 void
60 draw_tick(struct chart *chart,
61           enum tick_orientation orientation,
62           double position,
63           const char *label, ...)
64 {
65   const int tickSize = 10;
66
67   assert(chart);
68
69   pl_savestate_r(chart->lp);
70
71   pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
72
73   if ( orientation == TICK_ABSCISSA )
74     pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
75   else if (orientation == TICK_ORDINATE )
76       pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
77   else
78     NOT_REACHED ();
79
80   if ( label ) {
81     char buf[10];
82     va_list ap;
83     va_start(ap,label);
84     vsnprintf(buf,10,label,ap);
85
86     if ( orientation == TICK_ABSCISSA )
87       pl_alabel_r(chart->lp, 'c','t', buf);
88     else if (orientation == TICK_ORDINATE )
89       {
90         if ( fabs(position) < DBL_EPSILON )
91             pl_moverel_r(chart->lp, 0, 10);
92
93         pl_alabel_r(chart->lp, 'r','c', buf);
94       }
95
96     va_end(ap);
97   }
98
99   pl_restorestate_r(chart->lp);
100 }
101
102
103 /* Write the title on a chart*/
104 void
105 chart_write_title(struct chart *chart, const char *title, ...)
106 {
107   va_list ap;
108   char buf[100];
109
110   if ( ! chart )
111           return ;
112
113   pl_savestate_r(chart->lp);
114   pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
115   pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
116
117   va_start(ap,title);
118   vsnprintf(buf,100,title,ap);
119   pl_alabel_r(chart->lp,0,0,buf);
120   va_end(ap);
121
122   pl_restorestate_r(chart->lp);
123 }
124
125
126 /* Set the scale for the abscissa */
127 void
128 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
129 {
130   double x;
131
132   const double tick_interval =
133     chart_rounded_tick( (max - min) / (double) ticks);
134
135   assert ( ch );
136
137
138   ch->x_max = ceil( max / tick_interval ) * tick_interval ;
139   ch->x_min = floor ( min / tick_interval ) * tick_interval ;
140
141
142   ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
143     fabs(ch->x_max - ch->x_min);
144
145   for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
146     {
147       draw_tick (ch, TICK_ABSCISSA,
148                  (x - ch->x_min) * ch->abscissa_scale, "%g", x);
149     }
150
151 }
152
153
154 /* Set the scale for the ordinate */
155 void
156 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
157 {
158   double y;
159
160   const double tick_interval =
161     chart_rounded_tick( (smax - smin) / (double) ticks);
162
163   if ( !ch )
164           return;
165
166   ch->y_max = ceil  ( smax / tick_interval ) * tick_interval ;
167   ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
168
169   ch->ordinate_scale =
170     fabs(ch->data_top -  ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
171
172   for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
173     {
174     draw_tick (ch, TICK_ORDINATE,
175                (y - ch->y_min) * ch->ordinate_scale, "%g", y);
176     }
177 }
178
179
180 /* Write the abscissa label */
181 void
182 chart_write_xlabel(struct chart *ch, const char *label)
183 {
184   if ( ! ch )
185     return ;
186
187   pl_savestate_r(ch->lp);
188
189   pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
190   pl_alabel_r(ch->lp,0,'t',label);
191
192   pl_restorestate_r(ch->lp);
193
194 }
195
196
197
198 /* Write the ordinate label */
199 void
200 chart_write_ylabel(struct chart *ch, const char *label)
201 {
202   if ( ! ch )
203     return ;
204
205   pl_savestate_r(ch->lp);
206
207   pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
208   pl_textangle_r(ch->lp, 90);
209   pl_alabel_r(ch->lp, 0, 0, label);
210
211   pl_restorestate_r(ch->lp);
212 }