f02fd4efac9ef80a9007ce3429203891ae772181
[pspp] / src / output / cairo-chart.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2011, 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 #ifndef OUTPUT_CAIRO_CHART_H
18 #define OUTPUT_CAIRO_CHART_H 1
19
20 #include <cairo/cairo.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include "libpspp/compiler.h"
24
25 struct chart_item;
26 struct cell_color;
27
28 struct xrchart_colour
29   {
30     uint8_t red;
31     uint8_t green;
32     uint8_t blue;
33   };
34
35 struct xrchart_axis
36 {
37   int data_max;
38   int data_min;
39
40   double scale;
41   double min;
42   double max;
43 };
44
45 /* The geometry of a chart. */
46 struct xrchart_geometry
47   {
48     /* Bottom of the abscissa segment */
49     int abscissa_bottom;
50
51     /* Left of the ordinate segment */
52     int ordinate_left;
53
54     int title_bottom;
55
56     /* Legend. */
57     int legend_left;
58     int legend_right;
59     char **dataset;
60     int n_datasets;
61
62     /* Default font size for the plot. */
63     double font_size;
64
65     struct xrchart_colour fill_colour;
66
67     /* Stuff particular to cartesians and boxplots. */
68     struct xrchart_axis axis[2];
69
70     /* True iff a path is currently being drawn */
71     bool in_path;
72   };
73
74 void xrchart_geometry_init (cairo_t *, struct xrchart_geometry *,
75                             double width, double length);
76 void xrchart_geometry_free (cairo_t *, struct xrchart_geometry *);
77
78 #define XRCHART_N_COLOURS 27
79 extern const struct xrchart_colour data_colour[];
80
81 enum tick_orientation
82   {
83     SCALE_ABSCISSA=0,
84     SCALE_ORDINATE
85   };
86
87 enum xrmarker_type
88   {
89     XRMARKER_CIRCLE,              /* Hollow circle. */
90     XRMARKER_ASTERISK,            /* Asterisk (*). */
91     XRMARKER_SQUARE               /* Hollow square. */
92   };
93
94 void xrchart_draw_marker (cairo_t *, double x, double y, enum xrmarker_type,
95                           double size);
96
97 void xrchart_label (cairo_t *, int horz_justify, int vert_justify,
98                     double font_size, const char *);
99
100 void xrchart_label_rotate (cairo_t *cr, int horz_justify, int vert_justify,
101                            double font_size, const char *string, double angle);
102
103
104 /* Draw a tick mark at position
105    If label is non zero, then print it at the tick mark
106 */
107 void draw_tick (cairo_t *, const struct xrchart_geometry *,
108                 enum tick_orientation orientation,
109                 bool rotated,
110                 double position,
111                 const char *label, ...)
112   PRINTF_FORMAT (6, 7);
113
114
115 /* Write the title on a chart*/
116 void xrchart_write_title (cairo_t *, const struct xrchart_geometry *,
117                           const char *title, ...)
118   PRINTF_FORMAT (3, 4);
119
120 /* Set the scale for the abscissa */
121 bool xrchart_write_xscale (cairo_t *, struct xrchart_geometry *,
122                            double min, double max) WARN_UNUSED_RESULT;
123
124 /* Set the scale for the ordinate */
125 bool xrchart_write_yscale (cairo_t *, struct xrchart_geometry *,
126                            double smin, double smax) WARN_UNUSED_RESULT;
127
128 void xrchart_write_xlabel (cairo_t *, const struct xrchart_geometry *,
129                            const char *label) ;
130
131 /* Write the ordinate label */
132 void xrchart_write_ylabel (cairo_t *, const struct xrchart_geometry *,
133                            const char *label);
134
135 void xrchart_write_legend (cairo_t *, const struct xrchart_geometry *);
136
137 enum xrchart_dim
138   {
139     XRCHART_DIM_X,
140     XRCHART_DIM_Y
141   };
142
143 void xrchart_vector_start (cairo_t *, struct xrchart_geometry *,
144                            const char *name);
145 void xrchart_vector_end (cairo_t *, struct xrchart_geometry *);
146 void xrchart_vector (cairo_t *, struct xrchart_geometry *, double x, double y);
147
148 /* Plot a data point */
149 void xrchart_datum (cairo_t *, const struct xrchart_geometry *,
150                     int dataset UNUSED, double x, double y);
151
152 /* Draw a line with slope SLOPE and intercept INTERCEPT.
153    between the points limit1 and limit2.
154    If lim_dim is XRCHART_DIM_Y then the limit{1,2} are on the
155    y axis otherwise the x axis
156 */
157 void xrchart_line (cairo_t *, const struct xrchart_geometry *,
158                    double slope, double intercept,
159                    double limit1, double limit2, enum xrchart_dim lim_dim);
160
161 /* Drawing various kinds of charts. */
162 void xrchart_draw_boxplot (const struct chart_item *, cairo_t *,
163                            struct xrchart_geometry *);
164 void xrchart_draw_roc (const struct chart_item *, cairo_t *,
165                        struct xrchart_geometry *);
166 void xrchart_draw_piechart (const struct chart_item *, cairo_t *,
167                             struct xrchart_geometry *);
168 void xrchart_draw_barchart (const struct chart_item *, cairo_t *,
169                             struct xrchart_geometry *);
170 void xrchart_draw_histogram (const struct chart_item *, cairo_t *,
171                              struct xrchart_geometry *);
172 void xrchart_draw_np_plot (const struct chart_item *, cairo_t *,
173                            struct xrchart_geometry *);
174 void xrchart_draw_scree (const struct chart_item *, cairo_t *,
175                          struct xrchart_geometry *);
176 void xrchart_draw_spreadlevel (const struct chart_item *, cairo_t *,
177                          struct xrchart_geometry *);
178 void xrchart_draw_scatterplot (const struct chart_item *, cairo_t *,
179                          struct xrchart_geometry *);
180
181 void xr_draw_chart (const struct chart_item *, cairo_t *,
182                     double width, double height);
183
184 cairo_surface_t *xr_draw_image_chart (const struct chart_item *,
185                                       const struct cell_color *fg,
186                                       const struct cell_color *bg);
187 char *xr_write_png_image (cairo_surface_t *,
188                           const char *file_name_template, int number);
189
190 char *xr_draw_png_chart (const struct chart_item *,
191                          const char *file_name_template, int number,
192                          const struct cell_color *fg,
193                          const struct cell_color *bg);
194
195 char *xr_draw_eps_chart (const struct chart_item *item,
196                          const char *file_name_template, int number,
197                          const struct cell_color *fg,
198                          const struct cell_color *bg);
199
200 #endif /* output/cairo-chart.h */