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/>. */
19 #include <output/chart.h>
20 #include <output/chart-provider.h>
23 #include <cairo/cairo.h>
32 #include <libpspp/str.h>
33 #include <output/manager.h>
34 #include <output/output.h>
40 #define _(msgid) gettext (msgid)
42 extern struct som_table_class tab_table_class;
45 chart_init (struct chart *chart, const struct chart_class *class)
52 chart_geometry_init (cairo_t *cr, struct chart_geometry *geom,
53 double width, double length)
55 /* Set default chartetry. */
56 geom->data_top = 0.900 * length;
57 geom->data_right = 0.800 * width;
58 geom->data_bottom = 0.120 * length;
59 geom->data_left = 0.150 * width;
60 geom->abscissa_top = 0.070 * length;
61 geom->ordinate_right = 0.120 * width;
62 geom->title_bottom = 0.920 * length;
63 geom->legend_left = 0.810 * width;
64 geom->legend_right = width;
65 geom->font_size = 15.0;
67 geom->fill_colour.red = 255;
68 geom->fill_colour.green = 0;
69 geom->fill_colour.blue = 0;
71 cairo_set_line_width (cr, 1.0);
73 cairo_rectangle (cr, geom->data_left, geom->data_bottom,
74 geom->data_right - geom->data_left,
75 geom->data_top - geom->data_bottom);
80 chart_geometry_free (cairo_t *cr UNUSED)
85 chart_draw (const struct chart *chart, cairo_t *cr,
86 struct chart_geometry *geom)
88 chart->class->draw (chart, cr, geom);
92 chart_draw_png (const struct chart *chart, const char *file_name_template,
95 const int width = 640;
96 const int length = 480;
98 struct chart_geometry geom;
99 cairo_surface_t *surface;
100 cairo_status_t status;
101 const char *number_pos;
105 number_pos = strchr (file_name_template, '#');
106 if (number_pos != NULL)
107 file_name = xasprintf ("%.*s%d%s", (int) (number_pos - file_name_template),
108 file_name_template, number, number_pos + 1);
110 file_name = xstrdup (file_name_template);
112 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
113 cr = cairo_create (surface);
115 cairo_translate (cr, 0.0, length);
116 cairo_scale (cr, 1.0, -1.0);
119 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
120 cairo_rectangle (cr, 0, 0, width, length);
124 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
126 chart_geometry_init (cr, &geom, width, length);
127 chart_draw (chart, cr, &geom);
128 chart_geometry_free (cr);
130 status = cairo_surface_write_to_png (surface, file_name);
131 if (status != CAIRO_STATUS_SUCCESS)
132 error (0, 0, _("writing output file \"%s\": %s"),
133 file_name, cairo_status_to_string (status));
136 cairo_surface_destroy (surface);
143 chart_ref (const struct chart *chart_)
145 struct chart *chart = CONST_CAST (struct chart *, chart_);
151 chart_unref (struct chart *chart)
155 assert (chart->ref_cnt > 0);
156 if (--chart->ref_cnt == 0)
157 chart->class->destroy (chart);
162 chart_submit (struct chart *chart)
165 struct outp_driver *d;
167 for (d = outp_drivers (NULL); d; d = outp_drivers (d))
168 if (d->class->output_chart != NULL)
169 d->class->output_chart (d, chart);