1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
28 #include <output/charts/plot-chart.h>
30 #include <math/chart-geometry.h>
34 #include <libpspp/str.h>
35 #include <libpspp/assertion.h>
36 #include <output/manager.h>
37 #include <output/output.h>
41 const char *const data_colour[] = {
55 /* Draw a tick mark at position
56 If label is non zero, then print it at the tick mark
59 draw_tick(struct chart *chart,
60 enum tick_orientation orientation,
62 const char *label, ...)
64 const int tickSize = 10;
68 pl_savestate_r(chart->lp);
70 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
72 if ( orientation == TICK_ABSCISSA )
73 pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
74 else if (orientation == TICK_ORDINATE )
75 pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
83 vsnprintf(buf,10,label,ap);
85 if ( orientation == TICK_ABSCISSA )
86 pl_alabel_r(chart->lp, 'c','t', buf);
87 else if (orientation == TICK_ORDINATE )
89 if ( fabs(position) < DBL_EPSILON )
90 pl_moverel_r(chart->lp, 0, 10);
92 pl_alabel_r(chart->lp, 'r','c', buf);
98 pl_restorestate_r(chart->lp);
102 /* Write the title on a chart*/
104 chart_write_title(struct chart *chart, const char *title, ...)
112 pl_savestate_r(chart->lp);
113 pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
114 pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
117 vsnprintf(buf,100,title,ap);
118 pl_alabel_r(chart->lp,0,0,buf);
121 pl_restorestate_r(chart->lp);
125 /* Set the scale for the abscissa */
127 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
131 const double tick_interval =
132 chart_rounded_tick( (max - min) / (double) ticks);
137 ch->x_max = ceil( max / tick_interval ) * tick_interval ;
138 ch->x_min = floor ( min / tick_interval ) * tick_interval ;
141 ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
142 fabs(ch->x_max - ch->x_min);
144 for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
146 draw_tick (ch, TICK_ABSCISSA,
147 (x - ch->x_min) * ch->abscissa_scale, "%g", x);
153 /* Set the scale for the ordinate */
155 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
159 const double tick_interval =
160 chart_rounded_tick( (smax - smin) / (double) ticks);
165 ch->y_max = ceil ( smax / tick_interval ) * tick_interval ;
166 ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
169 fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
171 for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
173 draw_tick (ch, TICK_ORDINATE,
174 (y - ch->y_min) * ch->ordinate_scale, "%g", y);
179 /* Write the abscissa label */
181 chart_write_xlabel(struct chart *ch, const char *label)
186 pl_savestate_r(ch->lp);
188 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
189 pl_alabel_r(ch->lp,0,'t',label);
191 pl_restorestate_r(ch->lp);
197 /* Write the ordinate label */
199 chart_write_ylabel(struct chart *ch, const char *label)
204 pl_savestate_r(ch->lp);
206 pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
207 pl_textangle_r(ch->lp, 90);
208 pl_alabel_r(ch->lp, 0, 0, label);
210 pl_restorestate_r(ch->lp);