X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Foutput%2Fcharts%2Fplot-chart.c;h=cda6d1ec83ce5f431d6e75f93e3a607005308bc3;hb=34cf34e01daa543838eebd89c9879472ef21ac24;hp=8a1dc0bd1fbed3f5f3943dc41adb69935b7fce8f;hpb=97d4f38945476834fd7fce612b663f19f2b291f8;p=pspp-builds.git diff --git a/src/output/charts/plot-chart.c b/src/output/charts/plot-chart.c index 8a1dc0bd..cda6d1ec 100644 --- a/src/output/charts/plot-chart.c +++ b/src/output/charts/plot-chart.c @@ -1,214 +1,356 @@ -/* PSPP - computes sample statistics. - Copyright (C) 2004 Free Software Foundation, Inc. - Written by John Darrington +/* PSPP - a program for statistical analysis. + Copyright (C) 2004, 2009 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include -#include -#include -#include -#include -#include -#include +#include + #include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include -#include - +#include +#include #include +#include +#include +#include +#include "xalloc.h" +#if ! PANGO_VERSION_CHECK (2, 22, 0) +int pango_layout_get_baseline (PangoLayout *layout); + +/* Shamelessly copied from the pango source */ +int +pango_layout_get_baseline (PangoLayout *layout) +{ + int baseline; + + /* XXX this is so inefficient */ + PangoLayoutIter *iter = pango_layout_get_iter (layout); + baseline = pango_layout_iter_get_baseline (iter); + pango_layout_iter_free (iter); + + return baseline; +} +#endif -#include -#include -#include -#include -#include -const char *data_colour[] = { - "brown", - "red", - "orange", - "yellow", - "green", - "blue", - "violet", - "grey", - "pink" -}; +const struct chart_colour data_colour[N_CHART_COLOURS] = + { + { 165, 42, 42 }, /* brown */ + { 255, 0, 0 }, /* red */ + { 255, 165, 0 }, /* orange */ + { 255, 255, 0 }, /* yellow */ + { 0, 255, 0 }, /* green */ + { 0, 0, 255 }, /* blue */ + { 238, 130, 238 }, /* violet */ + { 190, 190, 190 }, /* grey */ + { 255, 192, 203 }, /* pink */ + }; + +void +chart_draw_marker (cairo_t *cr, double x, double y, enum marker_type marker, + double size) +{ + cairo_save (cr); + cairo_translate (cr, x, y); + cairo_scale (cr, size / 2.0, size / 2.0); + cairo_set_line_width (cr, cairo_get_line_width (cr) / (size / 2.0)); + switch (marker) + { + case MARKER_CIRCLE: + cairo_arc (cr, 0, 0, 1.0, 0, 2 * M_PI); + cairo_stroke (cr); + break; + + case MARKER_ASTERISK: + cairo_move_to (cr, 0, -1.0); /* | */ + cairo_line_to (cr, 0, 1.0); + cairo_move_to (cr, -M_SQRT1_2, -M_SQRT1_2); /* / */ + cairo_line_to (cr, M_SQRT1_2, M_SQRT1_2); + cairo_move_to (cr, -M_SQRT1_2, M_SQRT1_2); /* \ */ + cairo_line_to (cr, M_SQRT1_2, -M_SQRT1_2); + cairo_stroke (cr); + break; + + case MARKER_SQUARE: + cairo_rectangle (cr, -1.0, -1.0, 2.0, 2.0); + cairo_stroke (cr); + break; + } + cairo_restore (cr); +} + +void +chart_label (cairo_t *cr, int horz_justify, int vert_justify, double font_size, + const char *string) +{ + PangoFontDescription *desc; + PangoLayout *layout; + double x, y; + + desc = pango_font_description_from_string ("sans serif"); + if (desc == NULL) + { + cairo_new_path (cr); + return; + } + pango_font_description_set_absolute_size (desc, font_size * PANGO_SCALE); + + cairo_save (cr); + cairo_get_current_point (cr, &x, &y); + cairo_translate (cr, x, y); + cairo_move_to (cr, 0, 0); + cairo_scale (cr, 1.0, -1.0); + + layout = pango_cairo_create_layout (cr); + pango_layout_set_font_description (layout, desc); + pango_layout_set_text (layout, string, -1); + if (horz_justify != 'l') + { + int width_pango; + double width; + + pango_layout_get_size (layout, &width_pango, NULL); + width = (double) width_pango / PANGO_SCALE; + if (horz_justify == 'r') + cairo_rel_move_to (cr, -width, 0); + else + cairo_rel_move_to (cr, -width / 2.0, 0); + } + if (vert_justify == 'x') + { + int baseline_pango = pango_layout_get_baseline (layout); + double baseline = (double) baseline_pango / PANGO_SCALE; + cairo_rel_move_to (cr, 0, -baseline); + } + else if (vert_justify != 't') + { + int height_pango; + double height; + + pango_layout_get_size (layout, NULL, &height_pango); + height = (double) height_pango / PANGO_SCALE; + if (vert_justify == 'b') + cairo_rel_move_to (cr, 0, -height); + else if (vert_justify == 'c') + cairo_rel_move_to (cr, 0, -height / 2.0); + } + pango_cairo_show_layout (cr, layout); + g_object_unref (layout); + + cairo_restore (cr); + cairo_new_path (cr); + pango_font_description_free (desc); +} /* Draw a tick mark at position If label is non zero, then print it at the tick mark */ void -draw_tick(struct chart *chart, - enum tick_orientation orientation, - double position, - const char *label, ...) +draw_tick (cairo_t *cr, const struct chart_geometry *geom, + enum tick_orientation orientation, + double position, + const char *label, ...) { const int tickSize = 10; + double x, y; - assert(chart); - - pl_savestate_r(chart->lp); + cairo_move_to (cr, geom->data_left, geom->data_bottom); - pl_move_r(chart->lp, chart->data_left, chart->data_bottom); - - if ( orientation == TICK_ABSCISSA ) - pl_flinerel_r(chart->lp, position, 0, position, -tickSize); - else if (orientation == TICK_ORDINATE ) - pl_flinerel_r(chart->lp, 0, position, -tickSize, position); + if (orientation == TICK_ABSCISSA) + { + cairo_rel_move_to (cr, position, 0); + cairo_rel_line_to (cr, 0, -tickSize); + } + else if (orientation == TICK_ORDINATE) + { + cairo_rel_move_to (cr, 0, position); + cairo_rel_line_to (cr, -tickSize, 0); + } else NOT_REACHED (); + cairo_get_current_point (cr, &x, &y); - if ( label ) { - char buf[10]; - va_list ap; - va_start(ap,label); - vsnprintf(buf,10,label,ap); - - if ( orientation == TICK_ABSCISSA ) - pl_alabel_r(chart->lp, 'c','t', buf); - else if (orientation == TICK_ORDINATE ) - { - if ( fabs(position) < DBL_EPSILON ) - pl_moverel_r(chart->lp, 0, 10); - - pl_alabel_r(chart->lp, 'r','c', buf); - } - - va_end(ap); - } - - pl_restorestate_r(chart->lp); + cairo_stroke (cr); + + if (label != NULL) + { + va_list ap; + char *s; + + cairo_move_to (cr, x, y); + + va_start (ap, label); + s = xvasprintf (label, ap); + if (orientation == TICK_ABSCISSA) + chart_label (cr, 'c', 't', geom->font_size, s); + else if (orientation == TICK_ORDINATE) + { + if (fabs (position) < DBL_EPSILON) + cairo_rel_move_to (cr, 0, 10); + chart_label (cr, 'r', 'c', geom->font_size, s); + } + free (s); + va_end (ap); + } } /* Write the title on a chart*/ -void -chart_write_title(struct chart *chart, const char *title, ...) +void +chart_write_title (cairo_t *cr, const struct chart_geometry *geom, + const char *title, ...) { va_list ap; - char buf[100]; - - if ( ! chart ) - return ; + char *s; - pl_savestate_r(chart->lp); - pl_ffontsize_r(chart->lp,chart->font_size * 1.5); - pl_move_r(chart->lp,chart->data_left, chart->title_bottom); + cairo_save (cr); + cairo_move_to (cr, geom->data_left, geom->title_bottom); - va_start(ap,title); - vsnprintf(buf,100,title,ap); - pl_alabel_r(chart->lp,0,0,buf); - va_end(ap); + va_start(ap, title); + s = xvasprintf (title, ap); + chart_label (cr, 'l', 'x', geom->font_size * 1.5, s); + free (s); + va_end (ap); - pl_restorestate_r(chart->lp); + cairo_restore (cr); } /* Set the scale for the abscissa */ -void -chart_write_xscale(struct chart *ch, double min, double max, int ticks) +void +chart_write_xscale (cairo_t *cr, struct chart_geometry *geom, + double min, double max, int ticks) { double x; - const double tick_interval = - chart_rounded_tick( (max - min) / (double) ticks); - - assert ( ch ); - - - ch->x_max = ceil( max / tick_interval ) * tick_interval ; - ch->x_min = floor ( min / tick_interval ) * tick_interval ; - + const double tick_interval = + chart_rounded_tick ((max - min) / (double) ticks); - ch->abscissa_scale = fabs(ch->data_right - ch->data_left) / - fabs(ch->x_max - ch->x_min); - - for(x = ch->x_min ; x <= ch->x_max; x += tick_interval ) - { - draw_tick (ch, TICK_ABSCISSA, - (x - ch->x_min) * ch->abscissa_scale, "%g", x); - } + geom->x_max = ceil (max / tick_interval) * tick_interval; + geom->x_min = floor (min / tick_interval) * tick_interval; + geom->abscissa_scale = fabs(geom->data_right - geom->data_left) / + fabs(geom->x_max - geom->x_min); + for (x = geom->x_min; x <= geom->x_max; x += tick_interval) + draw_tick (cr, geom, TICK_ABSCISSA, + (x - geom->x_min) * geom->abscissa_scale, "%g", x); } /* Set the scale for the ordinate */ -void -chart_write_yscale(struct chart *ch, double smin, double smax, int ticks) +void +chart_write_yscale (cairo_t *cr, struct chart_geometry *geom, + double smin, double smax, int ticks) { double y; - const double tick_interval = - chart_rounded_tick( (smax - smin) / (double) ticks); - - if ( !ch ) - return; + const double tick_interval = + chart_rounded_tick ((smax - smin) / (double) ticks); - ch->y_max = ceil ( smax / tick_interval ) * tick_interval ; - ch->y_min = floor ( smin / tick_interval ) * tick_interval ; + geom->y_max = ceil (smax / tick_interval) * tick_interval; + geom->y_min = floor (smin / tick_interval) * tick_interval; - ch->ordinate_scale = - fabs(ch->data_top - ch->data_bottom) / fabs(ch->y_max - ch->y_min) ; + geom->ordinate_scale = + (fabs (geom->data_top - geom->data_bottom) + / fabs (geom->y_max - geom->y_min)); - for(y = ch->y_min ; y <= ch->y_max; y += tick_interval ) - { - draw_tick (ch, TICK_ORDINATE, - (y - ch->y_min) * ch->ordinate_scale, "%g", y); - } + for (y = geom->y_min; y <= geom->y_max; y += tick_interval) + draw_tick (cr, geom, TICK_ORDINATE, + (y - geom->y_min) * geom->ordinate_scale, "%g", y); } - /* Write the abscissa label */ -void -chart_write_xlabel(struct chart *ch, const char *label) +void +chart_write_xlabel (cairo_t *cr, const struct chart_geometry *geom, + const char *label) { - if ( ! ch ) - return ; - - pl_savestate_r(ch->lp); - - pl_move_r(ch->lp,ch->data_left, ch->abscissa_top); - pl_alabel_r(ch->lp,0,'t',label); - - pl_restorestate_r(ch->lp); - + cairo_move_to (cr, geom->data_left, geom->abscissa_top); + chart_label (cr, 'l', 't', geom->font_size, label); } - - /* Write the ordinate label */ -void -chart_write_ylabel(struct chart *ch, const char *label) +void +chart_write_ylabel (cairo_t *cr, const struct chart_geometry *geom, + const char *label) { - if ( ! ch ) - return ; + cairo_save (cr); + cairo_translate (cr, -geom->data_bottom, -geom->ordinate_right); + cairo_move_to (cr, 0, 0); + cairo_rotate (cr, M_PI / 2.0); + chart_label (cr, 'l', 'x', geom->font_size, label); + cairo_restore (cr); +} - pl_savestate_r(ch->lp); - pl_move_r(ch->lp, ch->data_bottom, ch->ordinate_right); - pl_textangle_r(ch->lp, 90); - pl_alabel_r(ch->lp, 0, 0, label); +void +chart_write_legend (cairo_t *cr, const struct chart_geometry *geom) +{ + int i; + const int vstep = geom->font_size * 2; + const int xpad = 10; + const int ypad = 10; + const int swatch = 20; + const int legend_top = geom->data_top; + const int legend_bottom = legend_top - + (vstep * geom->n_datasets + 2 * ypad ); + + cairo_save (cr); + + cairo_rectangle (cr, geom->legend_left, legend_top, + geom->legend_right - xpad - geom->legend_left, + legend_bottom - legend_top); + cairo_stroke (cr); + + for (i = 0 ; i < geom->n_datasets ; ++i ) + { + const int ypos = legend_top - vstep * (i + 1); + const int xpos = geom->legend_left + xpad; + const struct chart_colour *colour; + + cairo_move_to (cr, xpos, ypos); + + cairo_save (cr); + colour = &data_colour [ i % N_CHART_COLOURS]; + cairo_set_source_rgb (cr, + colour->red / 255.0, + colour->green / 255.0, + colour->blue / 255.0); + cairo_rectangle (cr, xpos, ypos, swatch, swatch); + cairo_fill_preserve (cr); + cairo_stroke (cr); + cairo_restore (cr); + + cairo_move_to (cr, xpos + swatch * 1.5, ypos); + chart_label (cr, 'l', 'x', geom->font_size, geom->dataset[i]); + } - pl_restorestate_r(ch->lp); + cairo_restore (cr); }