case: Introduce new functions for numbers and substrings in cases.
[pspp] / src / output / charts / np-plot-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011 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/np-plot.h"
20
21 #include "data/case.h"
22 #include "data/casereader.h"
23 #include "math/np.h"
24 #include "output/cairo-chart.h"
25
26 #include "gettext.h"
27 #define _(msgid) gettext (msgid)
28
29 static void
30 np_plot_chart_draw (const struct chart *chart, cairo_t *cr,
31                     struct xrchart_geometry *geom)
32 {
33   const struct np_plot_chart *npp = to_np_plot_chart (chart);
34   struct casereader *data;
35   struct ccase *c;
36
37   xrchart_write_title (cr, geom, _("Normal Q-Q Plot of %s"), chart->title);
38   xrchart_write_xlabel (cr, geom, _("Observed Value"));
39   xrchart_write_ylabel (cr, geom, _("Expected Normal"));
40   if (! xrchart_write_xscale (cr, geom,
41                       npp->x_lower - npp->slack,
42                               npp->x_upper + npp->slack))
43     return;
44
45   if (! xrchart_write_yscale (cr, geom, npp->y_first, npp->y_last))
46     return;
47
48   data = casereader_clone (npp->data);
49   for (; (c = casereader_read (data)) != NULL; case_unref (c))
50     xrchart_datum (cr, geom, 0,
51                    case_num_idx (c, NP_IDX_Y),
52                    case_num_idx (c, NP_IDX_NS));
53   casereader_destroy (data);
54
55   xrchart_line (cr, geom, npp->slope, npp->intercept,
56               npp->y_first, npp->y_last, XRCHART_DIM_Y);
57 }
58
59 static void
60 dnp_plot_chart_draw (const struct chart *chart, cairo_t *cr,
61                      struct xrchart_geometry *geom)
62 {
63   const struct np_plot_chart *dnpp = to_np_plot_chart (chart);
64   struct casereader *data;
65   struct ccase *c;
66
67   xrchart_write_title (cr, geom, _("Detrended Normal Q-Q Plot of %s"), chart->title);
68   xrchart_write_xlabel (cr, geom, _("Observed Value"));
69   xrchart_write_ylabel (cr, geom, _("Dev from Normal"));
70   if (! xrchart_write_xscale (cr, geom, dnpp->y_min, dnpp->y_max))
71     return;
72   if (! xrchart_write_yscale (cr, geom, dnpp->dns_min, dnpp->dns_max))
73     return;
74
75   data = casereader_clone (dnpp->data);
76   for (; (c = casereader_read (data)) != NULL; case_unref (c))
77     xrchart_datum (cr, geom, 0, case_num_idx (c, NP_IDX_Y),
78                    case_num_idx (c, NP_IDX_DNS));
79   casereader_destroy (data);
80
81   xrchart_line (cr, geom, 0, 0, dnpp->y_min, dnpp->y_max, XRCHART_DIM_X);
82 }
83
84 void
85 xrchart_draw_np_plot (const struct chart *chart, cairo_t *cr,
86                       struct xrchart_geometry *geom)
87 {
88   const struct np_plot_chart *npp = to_np_plot_chart (chart);
89
90   if (npp->detrended)
91     dnp_plot_chart_draw (chart, cr, geom);
92   else
93     np_plot_chart_draw (chart, cr, geom);
94 }