* configure.ac:
[scdataviz] / src / graph.c
1 /*
2 ** Graph.c
3 ** 
4 ** Made by (Johnny Q. Hacker)
5 ** Login   <solarion@johnathan>
6 ** 
7
8
9 Copyright (C) 2008 Joseph Pingenot
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
25 **Many thanks to Davyd Madeley for the excellent Cairo  Tutorial
26 **  at http://gnomejournal.org/article/34/writing-a--using-cairo-and-gtk28
27
28 ** Started on  Thu Jul 17 10:51:32 2008 Johnny Q. Hacker
29 ** Last update Thu Oct 15 21:51:41 2009 Johnny Q. Hacker
30 */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <math.h>
35 #include "config.h"
36 #include "graph.h"
37
38 #ifdef DEBUG
39 #define DEBUG_AUTOAXIS
40 #endif
41
42 G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
43
44 static void graph_class_init(GraphClass *klass) {
45   GObjectClass *gobject_klass;
46   gobject_klass = G_OBJECT_CLASS(klass);
47 }
48
49 static void graph_init(Graph *graph) {
50   graph->symbol = CIRCLE;
51   graph->points = g_ptr_array_new();
52   graph->lines = NULL;
53   graph->xaxis = graph->yaxis = NULL;
54 }
55
56 Graph *graph_new(void) {
57   return g_object_new(GRAPH_TYPE, NULL);
58 }
59
60 /*1 means you're stupid and passed NULL*/
61 int graph_add_graph_point(Graph* graph, struct graph_point *pt) {
62   if(pt == NULL) return 1;
63   g_ptr_array_add(graph->points, pt);
64   if((graph->points->len == 1) && ((graph->lines == NULL) || (graph->lines->len == 0))) {
65     graph->maxx = graph->minx = pt->x;
66     graph->maxy = graph->miny = pt->y;
67   }else{
68     if(pt->x > graph->maxx) graph->maxx = pt->x;
69     if(pt->x < graph->minx) graph->minx = pt->x;
70     if(pt->y > graph->maxy) graph->maxy = pt->y;
71     if(pt->y < graph->miny) graph->miny = pt->y;
72   }
73   return 0;
74 }
75
76 /*
77  *
78  *1 means failed to allocate point
79  *NOTE: label is duplicated if not NULL (using g_strdup)
80  */
81 int graph_add_point(Graph* graph, double x, double y, const GString *label) {
82   struct graph_point *pt = (struct graph_point*)malloc(sizeof(struct graph_point));
83   if(pt == NULL) return 1;
84   pt->x = x;
85   pt->y = y;
86   pt->label = (label)?g_string_new(label->str):NULL;
87   graph_add_graph_point(graph, pt);
88   return 0;
89 }
90
91 int graph_add_graph_line(Graph* graph, struct graph_line *l) {
92   if(l == NULL) return 1;
93   if(graph->lines == NULL) {
94     if((graph->lines = g_ptr_array_new()) == NULL) return 1;
95   }
96   g_ptr_array_add(graph->lines, l);
97   if((graph->points->len == 0) && 
98      (graph->lines->len == 1)) {
99     graph->maxx = graph->minx = l->p0_x;
100     graph->maxy = graph->miny = l->p0_y;
101   }else{
102     if(l->p0_x > graph->maxx) graph->maxx = l->p0_x;
103     if(l->p0_x < graph->minx) graph->minx = l->p0_x;
104     if(l->p0_y > graph->maxy) graph->maxy = l->p0_y;
105     if(l->p0_y < graph->miny) graph->miny = l->p0_y;
106   }
107   if(l->p3_x > graph->maxx) graph->maxx = l->p3_x;
108   if(l->p3_x < graph->minx) graph->minx = l->p3_x;
109   if(l->p3_y > graph->maxy) graph->maxy = l->p3_y;
110   if(l->p3_y < graph->miny) graph->miny = l->p3_y;
111   return 0;
112 }
113
114
115 int graph_add_line(Graph* graph, double p0_x, double p0_y, double p1_x, double p1_y, double p2_x, double p2_y, double p3_x, double p3_y) {
116   #ifdef DEBUG
117   fprintf(stderr, "graph_add_line(%d, p0_x=%g, p0_y=%g, p1_x=%g, p1_y=%g, p2_x=%g, p2_y=%g, p3_x=%g, p3_y=%g\n", (unsigned int) graph, p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
118   #endif
119   struct graph_line *l;
120   if((l=(struct graph_line*)malloc(sizeof(struct graph_line))) == NULL) return 2;
121   l->p0_x=p0_x;
122   l->p0_y=p0_y;
123   l->p1_x=p1_x;
124   l->p1_y=p1_y;
125   l->p2_x=p2_x;
126   l->p2_y=p2_y;
127   l->p3_x=p3_x;
128   l->p3_y=p3_y;
129   return graph_add_graph_line(graph, l);
130 }
131     
132 struct cxt {
133   Graph *graph;
134   struct graph_point *p0;
135 };
136     
137 static void linear_interpolate(gpointer data, gpointer user_data) {
138   struct cxt *cxt = user_data;
139   struct graph_point *p3 = data;
140   if(cxt->p0 != NULL) {
141     double p1_x = (p3->x + 2*cxt->p0->x)/3.0;
142     double p1_y = (p3->y + 2*cxt->p0->y)/3.0;
143     double p2_x = (2*p3->x + cxt->p0->x)/3.0;
144     double p2_y = (2*p3->y + cxt->p0->y)/3.0;
145     graph_add_line(cxt->graph, cxt->p0->x, cxt->p0->y, p1_x, p1_y, p2_x, p2_y, p3->x, p3->y);
146   }
147   cxt->p0 = p3;
148 }
149     
150 void graph_add_linear_connectors(Graph* graph) {
151   struct cxt cxt;
152   cxt.graph = graph;
153   cxt.p0 = NULL;
154   g_ptr_array_foreach(graph->points, &linear_interpolate, &cxt);
155 }
156
157 void graph_bezier_linear_to_cubic(double x0, double x3, double *x1, double *x2) {
158   *x1 = (x3 + 2.0*x0)/3.0;
159   *x2 = (2.0*x3 + x0)/3.0;
160 }
161
162 void graph_bezier_linear_to_quadratic(double x0, double x3, double *x1) {
163   *x1 = (x3 + x0)/2.0;
164 }
165
166 void graph_bezier_quadratic_to_cubic(double x0, double x3, double *x1, double *x2) {
167   *x2 = (x3-2.0*(*x1))/3.0;
168   *x1 = (x0-2.0*(*x1))/3.0;
169 }
170
171 void graph_set_xaxis(Graph *g, struct graph_axis *axis) {
172   if(g->xaxis != NULL) {
173     free(g->xaxis);
174   }
175   g->xaxis=axis;
176 }
177 void graph_set_yaxis(Graph *g, struct graph_axis *axis) {
178   if(g->yaxis != NULL) {
179     free(g->yaxis);
180   }
181   g->yaxis=axis;
182 }
183
184 struct graph_axis* autoset_axis(Graph* g, GString* title, double min, double max) {
185   struct graph_axis *axis;
186   double range_mag;
187   double start_mag;
188   double stop_mag;
189   int i, j;
190   if(g->points->len == 0) {
191     return NULL;
192   }
193   if((axis=(struct graph_axis *)malloc(sizeof(struct graph_axis))) == NULL) {
194     return NULL;
195   }
196   if(g->points->len == 1) {
197     axis->major=1;
198     axis->minor=0;
199     axis->subminor=0;
200     axis->major_start=min;
201     axis->major_step=max;
202   }else{
203     range_mag = logb(max - min);
204     if(min == 0) 
205       start_mag = 0;
206     else
207       start_mag = copysign(pow(10, floor(log10(min))), min);
208     if(max == 0) 
209       stop_mag = 0;
210     else
211       stop_mag = copysign(pow(10, ceil(log10(max))), max);
212     for(i=1; i<9; i++) {
213       if(i*start_mag > min) break;
214     }
215     i--;
216     for(j=10; j>1; j--) {
217       if(j*(stop_mag/10) < max) break;
218     }
219     j++;
220     axis->major=10;
221     axis->minor=1;
222     axis->subminor=4;
223     axis->major_start=i*start_mag;
224     axis->major_step=(j*(stop_mag/10) - axis->major_start)/(axis->major);
225   }
226   axis->title=g_string_new(title->str);
227   #ifdef DEBUG_AUTOAXIS
228   fprintf(stderr, "graph_autoset_xaxis: xmin=%g, log10(xmin)=%g, floor=%g, pow=%g round=%g\n", min, log10(min), floor(log10(min)), pow(10, floor(log10(min))), round(pow(10, floor(log10(min)))));
229   fprintf(stderr, "graph_autoset_xaxis: xmax=%g, log10(xmax)=%g, floor=%g, pow=%g round=%g\n", max, log10(max), floor(log10(max)), pow(10, floor(log10(max))), round(pow(10, floor(log10(max)))));
230   fprintf(stderr, "graph_autoset_xaxis: xmin=%g,max=%g start_mag=%g, stop_mag=%g range_mag=%g\n", min, max, start_mag, stop_mag, range_mag);
231   #endif
232   return axis;
233 }
234
235 int graph_autoset_xaxis(Graph *g, GString *title) {
236   graph_set_xaxis(g, autoset_axis(g, title, g->minx, g->maxx));
237   return 0;
238 }
239
240 int graph_autoset_yaxis(Graph *g, GString *title) {
241   graph_set_yaxis(g, autoset_axis(g, title, g->miny, g->maxy));
242   return 0;
243 }