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/>. */
24 #include <output/charts/barchart.h>
25 #include <output/chart.h>
26 #include <output/charts/plot-chart.h>
29 #define SUB_CATAGORIES 3
31 static const double x_min = 0;
32 static const double x_max = 15.0;
34 static const char *cat_labels[] =
53 static const double data1[] =
67 static const double data2[] =
79 static const double data3[] =
82 0, 45,23, 9, 40, 24,4, 8
86 static const char subcat_name[]="Gender";
94 static const struct subcat sub_catagory[SUB_CATAGORIES] =
103 static const double y_min = 0;
104 static const double y_max = 120.0;
105 static const double y_tick = 20.0;
109 static void write_legend(struct chart *chart) ;
113 draw_barchart(struct chart *ch, const char *title,
114 const char *xlabel, const char *ylabel, enum bar_opts opt)
119 double interval_size = fabs(ch->data_right - ch->data_left) / ( CATAGORIES );
121 double bar_width = interval_size / 1.1 ;
123 double ordinate_scale = fabs(ch->data_top - ch->data_bottom) /
124 fabs(y_max - y_min) ;
126 if ( opt != BAR_STACKED )
127 bar_width /= SUB_CATAGORIES;
129 /* Move to data bottom-left */
130 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
132 pl_savestate_r(ch->lp);
133 pl_filltype_r(ch->lp,1);
136 for (i = 0 ; i < CATAGORIES ; ++i )
140 double x = i * interval_size;
142 pl_savestate_r(ch->lp);
144 draw_tick (ch, TICK_ABSCISSA, x + (interval_size/2 ),
147 for(sc = 0 ; sc < SUB_CATAGORIES ; ++sc )
150 pl_savestate_r(ch->lp);
151 pl_fillcolorname_r(ch->lp,data_colour[sc % N_CHART_COLOURS]);
157 x + (sc * bar_width ), 0,
158 x + (sc + 1) * bar_width,
159 sub_catagory[sc].data[i] * ordinate_scale );
168 ystart + sub_catagory[sc].data[i] * ordinate_scale );
170 ystart += sub_catagory[sc].data[i] * ordinate_scale ;
177 pl_restorestate_r(ch->lp);
180 pl_restorestate_r(ch->lp);
182 pl_restorestate_r(ch->lp);
184 for ( d = y_min; d <= y_max ; d += y_tick )
187 draw_tick (ch, TICK_ORDINATE,
188 (d - y_min ) * ordinate_scale, "%g", d);
192 /* Write the abscissa label */
193 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
194 pl_alabel_r(ch->lp,0,'t',xlabel);
197 /* Write the ordinate label */
198 pl_savestate_r(ch->lp);
199 pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right);
200 pl_textangle_r(ch->lp,90);
201 pl_alabel_r(ch->lp,0,0,ylabel);
202 pl_restorestate_r(ch->lp);
205 chart_write_title(ch, title);
217 write_legend(struct chart *chart)
221 pl_savestate_r(chart->lp);
223 pl_filltype_r(chart->lp,1);
225 pl_move_r(chart->lp, chart->legend_left,
226 chart->data_bottom + chart->font_size * SUB_CATAGORIES * 1.5);
228 pl_alabel_r(chart->lp,0,'b',subcat_name);
230 for (sc = 0 ; sc < SUB_CATAGORIES ; ++sc )
232 pl_fmove_r(chart->lp,
234 chart->data_bottom + chart->font_size * sc * 1.5);
236 pl_savestate_r(chart->lp);
237 pl_fillcolorname_r(chart->lp,data_colour[sc]);
238 pl_fboxrel_r (chart->lp,
240 chart->font_size, chart->font_size);
241 pl_restorestate_r(chart->lp);
243 pl_fmove_r(chart->lp,
244 chart->legend_left + chart->font_size * 1.5,
245 chart->data_bottom + chart->font_size * sc * 1.5);
247 pl_alabel_r(chart->lp,'l','b',sub_catagory[sc].label);
251 pl_restorestate_r(chart->lp);