b1f96ef931b06f6a401c893ddd091271a2e03156
[pspp] / 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 <plot.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 (plPlotter *lp, struct chart_geometry *geom)
53 {
54   /* Start output page. */
55   pl_openpl_r (lp);
56
57   /* Set coordinate system. */
58   pl_fspace_r (lp, 0.0, 0.0, 1000.0, 1000.0);
59
60   /* Set line thickness. */
61   pl_flinewidth_r (lp, 0.25);
62   pl_pencolorname_r (lp, "black");
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 = 900;
72   geom->data_right = 800;
73   geom->data_bottom = 120;
74   geom->data_left = 150;
75   geom->abscissa_top = 70;
76   geom->ordinate_right = 120;
77   geom->title_bottom = 920;
78   geom->legend_left = 810;
79   geom->legend_right = 1000;
80   geom->font_size = 0;
81   strcpy (geom->fill_colour, "red");
82
83   /* Get default font size */
84   if (!geom->font_size)
85     geom->font_size = pl_fontsize_r (lp, -1);
86
87   /* Draw the data area */
88   pl_box_r (lp,
89             geom->data_left, geom->data_bottom,
90             geom->data_right, geom->data_top);
91 }
92
93 void
94 chart_geometry_free (plPlotter *lp)
95 {
96   if (pl_closepl_r (lp) < 0)
97     fprintf (stderr, "Couldn't close Plotter\n");
98 }
99
100 void
101 chart_draw (const struct chart *chart, plPlotter *lp)
102 {
103   chart->class->draw (chart, lp);
104 }
105
106 struct chart *
107 chart_ref (const struct chart *chart_)
108 {
109   struct chart *chart = (struct chart *) chart_;
110   chart->ref_cnt++;
111   return chart;
112 }
113
114 void
115 chart_unref (struct chart *chart)
116 {
117   if (chart != NULL)
118     {
119       assert (chart->ref_cnt > 0);
120       if (--chart->ref_cnt == 0)
121         chart->class->destroy (chart);
122     }
123 }
124
125 void
126 chart_submit (struct chart *chart)
127 {
128   struct outp_driver *d;
129
130   for (d = outp_drivers (NULL); d; d = outp_drivers (d))
131     if (d->class->output_chart != NULL)
132       d->class->output_chart (d, chart);
133
134   chart_unref (chart);
135 }
136
137 bool
138 chart_create_file (const char *type, const char *file_name_tmpl, int number,
139                    plPlotterParams *params, char **file_namep, plPlotter **lpp)
140 {
141   char *file_name = NULL;
142   FILE *fp = NULL;
143   int number_pos;
144   plPlotter *lp;
145
146   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
147   file_name = xasprintf ("%.*s%d%s", number_pos, file_name_tmpl,
148                          number, file_name_tmpl + number_pos + 1);
149
150   fp = fopen (file_name, "wb");
151   if (fp == NULL)
152     {
153       error (0, errno, _("creating \"%s\""), file_name);
154       goto error;
155     }
156
157   if (params != NULL)
158     lp = pl_newpl_r (type, 0, fp, stderr, params);
159   else
160     {
161       params = pl_newplparams ();
162       lp = pl_newpl_r (type, 0, fp, stderr, params);
163       pl_deleteplparams (params);
164     }
165   if (lp == NULL)
166     goto error;
167
168   *file_namep = file_name;
169   *lpp = lp;
170   return true;
171
172 error:
173   if (fp != NULL)
174     {
175       fclose (fp);
176       if (file_name != NULL)
177         unlink (file_name);
178     }
179   free (file_name);
180   *file_namep = NULL;
181   *lpp = NULL;
182   return false;
183 }