abf4e70b7d2c4b76af55056453c21e3131eeb932
[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 <float.h>
26 #include <assert.h>
27 #include <math.h>
28
29 #include "chart.h"
30
31
32 const char *data_colour[] = {
33   "brown",
34   "red",
35   "orange",
36   "yellow",
37   "green",
38   "blue",
39   "violet",
40   "grey",
41   "pink"
42 };
43
44
45
46 int
47 chart_initialise(struct chart *chart)
48 {
49
50   chart->pl_params = pl_newplparams();
51
52   chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params);
53
54   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
55       return 1;
56
57   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
58   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
59   pl_pencolorname_r (chart->lp, "black"); 
60
61   pl_erase_r (chart->lp);               /* erase graphics display */
62   pl_filltype_r(chart->lp,0);
63
64
65
66   pl_savestate_r(chart->lp);
67
68   /* Set default chartetry */
69   chart->data_top =   900;
70   chart->data_right = 800;
71   chart->data_bottom = 120;
72   chart->data_left = 150;
73   chart->abscissa_top = 70;
74   chart->ordinate_right = 120;
75   chart->title_bottom = 920;
76   chart->legend_left = 810;
77   chart->legend_right = 1000;
78   chart->font_size = 0;
79   strcpy(chart->fill_colour,"red");
80
81
82   /* Get default font size */
83   if ( !chart->font_size) 
84     chart->font_size = pl_fontsize_r(chart->lp, -1);
85
86   /* Draw the data area */
87   pl_box_r(chart->lp, 
88            chart->data_left, chart->data_bottom, 
89            chart->data_right, chart->data_top);
90
91   return 0;
92
93 }
94
95
96
97 /* Draw a tick mark at position
98    If label is non zero, then print it at the tick mark
99 */
100 void
101 draw_tick(struct chart *chart, 
102           enum tick_orientation orientation, 
103           double position, 
104           const char *label, ...)
105 {
106   const int tickSize = 10;
107
108   pl_savestate_r(chart->lp);
109
110   pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
111
112   if ( orientation == TICK_ABSCISSA ) 
113     pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
114   else if (orientation == TICK_ORDINATE ) 
115       pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
116   else
117     assert(0);
118
119   if ( label ) {
120     char buf[10];
121     va_list ap;
122     va_start(ap,label);
123     vsnprintf(buf,10,label,ap);
124
125     if ( orientation == TICK_ABSCISSA ) 
126       pl_alabel_r(chart->lp, 'c','t', buf);
127     else if (orientation == TICK_ORDINATE ) 
128       {
129         if ( fabs(position) < DBL_EPSILON )
130             pl_moverel_r(chart->lp, 0, 10);
131
132         pl_alabel_r(chart->lp, 'r','c', buf);
133       }
134
135     va_end(ap);
136   }
137     
138   pl_restorestate_r(chart->lp);
139 }
140
141
142
143
144 void  
145 chart_write_title(struct chart *chart, const char *title)
146 {
147   /* Write the title */
148   pl_savestate_r(chart->lp);
149   pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
150   pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
151   pl_alabel_r(chart->lp,0,0,title);
152   pl_restorestate_r(chart->lp);
153 }
154
155
156
157 void
158 chart_finalise(struct chart *chart)
159 {
160   pl_restorestate_r(chart->lp);
161
162   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
163     {
164       fprintf (stderr, "Couldn't close Plotter\n");
165     }
166
167
168   pl_deletepl_r(chart->lp);
169
170   pl_deleteplparams(chart->pl_params);
171
172 }
173