scatterplot: changed colorscheme - first color is black - fixes bug#47806
[pspp] / src / output / charts / scatterplot-cairo.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2014, 2015 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 static const struct xrchart_colour black = {0,0,0};
32
33 void
34 xrchart_draw_scatterplot (const struct chart_item *chart_item, cairo_t *cr,
35                           struct xrchart_geometry *geom)
36 {
37   const struct scatterplot_chart *spc = to_scatterplot_chart (chart_item);
38   struct casereader *data;
39   struct ccase *c;
40   /* While reading the cases, a list with categories of the byvar is build */
41   /* All distinct values are stored in catvals                             */
42   /* Each category will later have a different plot colour                 */
43   const int MAX_PLOT_CATS = 20;
44   union value catvals[MAX_PLOT_CATS];
45   int n_catvals = 0;
46   int byvar_width = 0;
47   int i = 0;
48   const struct xrchart_colour *colour;
49   
50   if (spc->byvar)
51     byvar_width = var_get_width (spc->byvar);
52
53   xrchart_write_xscale (cr, geom, spc->x_min, spc->x_max);
54   xrchart_write_yscale (cr, geom, spc->y_min, spc->y_max);
55   xrchart_write_title (cr, geom, _("Scatterplot %s"), chart_item->title);
56   xrchart_write_xlabel (cr, geom, spc->xlabel);
57   xrchart_write_ylabel (cr, geom, spc->ylabel);
58
59   cairo_save (cr);
60   data = casereader_clone (spc->data);
61   for (; (c = casereader_read (data)) != NULL; case_unref (c))
62     {
63       if (spc->byvar)
64         {
65           const union value *val = case_data_idx (c,SP_IDX_BY);
66           for(i=0;i<n_catvals && !value_equal (&catvals[i],val,byvar_width);i++);
67           if (i == n_catvals) /* No entry found */
68             {
69               if (n_catvals < MAX_PLOT_CATS)
70                 {
71                   struct string label;
72                   ds_init_empty (&label);
73                   if (var_is_value_missing (spc->byvar,val,MV_ANY))
74                     ds_put_cstr (&label,"missing");
75                   else
76                     var_append_value_name (spc->byvar,val,&label);
77                   value_clone (&catvals[n_catvals++],val,byvar_width);
78                   geom->n_datasets++;
79                   geom->dataset = xrealloc (geom->dataset,
80                                             geom->n_datasets * sizeof (*geom->dataset));
81
82                   geom->dataset[geom->n_datasets - 1] = strdup (ds_cstr(&label));
83                   ds_destroy (&label);
84                 }
85               else /* Use the last plot category */
86                 {
87                   *(spc->byvar_overflow) = true;
88                   i--;
89                 }
90             }
91           colour = &data_colour[i % XRCHART_N_COLOURS];
92         }
93       else
94         colour = &black;
95
96       cairo_set_source_rgb (cr,
97                             colour->red / 255.0,
98                             colour->green / 255.0,
99                             colour->blue / 255.0);
100     
101       xrchart_datum (cr, geom, 0,
102                      case_data_idx (c, SP_IDX_X)->f,
103                      case_data_idx (c, SP_IDX_Y)->f);
104     }
105   casereader_destroy (data);
106   cairo_restore (cr);
107
108   for(i=0;i<n_catvals;i++)
109     value_destroy (&catvals[i],byvar_width);
110
111   if (spc->byvar)
112     xrchart_write_legend (cr, geom);
113
114 }