1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2009 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[N_CHART_COLOURS] =
56 /* Draw a tick mark at position
57 If label is non zero, then print it at the tick mark
60 draw_tick(struct chart *chart,
61 enum tick_orientation orientation,
63 const char *label, ...)
65 const int tickSize = 10;
69 pl_savestate_r(chart->lp);
71 pl_move_r(chart->lp, chart->data_left, chart->data_bottom);
73 if ( orientation == TICK_ABSCISSA )
74 pl_flinerel_r(chart->lp, position, 0, position, -tickSize);
75 else if (orientation == TICK_ORDINATE )
76 pl_flinerel_r(chart->lp, 0, position, -tickSize, position);
84 vsnprintf(buf,10,label,ap);
86 if ( orientation == TICK_ABSCISSA )
87 pl_alabel_r(chart->lp, 'c','t', buf);
88 else if (orientation == TICK_ORDINATE )
90 if ( fabs(position) < DBL_EPSILON )
91 pl_moverel_r(chart->lp, 0, 10);
93 pl_alabel_r(chart->lp, 'r','c', buf);
99 pl_restorestate_r(chart->lp);
103 /* Write the title on a chart*/
105 chart_write_title(struct chart *chart, const char *title, ...)
113 pl_savestate_r(chart->lp);
114 pl_ffontsize_r(chart->lp,chart->font_size * 1.5);
115 pl_move_r(chart->lp,chart->data_left, chart->title_bottom);
118 vsnprintf(buf,100,title,ap);
119 pl_alabel_r(chart->lp,0,0,buf);
122 pl_restorestate_r(chart->lp);
126 /* Set the scale for the abscissa */
128 chart_write_xscale(struct chart *ch, double min, double max, int ticks)
132 const double tick_interval =
133 chart_rounded_tick( (max - min) / (double) ticks);
138 ch->x_max = ceil( max / tick_interval ) * tick_interval ;
139 ch->x_min = floor ( min / tick_interval ) * tick_interval ;
142 ch->abscissa_scale = fabs(ch->data_right - ch->data_left) /
143 fabs(ch->x_max - ch->x_min);
145 for(x = ch->x_min ; x <= ch->x_max; x += tick_interval )
147 draw_tick (ch, TICK_ABSCISSA,
148 (x - ch->x_min) * ch->abscissa_scale, "%g", x);
154 /* Set the scale for the ordinate */
156 chart_write_yscale(struct chart *ch, double smin, double smax, int ticks)
160 const double tick_interval =
161 chart_rounded_tick( (smax - smin) / (double) ticks);
166 ch->y_max = ceil ( smax / tick_interval ) * tick_interval ;
167 ch->y_min = floor ( smin / tick_interval ) * tick_interval ;
170 fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ;
172 for(y = ch->y_min ; y <= ch->y_max; y += tick_interval )
174 draw_tick (ch, TICK_ORDINATE,
175 (y - ch->y_min) * ch->ordinate_scale, "%g", y);
180 /* Write the abscissa label */
182 chart_write_xlabel(struct chart *ch, const char *label)
187 pl_savestate_r(ch->lp);
189 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
190 pl_alabel_r(ch->lp,0,'t',label);
192 pl_restorestate_r(ch->lp);
198 /* Write the ordinate label */
200 chart_write_ylabel(struct chart *ch, const char *label)
205 pl_savestate_r(ch->lp);
207 pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right);
208 pl_textangle_r(ch->lp, 90);
209 pl_alabel_r(ch->lp, 0, 0, label);
211 pl_restorestate_r(ch->lp);
216 chart_write_legend (struct chart *ch)
219 const int vstep = ch->font_size * 2;
222 const int swatch = 20;
223 const int legend_top = ch->data_top;
224 const int legend_bottom = legend_top -
225 (vstep * ch->n_datasets + 2 * ypad );
230 pl_savestate_r (ch->lp);
232 pl_box_r (ch->lp, ch->legend_left, legend_top,
233 ch->legend_right - xpad, legend_bottom);
235 for (i = 0 ; i < ch->n_datasets ; ++i )
237 const int ypos = vstep * (i + 1);
238 const int xpos = ch->legend_left + xpad;
239 pl_move_r (ch->lp, xpos, legend_top - ypos);
241 pl_savestate_r(ch->lp);
242 pl_fillcolorname_r (ch->lp, data_colour [ i % N_CHART_COLOURS]);
244 pl_pentype_r (ch->lp, 1);
245 pl_filltype_r (ch->lp, 1);
246 pl_boxrel_r (ch->lp, 0, 0, swatch, swatch);
249 pl_moverel_r (ch->lp, swatch, 0);
250 pl_alabel_r (ch->lp, 0, 0, ch->dataset[i]);
252 pl_restorestate_r (ch->lp);
255 pl_restorestate_r (ch->lp);