New module to perform decimal floating point arithmetic for charts.
[pspp] / src / output / charts / scatterplot-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2014 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/scatterplot.h"
20
21 #include "data/case.h"
22 #include "data/casereader.h"
23 #include "data/variable.h"
24 #include "output/cairo-chart.h"
25 #include "libpspp/str.h"
26 #include "libpspp/message.h"
27
28 #include "gettext.h"
29 #define _(msgid) gettext (msgid)
30
31
32 void
33 xrchart_draw_scatterplot (const struct chart_item *chart_item, cairo_t *cr,
34                           struct xrchart_geometry *geom)
35 {
36   const struct scatterplot_chart *spc = to_scatterplot_chart (chart_item);
37   struct casereader *data;
38   struct ccase *c;
39   /* While reading the cases, a list with categories of the byvar is build */
40   /* All distinct values are stored in catvals                             */
41   /* Each category will later have a different plot colour                 */
42   const int MAX_PLOT_CATS = 20;
43   union value catvals[MAX_PLOT_CATS];
44   int n_catvals = 0;
45   int byvar_width = 0;
46   int i = 0;
47   const struct xrchart_colour *colour;
48   
49   if (spc->byvar)
50     byvar_width = var_get_width(spc->byvar);
51
52   xrchart_write_xscale (cr, geom,
53                       spc->x_min,
54                       spc->x_max);
55   xrchart_write_yscale (cr, geom, spc->y_min, spc->y_max);
56   xrchart_write_title (cr, geom, _("Scatterplot %s"), chart_item->title);
57   xrchart_write_xlabel (cr, geom, var_to_string(spc->xvar));
58   xrchart_write_ylabel (cr, geom, var_to_string(spc->yvar));
59
60   cairo_save (cr);
61   data = casereader_clone (spc->data);
62   for (; (c = casereader_read (data)) != NULL; case_unref (c))
63     {
64       if (spc->byvar)
65         {
66           const union value *val = case_data(c,spc->byvar);
67           for(i=0;i<n_catvals && !value_equal(&catvals[i],val,byvar_width);i++);
68           if (i == n_catvals) /* No entry found */
69             {
70               if (n_catvals < MAX_PLOT_CATS)
71                 {
72                   struct string label;
73                   ds_init_empty(&label);
74                   if (var_is_value_missing(spc->byvar,val,MV_ANY))
75                     ds_put_cstr(&label,"missing");
76                   else
77                     var_append_value_name(spc->byvar,val,&label);
78                   value_clone(&catvals[n_catvals++],val,byvar_width);
79                   geom->n_datasets++;
80                   geom->dataset = xrealloc (geom->dataset,
81                                             geom->n_datasets * sizeof (*geom->dataset));
82
83                   geom->dataset[geom->n_datasets - 1] = strdup(ds_cstr(&label));
84                   ds_destroy(&label);
85                 }
86               else /* Use the last plot category */
87                 {
88                   *(spc->byvar_overflow) = true;
89                   i--;
90                 }
91             }
92         }
93       colour = &data_colour [ i % XRCHART_N_COLOURS];
94       cairo_set_source_rgb (cr,
95                             colour->red / 255.0,
96                             colour->green / 255.0,
97                             colour->blue / 255.0);
98     
99       xrchart_datum (cr, geom, 0,
100                      case_data (c, spc->xvar)->f,
101                      case_data (c, spc->yvar)->f);
102     }
103   casereader_destroy (data);
104   cairo_restore(cr);
105
106   for(i=0;i<n_catvals;i++)
107     value_destroy(&catvals[i],byvar_width);
108
109   if (spc->byvar)
110     xrchart_write_legend(cr, geom);
111
112     
113
114   //  xrchart_line (cr, geom, npp->slope, npp->intercept,
115   //            npp->y_first, npp->y_last, XRCHART_DIM_Y);
116
117 }