1 /*	$Id: kplot.h,v 1.40 2015/03/24 13:05:05 kristaps Exp $ */
2 /*
3  * Copyright (c) 2014, 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef KPLOT_H
18 #define KPLOT_H
19 
20 struct 	kpair {
21 	double	 x;
22 	double	 y;
23 };
24 
25 enum	kplottype {
26 	KPLOT_POINTS,
27 	KPLOT_MARKS,
28 	KPLOT_LINES,
29 	KPLOT_LINESPOINTS,
30 	KPLOT_LINESMARKS
31 };
32 
33 enum	ksmthtype {
34 	KSMOOTH_NONE,
35 	KSMOOTH_MOVAVG,
36 	KSMOOTH_CDF,
37 	KSMOOTH_PMF
38 };
39 
40 enum	kplotstype {
41 	KPLOTS_SINGLE,
42 	KPLOTS_YERRORLINE,
43 	KPLOTS_YERRORBAR
44 };
45 
46 enum	kplotctype {
47 	KPLOTCTYPE_DEFAULT = 0,
48 	KPLOTCTYPE_PALETTE,
49 	KPLOTCTYPE_PATTERN,
50 	KPLOTCTYPE_RGBA
51 };
52 
53 struct	kplotccfg {
54 	enum kplotctype	 type;
55 	size_t		 palette;
56 	cairo_pattern_t	*pattern;
57 	double		 rgba[4];
58 };
59 
60 struct 	kplotfont {
61 	cairo_font_slant_t   slant;
62 	cairo_font_weight_t  weight;
63 	const char	    *family;
64 	double		     sz;
65 	struct kplotccfg     clr;
66 };
67 
68 struct	kplotticln {
69 	double		  sz;
70 	double		  len;
71 #define	KPLOT_DASH_MAX	  8
72 	double	  	  dashes[KPLOT_DASH_MAX];
73 	size_t		  dashesz;
74 	double	 	  dashoff;
75 	struct kplotccfg  clr;
76 };
77 
78 struct	kplotpoint {
79 	double		  sz;
80 	double		  radius;
81 	double	  	  dashes[KPLOT_DASH_MAX];
82 	size_t		  dashesz;
83 	double	 	  dashoff;
84 	struct kplotccfg  clr;
85 };
86 
87 struct	kplotline {
88 	double		  sz;
89 	double	  	  dashes[KPLOT_DASH_MAX];
90 	size_t		  dashesz;
91 	double	 	  dashoff;
92 	cairo_line_join_t join;
93 	struct kplotccfg  clr;
94 };
95 
96 struct	ksmthcfg {
97 	size_t		  movsamples;
98 };
99 
100 struct	kdatacfg {
101 	struct kplotline  line;
102 	struct kplotpoint point;
103 };
104 
105 struct	kplotcfg {
106 	struct kplotccfg *clrs;
107 	size_t		  clrsz;
108 	double		  marginsz;
109 #define	MARGIN_LEFT	  0x01
110 #define	MARGIN_RIGHT	  0x02
111 #define	MARGIN_TOP	  0x04
112 #define	MARGIN_BOTTOM	  0x08
113 #define	MARGIN_ALL	  0xf
114 	unsigned int	  margin;
115 	struct kplotline  borderline;
116 	double		  bordersz;
117 #define	BORDER_LEFT	  0x01
118 #define	BORDER_RIGHT	  0x02
119 #define	BORDER_TOP	  0x04
120 #define	BORDER_BOTTOM	  0x08
121 #define	BORDER_ALL	  0xf
122 	unsigned int	  border;
123 	size_t		  xtics;
124 	size_t		  ytics;
125 	struct kplotticln ticline;
126 #define	TIC_LEFT_IN	  0x01
127 #define	TIC_LEFT_OUT	  0x02
128 #define	TIC_RIGHT_IN	  0x04
129 #define	TIC_RIGHT_OUT	  0x08
130 #define	TIC_TOP_IN	  0x10
131 #define	TIC_TOP_OUT	  0x20
132 #define	TIC_BOTTOM_IN	  0x40
133 #define	TIC_BOTTOM_OUT	  0x80
134 	unsigned int	  tic;
135 	double		  xticlabelrot;
136 	void		(*xticlabelfmt)(double, char *, size_t);
137 	void		(*yticlabelfmt)(double, char *, size_t);
138 	double		  yticlabelpad;
139 	double		  xticlabelpad;
140 	struct kplotfont  ticlabelfont;
141 #define	TICLABEL_LEFT	  0x01
142 #define	TICLABEL_RIGHT	  0x02
143 #define	TICLABEL_TOP	  0x04
144 #define	TICLABEL_BOTTOM	  0x08
145 	unsigned int	  ticlabel;
146 #define	GRID_X 		  0x01
147 #define GRID_Y 		  0x02
148 #define GRID_ALL 	  0x03
149 	unsigned int 	  grid;
150 	struct kplotline  gridline;
151 	double		  xaxislabelpad;
152 	double		  yaxislabelpad;
153 	const char	 *xaxislabel;
154 	const char	 *x2axislabel;
155 	const char	 *yaxislabel;
156 	const char	 *y2axislabel;
157 	struct kplotfont  axislabelfont;
158 	double		  xaxislabelrot;
159 	double		  yaxislabelrot;
160 #define	EXTREMA_XMIN	  0x01
161 #define	EXTREMA_XMAX	  0x02
162 #define	EXTREMA_YMIN	  0x04
163 #define	EXTREMA_YMAX	  0x08
164 	unsigned int	  extrema;
165 	double		  extrema_xmin;
166 	double		  extrema_xmax;
167 	double		  extrema_ymin;
168 	double		  extrema_ymax;
169 };
170 
171 struct 	kdata;
172 struct	kplot;
173 
174 __BEGIN_DECLS
175 
176 void		 kdata_destroy(struct kdata *);
177 int		 kdata_get(const struct kdata *, size_t, struct kpair *);
178 
179 int		 kdata_array_add(struct kdata *, size_t, double);
180 struct kdata	*kdata_array_alloc(const struct kpair *, size_t);
181 int		 kdata_array_fill(struct kdata *, void *,
182 			void (*)(size_t, struct kpair *, void *));
183 int		 kdata_array_fill_ydoubles(struct kdata *, const double *);
184 int		 kdata_array_fill_ysizes(struct kdata *, const size_t *);
185 int		 kdata_array_set(struct kdata *, size_t, double, double);
186 
187 int		 kdata_bucket_add(struct kdata *, size_t, double);
188 struct kdata	*kdata_bucket_alloc(size_t, size_t);
189 int		 kdata_bucket_set(struct kdata *, size_t, double, double);
190 
191 struct kdata	*kdata_buffer_alloc(size_t);
192 int		 kdata_buffer_copy(struct kdata *, const struct kdata *);
193 
194 int		 kdata_hist_add(struct kdata *, double, double);
195 struct kdata	*kdata_hist_alloc(double, double, size_t);
196 int		 kdata_hist_set(struct kdata *, double, double);
197 
198 struct kdata	*kdata_mean_alloc(struct kdata *);
199 int		 kdata_mean_attach(struct kdata *, struct kdata *);
200 
201 struct kdata	*kdata_stddev_alloc(struct kdata *);
202 int		 kdata_stddev_attach(struct kdata *, struct kdata *);
203 
204 struct kdata	*kdata_vector_alloc(size_t);
205 int		 kdata_vector_append(struct kdata *, double, double);
206 int		 kdata_vector_set(struct kdata *, size_t, double, double);
207 
208 double		 kdata_pmfmean(const struct kdata *);
209 double		 kdata_pmfvar(const struct kdata *);
210 double		 kdata_pmfstddev(const struct kdata *);
211 
212 ssize_t		 kdata_xmax(const struct kdata *, struct kpair *);
213 double		 kdata_xmean(const struct kdata *);
214 ssize_t		 kdata_xmin(const struct kdata *, struct kpair *);
215 
216 double		 kdata_xstddev(const struct kdata *);
217 ssize_t		 kdata_ymax(const struct kdata *, struct kpair *);
218 double		 kdata_ymean(const struct kdata *);
219 double		 kdata_ystddev(const struct kdata *);
220 ssize_t		 kdata_ymin(const struct kdata *, struct kpair *);
221 
222 void		 kdatacfg_defaults(struct kdatacfg *);
223 void		 kplotcfg_defaults(struct kplotcfg *);
224 int		 kplotcfg_default_palette(struct kplotccfg **, size_t *);
225 void		 ksmthcfg_defaults(struct ksmthcfg *);
226 
227 struct kplot	*kplot_alloc(const struct kplotcfg *);
228 int		 kplot_detach(struct kplot *, const struct kdata *);
229 int		 kplot_attach_data(struct kplot *, struct kdata *,
230 			enum kplottype, const struct kdatacfg *);
231 int		 kplot_attach_smooth(struct kplot *, struct kdata *,
232 			enum kplottype, const struct kdatacfg *,
233 			enum ksmthtype, const struct ksmthcfg *);
234 int		 kplot_attach_datas(struct kplot *, size_t,
235 			struct kdata **, const enum kplottype *,
236 			const struct kdatacfg *const *, enum kplotstype);
237 void		 kplot_draw(struct kplot *, double, double, cairo_t *);
238 void		 kplot_free(struct kplot *);
239 int		 kplot_get_datacfg(struct kplot *, size_t,
240 			struct kdatacfg **, size_t *);
241 struct kplotcfg	*kplot_get_plotcfg(struct kplot *);
242 
243 /* getter added by C. Richard - 2020 */
244 double get_dimx();
245 double get_dimy();
246 double get_offsx();
247 double get_offsy();
248 
249 
250 __END_DECLS
251 
252 #endif
253