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