Corrected bug in chart rendering which caused the ordinate label to never get displayed
[pspp] / src / output / charts / spreadlevel-plot.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2012 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/spreadlevel-plot.h"
20
21 #include "libpspp/cast.h"
22 #include "output/chart-item-provider.h"
23
24 #include "gl/xalloc.h"
25 #include "gl/minmax.h"
26
27 #include <math.h>
28 #include <float.h>
29
30 struct chart_item *
31 spreadlevel_plot_create (const char *label, double tx_pwr)
32 {
33   struct spreadlevel_plot_chart *sl = xzalloc (sizeof *sl);
34   chart_item_init (&sl->chart_item, &spreadlevel_plot_chart_class, label);
35
36   sl->x_lower = DBL_MAX;
37   sl->x_upper = -DBL_MAX;
38
39   sl->y_lower = DBL_MAX;
40   sl->y_upper = -DBL_MAX;
41
42   sl->tx_pwr = tx_pwr;
43
44   sl->n_data = 0;
45   sl->data = NULL;
46   
47   return &sl->chart_item;
48 }
49
50 void 
51 spreadlevel_plot_add (struct chart_item *ci, double spread, double level)
52 {
53   struct spreadlevel_plot_chart *sl = to_spreadlevel_plot_chart (ci);
54
55   if ( sl->tx_pwr == 0)
56     {
57       spread = log (spread);
58       level = log (level);
59     }
60   else
61     {
62       spread = pow (spread, sl->tx_pwr);
63       level = pow (level, sl->tx_pwr);
64     }
65
66   sl->x_lower = MIN (sl->x_lower, level);
67   sl->x_upper = MAX (sl->x_upper, level);
68
69   sl->y_lower = MIN (sl->y_lower, spread);
70   sl->y_upper = MAX (sl->y_upper, spread);
71
72   sl->n_data++;
73   sl->data = xrealloc (sl->data, sizeof (*sl->data) * sl->n_data);
74   sl->data[sl->n_data - 1].x = level;
75   sl->data[sl->n_data - 1].y = spread;
76 }
77
78
79 static void
80 spreadlevel_plot_chart_destroy (struct chart_item *chart_item)
81 {
82   struct spreadlevel_plot_chart *sl = to_spreadlevel_plot_chart (chart_item);
83
84   free (sl->data);
85   free (sl);
86 }
87
88 const struct chart_item_class spreadlevel_plot_chart_class =
89   {
90     spreadlevel_plot_chart_destroy
91   };