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
33 const char *data_colour[] = {
48 chart_initialise(struct chart *chart)
51 chart->pl_params = pl_newplparams();
53 chart->lp = pl_newpl_r ("X",0,stdout,stderr,chart->pl_params);
55 if (pl_openpl_r (chart->lp) < 0) /* open Plotter */
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");
62 pl_erase_r (chart->lp); /* erase graphics display */
63 pl_filltype_r(chart->lp,0);
67 pl_savestate_r(chart->lp);
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;
80 strcpy(chart->fill_colour,"red");
83 /* Get default font size */
84 if ( !chart->font_size)
85 chart->font_size = pl_fontsize_r(chart->lp, -1);
87 /* Draw the data area */
89 chart->data_left, chart->data_bottom,
90 chart->data_right, chart->data_top);
98 /* Draw a tick mark at position
99 If label is non zero, then print it at the tick mark
102 draw_tick(struct chart *chart,
103 enum tick_orientation orientation,
105 const char *label, ...)
107 const int tickSize = 10;
109 pl_savestate_r(chart->lp);
111 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
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);
124 vsnprintf(buf,10,label,ap);
126 if ( orientation == TICK_ABSCISSA )
127 pl_alabel_r(chart->lp, 'c','t', buf);
128 else if (orientation == TICK_ORDINATE )
130 if ( fabs(position) < DBL_EPSILON )
131 pl_moverel_r(chart->lp, 0, 10);
133 pl_alabel_r(chart->lp, 'r','c', buf);
139 pl_restorestate_r(chart->lp);
145 /* Write the title on a chart*/
147 chart_write_title(struct chart *chart, const char *title, ...)
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);
157 vsnprintf(buf,100,title,ap);
158 pl_alabel_r(chart->lp,0,0,buf);
161 pl_restorestate_r(chart->lp);
167 chart_finalise(struct chart *chart)
169 pl_restorestate_r(chart->lp);
171 if (pl_closepl_r (chart->lp) < 0) /* close Plotter */
173 fprintf (stderr, "Couldn't close Plotter\n");
177 pl_deletepl_r(chart->lp);
179 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 ;