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., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include <output/charts/plot-chart.h>
33 #include <math/chart-geometry.h>
37 #include <libpspp/str.h>
38 #include <libpspp/alloc.h>
39 #include <libpspp/assertion.h>
40 #include <output/manager.h>
41 #include <output/output.h>
44 const char *const data_colour[] = {
58 /* Draw a tick mark at position
59 If label is non zero, then print it at the tick mark
62 draw_tick(struct chart *chart,
63 enum tick_orientation orientation,
65 const char *label, ...)
67 const int tickSize = 10;
71 pl_savestate_r(chart->lp);
73 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
75 if ( orientation == TICK_ABSCISSA )
76 pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
77 else if (orientation == TICK_ORDINATE )
78 pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
86 vsnprintf(buf,10,label,ap);
88 if ( orientation == TICK_ABSCISSA )
89 pl_alabel_r(chart->lp, 'c','t', buf);
90 else if (orientation == TICK_ORDINATE )
92 if ( fabs(position) < DBL_EPSILON )
93 pl_moverel_r(chart->lp, 0, 10);
95 pl_alabel_r(chart->lp, 'r','c', buf);
101 pl_restorestate_r(chart->lp);
105 /* Write the title on a chart*/
107 chart_write_title(struct chart *chart, const char *title, ...)
115 pl_savestate_r(chart->lp);
116 pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
117 pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
120 vsnprintf(buf,100,title,ap);
121 pl_alabel_r(chart->lp,0,0,buf);
124 pl_restorestate_r(chart->lp);
128 /* Set the scale for the abscissa */
130 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
134 const double tick_interval =
135 chart_rounded_tick( (max - min) / (double) ticks);
140 ch->x_max = ceil( max / tick_interval ) * tick_interval ;
141 ch->x_min = floor ( min / tick_interval ) * tick_interval ;
144 ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
145 fabs(ch->x_max - ch->x_min);
147 for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
149 draw_tick (ch, TICK_ABSCISSA,
150 (x - ch->x_min) * ch->abscissa_scale, "%g", x);
156 /* Set the scale for the ordinate */
158 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
162 const double tick_interval =
163 chart_rounded_tick( (smax - smin) / (double) ticks);
168 ch->y_max = ceil ( smax / tick_interval ) * tick_interval ;
169 ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
172 fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
174 for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
176 draw_tick (ch, TICK_ORDINATE,
177 (y - ch->y_min) * ch->ordinate_scale, "%g", y);
182 /* Write the abscissa label */
184 chart_write_xlabel(struct chart *ch, const char *label)
189 pl_savestate_r(ch->lp);
191 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
192 pl_alabel_r(ch->lp,0,'t',label);
194 pl_restorestate_r(ch->lp);
200 /* Write the ordinate label */
202 chart_write_ylabel(struct chart *ch, const char *label)
207 pl_savestate_r(ch->lp);
209 pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
210 pl_textangle_r(ch->lp, 90);
211 pl_alabel_r(ch->lp, 0, 0, label);
213 pl_restorestate_r(ch->lp);