Support mult-data charts and legend.
[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/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 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   chart->lp = NULL;
56   d->class->initialise_chart(d, chart);
57   if (!chart->lp)
58     {
59       free (chart);
60       return NULL;
61     }
62
63   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
64     return NULL;
65
66   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
67   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
68   pl_pencolorname_r (chart->lp, "black");
69
70   pl_erase_r (chart->lp);               /* erase graphics display */
71   pl_filltype_r(chart->lp,0);
72
73   pl_savestate_r(chart->lp);
74
75   /* Set default chartetry */
76   chart->data_top =   900;
77   chart->data_right = 800;
78   chart->data_bottom = 120;
79   chart->data_left = 150;
80   chart->abscissa_top = 70;
81   chart->ordinate_right = 120;
82   chart->title_bottom = 920;
83   chart->legend_left = 810;
84   chart->legend_right = 1000;
85   chart->font_size = 0;
86   chart->in_path = false;
87   chart->dataset = NULL;
88   chart->n_datasets = 0;
89   strcpy(chart->fill_colour,"red");
90
91   /* Get default font size */
92   if ( !chart->font_size)
93     chart->font_size = pl_fontsize_r(chart->lp, -1);
94
95   /* Draw the data area */
96   pl_box_r(chart->lp,
97            chart->data_left, chart->data_bottom,
98            chart->data_right, chart->data_top);
99
100   return chart;
101 }
102
103 void
104 chart_submit(struct chart *chart)
105 {
106   int i;
107   struct som_entity s;
108   struct outp_driver *d;
109
110   if ( ! chart )
111      return ;
112
113   pl_restorestate_r(chart->lp);
114
115   s.class = &tab_table_class;
116   s.ext = chart;
117   s.type = SOM_CHART;
118   som_submit (&s);
119
120   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
121     {
122       fprintf (stderr, "Couldn't close Plotter\n");
123     }
124
125   pl_deletepl_r(chart->lp);
126
127   pl_deleteplparams(chart->pl_params);
128
129   d = outp_drivers (NULL);
130   d->class->finalise_chart(d, chart);
131
132   for (i = 0 ; i < chart->n_datasets; ++i)
133     free (chart->dataset[i]);
134   free (chart->dataset);
135
136   free(chart);
137 }
138
139 void
140 chart_init_separate (struct chart *ch, const char *type,
141                      const char *file_name_tmpl, int number)
142 {
143   FILE *fp;
144   int number_pos;
145
146   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
147   ch->file_name = xasprintf ("%.*s%d%s",
148                              number_pos, file_name_tmpl,
149                              number,
150                              file_name_tmpl + number_pos + 1);
151   fp = fopen (ch->file_name, "wb");
152   if (fp == NULL)
153     {
154       error (0, errno, _("creating \"%s\""), ch->file_name);
155       free (ch->file_name);
156       ch->file_name = NULL;
157       return;
158     }
159
160   ch->pl_params = pl_newplparams ();
161   ch->lp = pl_newpl_r (type, 0, fp, stderr, ch->pl_params);
162 }
163
164 void
165 chart_finalise_separate (struct chart *ch)
166 {
167   free (ch->file_name);
168 }