49819e9fe03cc4073242ae97d05b77365c7e78a2
[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   strcpy(chart->fill_colour,"red");
87
88   /* Get default font size */
89   if ( !chart->font_size)
90     chart->font_size = pl_fontsize_r(chart->lp, -1);
91
92   /* Draw the data area */
93   pl_box_r(chart->lp,
94            chart->data_left, chart->data_bottom,
95            chart->data_right, chart->data_top);
96
97   return chart;
98 }
99
100 void
101 chart_submit(struct chart *chart)
102 {
103   struct som_entity s;
104   struct outp_driver *d;
105
106   if ( ! chart )
107      return ;
108
109   pl_restorestate_r(chart->lp);
110
111   s.class = &tab_table_class;
112   s.ext = chart;
113   s.type = SOM_CHART;
114   som_submit (&s);
115
116   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
117     {
118       fprintf (stderr, "Couldn't close Plotter\n");
119     }
120
121   pl_deletepl_r(chart->lp);
122
123   pl_deleteplparams(chart->pl_params);
124
125   d = outp_drivers (NULL);
126   d->class->finalise_chart(d, chart);
127   free(chart);
128 }
129
130 void
131 chart_init_separate (struct chart *ch, const char *type,
132                      const char *file_name_tmpl, int number)
133 {
134   FILE *fp;
135   int number_pos;
136
137   number_pos = strchr (file_name_tmpl, '#') - file_name_tmpl;
138   ch->file_name = xasprintf ("%.*s%d%s",
139                              number_pos, file_name_tmpl,
140                              number,
141                              file_name_tmpl + number_pos + 1);
142   fp = fopen (ch->file_name, "wb");
143   if (fp == NULL)
144     {
145       error (0, errno, _("creating \"%s\""), ch->file_name);
146       free (ch->file_name);
147       ch->file_name = NULL;
148       return;
149     }
150
151   ch->pl_params = pl_newplparams ();
152   ch->lp = pl_newpl_r (type, 0, fp, stderr, ch->pl_params);
153 }
154
155 void
156 chart_finalise_separate (struct chart *ch)
157 {
158   free (ch->file_name);
159 }