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