FACTOR: Added "Scree Plots"
[pspp-builds.git] / src / output / charts / scree.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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/charts/scree.h>
20
21 #include <output/chart-provider.h>
22 #include <output/charts/cartesian.h>
23 #include <output/charts/plot-chart.h>
24
25 #include <gsl/gsl_vector.h>
26
27 #include "xalloc.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 struct scree
33   {
34     struct chart chart;
35     const gsl_vector *eval;
36     const char *xlabel;
37   };
38
39 static const struct chart_class scree_class;
40
41 struct scree *
42 scree_create (const gsl_vector *eigenvalues, const char *xlabel)
43 {
44   struct scree *rc = xmalloc (sizeof *rc);
45   chart_init (&rc->chart, &scree_class);
46   rc->eval = eigenvalues;
47   rc->xlabel = xlabel;
48   return rc;
49 }
50
51
52 struct chart *
53 scree_get_chart (struct scree *rc)
54 {
55   return &rc->chart;
56 }
57
58 static void
59 scree_draw (const struct chart *chart, cairo_t *cr,
60                 struct chart_geometry *geom)
61 {
62   const struct scree *rc = UP_CAST (chart, struct scree, chart);
63   size_t i;
64   double min, max;
65
66   chart_write_title (cr, geom, _("Scree Plot"));
67   chart_write_xlabel (cr, geom, rc->xlabel);
68   chart_write_ylabel (cr, geom, _("Eigenvalue"));
69
70   gsl_vector_minmax (rc->eval, &min, &max);
71
72   if ( fabs (max) > fabs (min))
73     max = fabs (max);
74   else
75     max = fabs (min);
76
77   chart_write_yscale (cr, geom, 0, max, max);
78   chart_write_xscale (cr, geom, 0, rc->eval->size + 1, rc->eval->size + 1);
79
80   chart_vector_start (cr, geom, "");
81   for (i = 0 ; i < rc->eval->size; ++i)
82     {
83       const double x = 1 + i;
84       const double y = gsl_vector_get (rc->eval, i);
85       chart_vector (cr, geom, x, y);
86     }
87   chart_vector_end (cr, geom);
88
89   for (i = 0 ; i < rc->eval->size; ++i)
90     {
91       const double x = 1 + i;
92       const double y = gsl_vector_get (rc->eval, i);
93       chart_datum (cr, geom, 0, x, y);
94     }
95 }
96
97 static void
98 scree_destroy (struct chart *chart)
99 {
100   struct scree *rc = UP_CAST (chart, struct scree, chart);
101
102   free (rc);
103 }
104
105 static const struct chart_class scree_class =
106   {
107     scree_draw,
108     scree_destroy
109   };
110
111