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