c029b1bf9a68709eb6403167846e2a6a7be262a1
[pspp-builds.git] / src / output / charts / cartesian.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
20 #include <config.h>
21
22 #include <math.h>
23 #include <assert.h>
24
25 #include <output/chart.h>
26
27 #include <output/charts/plot-chart.h>
28 #include <output/charts/cartesian.h>
29 #include <libpspp/compiler.h>
30
31
32
33 struct dataset
34 {
35   int n_data;
36   const char *label;
37 };
38
39
40 #define DATASETS 2
41
42 static const struct dataset dataset[DATASETS] =
43   {
44     { 13, "male"},
45     { 11, "female"},
46   };
47
48
49 /* Plot a data point */
50 void
51 chart_datum(struct chart *ch, int dataset UNUSED, double x, double y)
52 {
53   if ( ! ch )
54     return ;
55
56   {
57     const double x_pos =
58       (x - ch->x_min) * ch->abscissa_scale + ch->data_left ;
59
60     const double y_pos =
61       (y - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
62
63     pl_savestate_r(ch->lp);
64
65     pl_fmarker_r(ch->lp, x_pos, y_pos, 6, 15);
66
67     pl_restorestate_r(ch->lp);
68   }
69 }
70
71 /* Draw a line with slope SLOPE and intercept INTERCEPT.
72    between the points limit1 and limit2.
73    If lim_dim is CHART_DIM_Y then the limit{1,2} are on the
74    y axis otherwise the x axis
75 */
76 void
77 chart_line(struct chart *ch, double slope, double intercept,
78            double limit1, double limit2, enum CHART_DIM lim_dim)
79 {
80   double x1, y1;
81   double x2, y2 ;
82
83   if ( ! ch )
84     return ;
85
86
87   if ( lim_dim == CHART_DIM_Y )
88     {
89       x1 = ( limit1 - intercept ) / slope ;
90       x2 = ( limit2 - intercept ) / slope ;
91       y1 = limit1;
92       y2 = limit2;
93     }
94   else
95     {
96       x1 = limit1;
97       x2 = limit2;
98       y1 = slope * x1 + intercept;
99       y2 = slope * x2 + intercept;
100     }
101
102   y1 = (y1 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
103   y2 = (y2 - ch->y_min) * ch->ordinate_scale + ch->data_bottom ;
104   x1 = (x1 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
105   x2 = (x2 - ch->x_min) * ch->abscissa_scale + ch->data_left ;
106
107   pl_savestate_r(ch->lp);
108
109   pl_fline_r(ch->lp, x1, y1, x2, y2);
110
111   pl_restorestate_r(ch->lp);
112
113 }