LIST: Remove WEIGHT subcommand.
[pspp-builds.git] / src / output / chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <output/chart.h>
20 #include <output/chart-provider.h>
21
22 #include <assert.h>
23 #include <cairo/cairo.h>
24 #include <errno.h>
25 #include <float.h>
26 #include <math.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <libpspp/str.h>
33 #include <output/manager.h>
34 #include <output/output.h>
35
36 #include "error.h"
37 #include "xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 extern struct som_table_class tab_table_class;
43
44 void
45 chart_init (struct chart *chart, const struct chart_class *class)
46 {
47   chart->class = class;
48   chart->ref_cnt = 1;
49 }
50
51 void
52 chart_geometry_init (cairo_t *cr, struct chart_geometry *geom,
53                      double width, double length)
54 {
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;
67   geom->dataset = NULL;
68   geom->n_datasets = 0;
69
70   geom->fill_colour.red = 255;
71   geom->fill_colour.green = 0;
72   geom->fill_colour.blue = 0;
73
74   cairo_set_line_width (cr, 1.0);
75
76   cairo_rectangle (cr, geom->data_left, geom->data_bottom,
77                    geom->data_right - geom->data_left,
78                    geom->data_top - geom->data_bottom);
79   cairo_stroke (cr);
80 }
81
82 void
83 chart_geometry_free (cairo_t *cr UNUSED, struct chart_geometry *geom)
84 {
85   int i;
86
87   for (i = 0 ; i < geom->n_datasets; ++i)
88     free (geom->dataset[i]);
89   free (geom->dataset);
90 }
91
92 void
93 chart_draw (const struct chart *chart, cairo_t *cr,
94             struct chart_geometry *geom)
95 {
96   chart->class->draw (chart, cr, geom);
97 }
98
99 char *
100 chart_draw_png (const struct chart *chart, const char *file_name_template,
101                 int number)
102 {
103   const int width = 640;
104   const int length = 480;
105
106   struct chart_geometry geom;
107   cairo_surface_t *surface;
108   cairo_status_t status;
109   const char *number_pos;
110   char *file_name;
111   cairo_t *cr;
112
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);
117   else
118     file_name = xstrdup (file_name_template);
119
120   surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, length);
121   cr = cairo_create (surface);
122
123   cairo_translate (cr, 0.0, length);
124   cairo_scale (cr, 1.0, -1.0);
125
126   cairo_save (cr);
127   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
128   cairo_rectangle (cr, 0, 0, width, length);
129   cairo_fill (cr);
130   cairo_restore (cr);
131
132   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
133
134   chart_geometry_init (cr, &geom, width, length);
135   chart_draw (chart, cr, &geom);
136   chart_geometry_free (cr, &geom);
137
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));
142
143   cairo_destroy (cr);
144   cairo_surface_destroy (surface);
145
146   return file_name;
147 }
148
149
150 struct chart *
151 chart_ref (const struct chart *chart_)
152 {
153   struct chart *chart = CONST_CAST (struct chart *, chart_);
154   chart->ref_cnt++;
155   return chart;
156 }
157
158 void
159 chart_unref (struct chart *chart)
160 {
161   if (chart != NULL)
162     {
163       assert (chart->ref_cnt > 0);
164       if (--chart->ref_cnt == 0)
165         chart->class->destroy (chart);
166     }
167 }
168
169 void
170 chart_submit (struct chart *chart)
171 {
172 #ifdef HAVE_CAIRO
173   struct outp_driver *d;
174
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);
178 #endif
179
180   chart_unref (chart);
181 }