8c96b73fadaf517c93a388e8c26f087bf4a7b0ab
[pspp-builds.git] / src / output / charts / np-plot-cairo.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/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_item *chart_item, cairo_t *cr,
31                     struct xrchart_geometry *geom)
32 {
33   const struct np_plot_chart *npp = to_np_plot_chart (chart_item);
34   struct casereader *data;
35   struct ccase *c;
36
37   xrchart_write_title (cr, geom, _("Normal Q-Q Plot of %s"), chart_item->title);
38   xrchart_write_xlabel (cr, geom, _("Observed Value"));
39   xrchart_write_ylabel (cr, geom, _("Expected Normal"));
40   xrchart_write_xscale (cr, geom,
41                       npp->x_lower - npp->slack,
42                       npp->x_upper + npp->slack, 5);
43   xrchart_write_yscale (cr, geom, npp->y_first, npp->y_last, 5);
44
45   data = casereader_clone (npp->data);
46   for (; (c = casereader_read (data)) != NULL; case_unref (c))
47     xrchart_datum (cr, geom, 0,
48                  case_data_idx (c, NP_IDX_Y)->f,
49                  case_data_idx (c, NP_IDX_NS)->f);
50   casereader_destroy (data);
51
52   xrchart_line (cr, geom, npp->slope, npp->intercept,
53               npp->y_first, npp->y_last, XRCHART_DIM_Y);
54 }
55
56 static void
57 dnp_plot_chart_draw (const struct chart_item *chart_item, cairo_t *cr,
58                      struct xrchart_geometry *geom)
59 {
60   const struct np_plot_chart *dnpp = to_np_plot_chart (chart_item);
61   struct casereader *data;
62   struct ccase *c;
63
64   xrchart_write_title (cr, geom, _("Detrended Normal Q-Q Plot of %s"), chart_item->title);
65   xrchart_write_xlabel (cr, geom, _("Observed Value"));
66   xrchart_write_ylabel (cr, geom, _("Dev from Normal"));
67   xrchart_write_xscale (cr, geom, dnpp->y_min, dnpp->y_max, 5);
68   xrchart_write_yscale (cr, geom, dnpp->dns_min, dnpp->dns_max, 5);
69
70   data = casereader_clone (dnpp->data);
71   for (; (c = casereader_read (data)) != NULL; case_unref (c))
72     xrchart_datum (cr, geom, 0, case_data_idx (c, NP_IDX_Y)->f,
73                    case_data_idx (c, NP_IDX_DNS)->f);
74   casereader_destroy (data);
75
76   xrchart_line (cr, geom, 0, 0, dnpp->y_min, dnpp->y_max, XRCHART_DIM_X);
77 }
78
79 void
80 xrchart_draw_np_plot (const struct chart_item *chart_item, cairo_t *cr,
81                       struct xrchart_geometry *geom)
82 {
83   const struct np_plot_chart *npp = to_np_plot_chart (chart_item);
84
85   if (npp->detrended)
86     dnp_plot_chart_draw (chart_item, cr, geom);
87   else
88     np_plot_chart_draw (chart_item, cr, geom);
89 }