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