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