63eb13cfefdcddc249203e950bf12f5dbfd0d637
[pspp-builds.git] / src / output / chart.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <plot.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <float.h>
26 #include <assert.h>
27 #include <math.h>
28
29 #include "chart.h"
30 #include <libpspp/str.h>
31 #include <libpspp/alloc.h>
32 #include "manager.h"
33 #include "output.h"
34
35 extern struct som_table_class tab_table_class;
36
37 struct chart *
38 chart_create(void)
39 {
40   struct chart *chart;
41   struct outp_driver *d;
42
43   d = outp_drivers (NULL);
44   if (d == NULL)
45     return NULL;
46
47   chart = xmalloc (sizeof *chart);
48   d->class->initialise_chart(d, chart);
49   if (!chart->lp)
50     {
51       free (chart);
52       return NULL;
53     }
54
55   if (pl_openpl_r (chart->lp) < 0)      /* open Plotter */
56     return NULL;
57
58   pl_fspace_r (chart->lp, 0.0, 0.0, 1000.0, 1000.0); /* set coordinate system */
59   pl_flinewidth_r (chart->lp, 0.25);    /* set line thickness */
60   pl_pencolorname_r (chart->lp, "black");
61
62   pl_erase_r (chart->lp);               /* erase graphics display */
63   pl_filltype_r(chart->lp,0);
64
65   pl_savestate_r(chart->lp);
66
67   /* Set default chartetry */
68   chart->data_top =   900;
69   chart->data_right = 800;
70   chart->data_bottom = 120;
71   chart->data_left = 150;
72   chart->abscissa_top = 70;
73   chart->ordinate_right = 120;
74   chart->title_bottom = 920;
75   chart->legend_left = 810;
76   chart->legend_right = 1000;
77   chart->font_size = 0;
78   strcpy(chart->fill_colour,"red");
79
80   /* Get default font size */
81   if ( !chart->font_size)
82     chart->font_size = pl_fontsize_r(chart->lp, -1);
83
84   /* Draw the data area */
85   pl_box_r(chart->lp,
86            chart->data_left, chart->data_bottom,
87            chart->data_right, chart->data_top);
88
89   return chart;
90 }
91
92 void
93 chart_submit(struct chart *chart)
94 {
95   struct som_entity s;
96   struct outp_driver *d;
97
98   if ( ! chart )
99      return ;
100
101   pl_restorestate_r(chart->lp);
102
103   s.class = &tab_table_class;
104   s.ext = chart;
105   s.type = SOM_CHART;
106   som_submit (&s);
107
108   if (pl_closepl_r (chart->lp) < 0)     /* close Plotter */
109     {
110       fprintf (stderr, "Couldn't close Plotter\n");
111     }
112
113   pl_deletepl_r(chart->lp);
114
115   pl_deleteplparams(chart->pl_params);
116
117   d = outp_drivers (NULL);
118   d->class->finalise_chart(d, chart);
119   free(chart);
120 }
121