fb561a26b86d23a5455cec2a47c8e6c0b738f942
[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 <errno.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <libpspp/str.h>
32 #include <output/manager.h>
33 #include <output/output.h>
34
35 #include "error.h"
36 #include "xalloc.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 extern struct som_table_class tab_table_class;
42
43 void
44 chart_init (struct chart *chart, const struct chart_class *class)
45 {
46   chart->class = class;
47   chart->ref_cnt = 1;
48 }
49
50 void
51 chart_geometry_init (plPlotter *lp, struct chart_geometry *geom,
52                      double width, double length)
53 {
54   /* Start output page. */
55   pl_openpl_r (lp);
56
57   /* Set coordinate system. */
58   pl_fspace_r (lp, 0.0, 0.0, width, length);
59
60   /* Set line thickness. */
61   pl_flinewidth_r (lp, 0.25);
62   pl_pencolor_r (lp, 0, 0, 0);
63
64   /* Erase graphics display. */
65   pl_erase_r (lp);
66
67   pl_filltype_r (lp, 0);
68   pl_savestate_r(lp);
69
70   /* Set default chartetry. */
71   geom->data_top = 0.900 * length;
72   geom->data_right = 0.800 * width;
73   geom->data_bottom = 0.120 * length;
74   geom->data_left = 0.150 * width;
75   geom->abscissa_top = 0.070 * length;
76   geom->ordinate_right = 0.120 * width;
77   geom->title_bottom = 0.920 * length;
78   geom->legend_left = 0.810 * width;
79   geom->legend_right = width;
80   geom->font_size = 0;
81
82   geom->fill_colour.red = 255;
83   geom->fill_colour.green = 0;
84   geom->fill_colour.blue = 0;
85
86   /* Get default font size */
87   if (!geom->font_size)
88     geom->font_size = pl_fontsize_r (lp, -1);
89
90   /* Draw the data area */
91   pl_box_r (lp,
92             geom->data_left, geom->data_bottom,
93             geom->data_right, geom->data_top);
94 }
95
96 void
97 chart_geometry_free (plPlotter *lp)
98 {
99   if (pl_closepl_r (lp) < 0)
100     fprintf (stderr, "Couldn't close Plotter\n");
101 }
102
103 void
104 chart_draw (const struct chart *chart, plPlotter *lp,
105             struct chart_geometry *geom)
106 {
107   chart->class->draw (chart, lp, geom);
108 }
109
110 struct chart *
111 chart_ref (const struct chart *chart_)
112 {
113   struct chart *chart = (struct chart *) chart_;
114   chart->ref_cnt++;
115   return chart;
116 }
117
118 void
119 chart_unref (struct chart *chart)
120 {
121   if (chart != NULL)
122     {
123       assert (chart->ref_cnt > 0);
124       if (--chart->ref_cnt == 0)
125         chart->class->destroy (chart);
126     }
127 }
128
129 void
130 chart_submit (struct chart *chart)
131 {
132 #ifdef HAVE_LIBPLOT
133   struct outp_driver *d;
134
135   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
136     if (d->class->output_chart != NULL)
137       d->class->output_chart (d, chart);
138 #endif
139
140   chart_unref (chart);
141 }
142
143 bool
144 chart_create_file (const char *type, const char *file_name_tmpl, int number,
145                    plPlotterParams *params, char **file_namep, plPlotter **lpp)
146 {
147   char *file_name = NULL;
148   FILE *fp = NULL;
149   int number_pos;
150   plPlotter *lp;
151
152   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
153   file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl,
154                          number, file_name_tmpl + number_pos + 1);
155
156   fp = fopen (file_name, "wb");
157   if (fp == NULL)
158     {
159       error (0, errno, _("creating \"%s\""), file_name);
160       goto error;
161     }
162
163   if (params != NULL)
164     lp = pl_newpl_r (type, 0, fp, stderr, params);
165   else
166     {
167       params = pl_newplparams ();
168       lp = pl_newpl_r (type, 0, fp, stderr, params);
169       pl_deleteplparams (params);
170     }
171   if (lp == NULL)
172     goto error;
173
174   *file_namep = file_name;
175   *lpp = lp;
176   return true;
177
178 error:
179   if (fp != NULL)
180     {
181       fclose (fp);
182       if (file_name != NULL)
183         unlink (file_name);
184     }
185   free (file_name);
186   *file_namep = NULL;
187   *lpp = NULL;
188   return false;
189 }