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
26 #include <output/charts/barchart.h>
27 #include <output/chart.h>
28 #include <output/charts/plot-chart.h>
31 #define SUB_CATAGORIES 3
33 static const double x_min = 0;
34 static const double x_max = 15.0;
36 static const char *cat_labels[] =
55 static const double data1[] =
69 static const double data2[] =
81 static const double data3[] =
84 0, 45,23, 9, 40, 24,4, 8
88 static const char subcat_name[]="Gender";
96 static const struct subcat sub_catagory[SUB_CATAGORIES] =
105 static const double y_min = 0;
106 static const double y_max = 120.0;
107 static const double y_tick = 20.0;
111 static void write_legend(struct chart *chart) ;
115 draw_barchart(struct chart *ch, const char *title,
116 const char *xlabel, const char *ylabel, enum bar_opts opt)
121 double interval_size = fabs(ch->data_right - ch->data_left) / ( CATAGORIES );
123 double bar_width = interval_size / 1.1 ;
125 double ordinate_scale = fabs(ch->data_top - ch->data_bottom) /
126 fabs(y_max - y_min) ;
128 if ( opt != BAR_STACKED )
129 bar_width /= SUB_CATAGORIES;
131 /* Move to data bottom-left */
132 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
134 pl_savestate_r(ch->lp);
135 pl_filltype_r(ch->lp,1);
138 for (i = 0 ; i < CATAGORIES ; ++i )
142 double x = i * interval_size;
144 pl_savestate_r(ch->lp);
146 draw_tick (ch, TICK_ABSCISSA, x + (interval_size/2 ),
149 for(sc = 0 ; sc < SUB_CATAGORIES ; ++sc )
152 pl_savestate_r(ch->lp);
153 pl_fillcolorname_r(ch->lp,data_colour[sc]);
159 x + (sc * bar_width ), 0,
160 x + (sc + 1) * bar_width,
161 sub_catagory[sc].data[i] * ordinate_scale );
170 ystart + sub_catagory[sc].data[i] * ordinate_scale );
172 ystart += sub_catagory[sc].data[i] * ordinate_scale ;
179 pl_restorestate_r(ch->lp);
182 pl_restorestate_r(ch->lp);
184 pl_restorestate_r(ch->lp);
186 for ( d = y_min; d <= y_max ; d += y_tick )
189 draw_tick (ch, TICK_ORDINATE,
190 (d - y_min ) * ordinate_scale, "%g", d);
194 /* Write the abscissa label */
195 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
196 pl_alabel_r(ch->lp,0,'t',xlabel);
199 /* Write the ordinate label */
200 pl_savestate_r(ch->lp);
201 pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right);
202 pl_textangle_r(ch->lp,90);
203 pl_alabel_r(ch->lp,0,0,ylabel);
204 pl_restorestate_r(ch->lp);
207 chart_write_title(ch, title);
219 write_legend(struct chart *chart)
223 pl_savestate_r(chart->lp);
225 pl_filltype_r(chart->lp,1);
227 pl_move_r(chart->lp, chart->legend_left,
228 chart->data_bottom + chart->font_size * SUB_CATAGORIES * 1.5);
230 pl_alabel_r(chart->lp,0,'b',subcat_name);
232 for (sc = 0 ; sc < SUB_CATAGORIES ; ++sc )
234 pl_fmove_r(chart->lp,
236 chart->data_bottom + chart->font_size * sc * 1.5);
238 pl_savestate_r(chart->lp);
239 pl_fillcolorname_r(chart->lp,data_colour[sc]);
240 pl_fboxrel_r (chart->lp,
242 chart->font_size, chart->font_size);
243 pl_restorestate_r(chart->lp);
245 pl_fmove_r(chart->lp,
246 chart->legend_left + chart->font_size * 1.5,
247 chart->data_bottom + chart->font_size * sc * 1.5);
249 pl_alabel_r(chart->lp,'l','b',sub_catagory[sc].label);
253 pl_restorestate_r(chart->lp);