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;
66 geom->in_path = false;
70 geom->fill_colour.red = 255;
71 geom->fill_colour.green = 0;
72 geom->fill_colour.blue = 0;
74 cairo_set_line_width (cr, 1.0);
76 cairo_rectangle (cr, geom->data_left, geom->data_bottom,
77 geom->data_right - geom->data_left,
78 geom->data_top - geom->data_bottom);
83 chart_geometry_free (cairo_t *cr UNUSED, struct chart_geometry *geom)
87 for (i = 0 ; i < geom->n_datasets; ++i)
88 free (geom->dataset[i]);
93 chart_draw (const struct chart *chart, cairo_t *cr,
94 struct chart_geometry *geom)
96 chart->class->draw (chart, cr, geom);
100 chart_draw_png (const struct chart *chart, const char *file_name_template,
103 const int width = 640;
104 const int length = 480;
106 struct chart_geometry geom;
107 cairo_surface_t *surface;
108 cairo_status_t status;
109 const char *number_pos;
113 number_pos = strchr (file_name_template, '#');
114 if (number_pos != NULL)
115 file_name = xasprintf ("%.*s%d%s", (int) (number_pos - file_name_template),
116 file_name_template, number, number_pos + 1);
118 file_name = xstrdup (file_name_template);
120 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
121 cr = cairo_create (surface);
123 cairo_translate (cr, 0.0, length);
124 cairo_scale (cr, 1.0, -1.0);
127 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
128 cairo_rectangle (cr, 0, 0, width, length);
132 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
134 chart_geometry_init (cr, &geom, width, length);
135 chart_draw (chart, cr, &geom);
136 chart_geometry_free (cr, &geom);
138 status = cairo_surface_write_to_png (surface, file_name);
139 if (status != CAIRO_STATUS_SUCCESS)
140 error (0, 0, _("writing output file \"%s\": %s"),
141 file_name, cairo_status_to_string (status));
144 cairo_surface_destroy (surface);
151 chart_ref (const struct chart *chart_)
153 struct chart *chart = CONST_CAST (struct chart *, chart_);
159 chart_unref (struct chart *chart)
163 assert (chart->ref_cnt > 0);
164 if (--chart->ref_cnt == 0)
165 chart->class->destroy (chart);
170 chart_submit (struct chart *chart)
173 struct outp_driver *d;
175 for (d = outp_drivers (NULL); d; d = outp_drivers (d))
176 if (d->class->output_chart != NULL)
177 d->class->output_chart (d, chart);