1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by John Darrington <john@darrington.wattle.id.au>
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.
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.
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
34 const char *data_colour[] = {
49 chart_initialise(struct chart *chart)
52 chart->pl_params = pl_newplparams();
54 chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params);
56 if (pl_openpl_r (chart->lp) < 0) /* open Plotter */
59 pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
60 pl_flinewidth_r (chart->lp, 0.25); /* set line thickness */
61 pl_pencolorname_r (chart->lp, "black");
63 pl_erase_r (chart->lp); /* erase graphics display */
64 pl_filltype_r(chart->lp,0);
68 pl_savestate_r(chart->lp);
70 /* Set default chartetry */
71 chart->data_top = 900;
72 chart->data_right = 800;
73 chart->data_bottom = 120;
74 chart->data_left = 150;
75 chart->abscissa_top = 70;
76 chart->ordinate_right = 120;
77 chart->title_bottom = 920;
78 chart->legend_left = 810;
79 chart->legend_right = 1000;
81 strcpy(chart->fill_colour,"red");
84 /* Get default font size */
85 if ( !chart->font_size)
86 chart->font_size = pl_fontsize_r(chart->lp, -1);
88 /* Draw the data area */
90 chart->data_left, chart->data_bottom,
91 chart->data_right, chart->data_top);
99 /* Draw a tick mark at position
100 If label is non zero, then print it at the tick mark
103 draw_tick(struct chart *chart,
104 enum tick_orientation orientation,
106 const char *label, ...)
108 const int tickSize = 10;
110 pl_savestate_r(chart->lp);
112 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
114 if ( orientation == TICK_ABSCISSA )
115 pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
116 else if (orientation == TICK_ORDINATE )
117 pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
125 vsnprintf(buf,10,label,ap);
127 if ( orientation == TICK_ABSCISSA )
128 pl_alabel_r(chart->lp, 'c','t', buf);
129 else if (orientation == TICK_ORDINATE )
131 if ( fabs(position) < DBL_EPSILON )
132 pl_moverel_r(chart->lp, 0, 10);
134 pl_alabel_r(chart->lp, 'r','c', buf);
140 pl_restorestate_r(chart->lp);
146 /* Write the title on a chart*/
148 chart_write_title(struct chart *chart, const char *title, ...)
153 pl_savestate_r(chart->lp);
154 pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
155 pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
158 vsnprintf(buf,100,title,ap);
159 pl_alabel_r(chart->lp,0,0,buf);
162 pl_restorestate_r(chart->lp);
168 chart_finalise(struct chart *chart)
170 pl_restorestate_r(chart->lp);
172 if (pl_closepl_r (chart->lp) < 0) /* close Plotter */
174 fprintf (stderr, "Couldn't close Plotter\n");
178 pl_deletepl_r(chart->lp);
180 pl_deleteplparams(chart->pl_params);
186 /* Adjust tick to be a sensible value
187 ie: ... 0.1,0.2,0.5, 1,2,5, 10,20,50 ... */
189 chart_rounded_tick(double tick)
194 double diff = DBL_MAX;
197 static const double standard_ticks[] = {1, 2, 5, 10};
199 const double factor = pow(10,ceil(log10(standard_ticks[0] / tick))) ;
201 for (i = 3 ; i >= 0 ; --i)
203 const double d = fabs( tick - standard_ticks[i] / factor ) ;
208 t = standard_ticks[i] / factor ;
217 /* Set the scale for the abscissa */
219 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
223 const double tick_interval =
224 chart_rounded_tick( (max - min) / (double) ticks);
226 ch->x_max = ceil( max / tick_interval ) * tick_interval ;
227 ch->x_min = floor ( min / tick_interval ) * tick_interval ;
229 ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
230 fabs(ch->x_max - ch->x_min);
232 for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
234 draw_tick (ch, TICK_ABSCISSA,
235 (x - ch->x_min) * ch->abscissa_scale, "%g", x);
241 /* Set the scale for the ordinate */
243 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
247 const double tick_interval =
248 chart_rounded_tick( (smax - smin) / (double) ticks);
250 ch->y_max = ceil ( smax / tick_interval ) * tick_interval ;
251 ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
254 fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
256 for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
258 draw_tick (ch, TICK_ORDINATE,
259 (y - ch->y_min) * ch->ordinate_scale, "%g", y);