1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 #include <output/charts/plot-chart.h>
32 #include <math/chart-geometry.h>
36 #include <libpspp/str.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/assertion.h>
39 #include <output/manager.h>
40 #include <output/output.h>
43 const char *const data_colour[] = {
57 /* Draw a tick mark at position
58 If label is non zero, then print it at the tick mark
61 draw_tick(struct chart *chart,
62 enum tick_orientation orientation,
64 const char *label, ...)
66 const int tickSize = 10;
70 pl_savestate_r(chart->lp);
72 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
74 if ( orientation == TICK_ABSCISSA )
75 pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
76 else if (orientation == TICK_ORDINATE )
77 pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
85 vsnprintf(buf,10,label,ap);
87 if ( orientation == TICK_ABSCISSA )
88 pl_alabel_r(chart->lp, 'c','t', buf);
89 else if (orientation == TICK_ORDINATE )
91 if ( fabs(position) < DBL_EPSILON )
92 pl_moverel_r(chart->lp, 0, 10);
94 pl_alabel_r(chart->lp, 'r','c', buf);
100 pl_restorestate_r(chart->lp);
104 /* Write the title on a chart*/
106 chart_write_title(struct chart *chart, const char *title, ...)
114 pl_savestate_r(chart->lp);
115 pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
116 pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
119 vsnprintf(buf,100,title,ap);
120 pl_alabel_r(chart->lp,0,0,buf);
123 pl_restorestate_r(chart->lp);
127 /* Set the scale for the abscissa */
129 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
133 const double tick_interval =
134 chart_rounded_tick( (max - min) / (double) ticks);
139 ch->x_max = ceil( max / tick_interval ) * tick_interval ;
140 ch->x_min = floor ( min / tick_interval ) * tick_interval ;
143 ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
144 fabs(ch->x_max - ch->x_min);
146 for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
148 draw_tick (ch, TICK_ABSCISSA,
149 (x - ch->x_min) * ch->abscissa_scale, "%g", x);
155 /* Set the scale for the ordinate */
157 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
161 const double tick_interval =
162 chart_rounded_tick( (smax - smin) / (double) ticks);
167 ch->y_max = ceil ( smax / tick_interval ) * tick_interval ;
168 ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
171 fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
173 for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
175 draw_tick (ch, TICK_ORDINATE,
176 (y - ch->y_min) * ch->ordinate_scale, "%g", y);
181 /* Write the abscissa label */
183 chart_write_xlabel(struct chart *ch, const char *label)
188 pl_savestate_r(ch->lp);
190 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
191 pl_alabel_r(ch->lp,0,'t',label);
193 pl_restorestate_r(ch->lp);
199 /* Write the ordinate label */
201 chart_write_ylabel(struct chart *ch, const char *label)
206 pl_savestate_r(ch->lp);
208 pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
209 pl_textangle_r(ch->lp, 90);
210 pl_alabel_r(ch->lp, 0, 0, label);
212 pl_restorestate_r(ch->lp);