Add support for charts to ASCII driver. Bug #16364.
[pspp-builds.git] / src / output / chart.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004 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
21 #include <assert.h>
22 #include <errno.h>
23 #include <float.h>
24 #include <math.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <plot.h>
31
32 #include <libpspp/alloc.h>
33 #include <libpspp/str.h>
34 #include <output/manager.h>
35 #include <output/output.h>
36
37 #include "error.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 extern struct som_table_class tab_table_class;
43
44 struct chart *
45 chart_create(void)
46 {
47   struct chart *chart;
48   struct outp_driver *d;
49
50   d = outp_drivers (NULL);
51   if (d == NULL)
52     return NULL;
53
54   chart = xmalloc (sizeof *chart);
55   d->class->initialise_chart(d, chart);
56   if (!chart->lp)
57     {
58       free (chart);
59       return NULL;
60     }
61
62   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
63     return NULL;
64
65   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
66   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
67   pl_pencolorname_r (chart->lp, "black");
68
69   pl_erase_r (chart->lp);               /* erase graphics display */
70   pl_filltype_r(chart->lp,0);
71
72   pl_savestate_r(chart->lp);
73
74   /* Set default chartetry */
75   chart->data_top =   900;
76   chart->data_right = 800;
77   chart->data_bottom = 120;
78   chart->data_left = 150;
79   chart->abscissa_top = 70;
80   chart->ordinate_right = 120;
81   chart->title_bottom = 920;
82   chart->legend_left = 810;
83   chart->legend_right = 1000;
84   chart->font_size = 0;
85   strcpy(chart->fill_colour,"red");
86
87   /* Get default font size */
88   if ( !chart->font_size)
89     chart->font_size = pl_fontsize_r(chart->lp, -1);
90
91   /* Draw the data area */
92   pl_box_r(chart->lp,
93            chart->data_left, chart->data_bottom,
94            chart->data_right, chart->data_top);
95
96   return chart;
97 }
98
99 void
100 chart_submit(struct chart *chart)
101 {
102   struct som_entity s;
103   struct outp_driver *d;
104
105   if ( ! chart )
106      return ;
107
108   pl_restorestate_r(chart->lp);
109
110   s.class = &tab_table_class;
111   s.ext = chart;
112   s.type = SOM_CHART;
113   som_submit (&s);
114
115   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
116     {
117       fprintf (stderr, "Couldn't close Plotter\n");
118     }
119
120   pl_deletepl_r(chart->lp);
121
122   pl_deleteplparams(chart->pl_params);
123
124   d = outp_drivers (NULL);
125   d->class->finalise_chart(d, chart);
126   free(chart);
127 }
128
129 void
130 chart_init_separate (struct chart *ch, const char *type,
131                      const char *file_name_tmpl, int number)
132 {
133   FILE *fp;
134   int number_pos;
135
136   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
137   ch->file_name = xasprintf ("%.*s%d%s",
138                              number_pos, file_name_tmpl,
139                              number,
140                              file_name_tmpl + number_pos + 1);
141   fp = fopen (ch->file_name, "wb");
142   if (fp == NULL)
143     {
144       error (0, errno, _("creating \"%s\""), ch->file_name);
145       free (ch->file_name);
146       ch->file_name = NULL;
147       return;
148     }
149
150   ch->pl_params = pl_newplparams ();
151   ch->lp = pl_newpl_r (type, 0, fp, stderr, ch->pl_params);
152 }
153
154 void
155 chart_finalise_separate (struct chart *ch)
156 {
157   free (ch->file_name);
158 }