Added npplot and detrended np plots
[pspp-builds.git] / src / 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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20
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.h"
31
32
33 const char *data_colour[] = {
34   "brown",
35   "red",
36   "orange",
37   "yellow",
38   "green",
39   "blue",
40   "violet",
41   "grey",
42   "pink"
43 };
44
45
46
47 int
48 chart_initialise(struct chart *chart)
49 {
50
51   chart->pl_params = pl_newplparams();
52
53   chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params);
54
55   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
56       return 1;
57
58   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
59   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
60   pl_pencolorname_r (chart->lp, "black"); 
61
62   pl_erase_r (chart->lp);               /* erase graphics display */
63   pl_filltype_r(chart->lp,0);
64
65
66
67   pl_savestate_r(chart->lp);
68
69   /* Set default chartetry */
70   chart->data_top =   900;
71   chart->data_right = 800;
72   chart->data_bottom = 120;
73   chart->data_left = 150;
74   chart->abscissa_top = 70;
75   chart->ordinate_right = 120;
76   chart->title_bottom = 920;
77   chart->legend_left = 810;
78   chart->legend_right = 1000;
79   chart->font_size = 0;
80   strcpy(chart->fill_colour,"red");
81
82
83   /* Get default font size */
84   if ( !chart->font_size) 
85     chart->font_size = pl_fontsize_r(chart->lp, -1);
86
87   /* Draw the data area */
88   pl_box_r(chart->lp, 
89            chart->data_left, chart->data_bottom, 
90            chart->data_right, chart->data_top);
91
92   return 0;
93
94 }
95
96
97
98 /* Draw a tick mark at position
99    If label is non zero, then print it at the tick mark
100 */
101 void
102 draw_tick(struct chart *chart, 
103           enum tick_orientation orientation, 
104           double position, 
105           const char *label, ...)
106 {
107   const int tickSize = 10;
108
109   pl_savestate_r(chart->lp);
110
111   pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
112
113   if ( orientation == TICK_ABSCISSA ) 
114     pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
115   else if (orientation == TICK_ORDINATE ) 
116       pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
117   else
118     assert(0);
119
120   if ( label ) {
121     char buf[10];
122     va_list ap;
123     va_start(ap,label);
124     vsnprintf(buf,10,label,ap);
125
126     if ( orientation == TICK_ABSCISSA ) 
127       pl_alabel_r(chart->lp, 'c','t', buf);
128     else if (orientation == TICK_ORDINATE ) 
129       {
130         if ( fabs(position) < DBL_EPSILON )
131             pl_moverel_r(chart->lp, 0, 10);
132
133         pl_alabel_r(chart->lp, 'r','c', buf);
134       }
135
136     va_end(ap);
137   }
138     
139   pl_restorestate_r(chart->lp);
140 }
141
142
143
144
145 /* Write the title on a chart*/
146 void  
147 chart_write_title(struct chart *chart, const char *title, ...)
148 {
149   va_list ap;
150   char buf[100];
151
152   pl_savestate_r(chart->lp);
153   pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
154   pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
155
156   va_start(ap,title);
157   vsnprintf(buf,100,title,ap);
158   pl_alabel_r(chart->lp,0,0,buf);
159   va_end(ap);
160
161   pl_restorestate_r(chart->lp);
162 }
163
164
165
166 void
167 chart_finalise(struct chart *chart)
168 {
169   pl_restorestate_r(chart->lp);
170
171   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
172     {
173       fprintf (stderr, "Couldn't close Plotter\n");
174     }
175
176
177   pl_deletepl_r(chart->lp);
178
179   pl_deleteplparams(chart->pl_params);
180
181 }
182
183
184
185   
186 /* Adjust tick to be a sensible value 
187    ie:  ... 0.1,0.2,0.5,   1,2,5,  10,20,50 ... */
188 double
189 chart_rounded_tick(double tick)
190 {
191
192   int i;
193
194   double diff = DBL_MAX;
195   double t = tick;
196     
197   static const double standard_ticks[] = {1, 2, 5, 10};
198
199   const double factor = pow(10,ceil(log10(standard_ticks[0] / tick))) ;
200
201   for (i = 3  ; i >= 0 ; --i) 
202     {
203       const double d = fabs( tick - standard_ticks[i] / factor ) ;
204
205       if ( d < diff ) 
206         {
207           diff = d;
208           t = standard_ticks[i] / factor ;
209         }
210     }
211
212   return t;
213     
214 }
215