* Well, the groundwork has been laid, but now I can't see my data anymore.
[scdataviz] / 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 Sun May 12 01:17:25 2002 Speed Blue
30 */
31
32 #include <graph.h>
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <math.h>
36
37 G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
38
39 static void graph_class_init(GraphClass *klass) {
40   GObjectClass *gobject_klass;
41   gobject_klass = G_OBJECT_CLASS(klass);
42 }
43
44 static void graph_init(Graph *graph) {
45   graph->symbol = CIRCLE;
46   graph->points = g_ptr_array_new();
47   graph->lines = NULL;
48   graph->xaxis = graph->yaxis = NULL;
49 }
50
51 Graph *graph_new(void) {
52   return g_object_new(GRAPH_TYPE, NULL);
53 }
54
55 /*1 means you're stupid and passed NULL*/
56 int graph_add_graph_point(Graph* graph, struct graph_point *pt) {
57   if(pt == NULL) return 1;
58   g_ptr_array_add(graph->points, pt);
59   if((graph->points->len == 1) && ((graph->lines == NULL) || (graph->lines->len == 0))) {
60     graph->maxx = graph->minx = pt->x;
61     graph->maxy = graph->miny = pt->y;
62   }else{
63     if(pt->x > graph->maxx) graph->maxx = pt->x;
64     if(pt->x < graph->minx) graph->minx = pt->x;
65     if(pt->y > graph->maxy) graph->maxy = pt->y;
66     if(pt->y < graph->miny) graph->miny = pt->y;
67   }
68   return 0;
69 }
70
71 /*
72  *
73  *1 means failed to allocate point
74  *NOTE: label is duplicated if not NULL (using g_strdup)
75  */
76 int graph_add_point(Graph* graph, double x, double y, const GString *label) {
77   struct graph_point *pt = (struct graph_point*)malloc(sizeof(struct graph_point));
78   if(pt == NULL) return 1;
79   pt->x = x;
80   pt->y = y;
81   pt->label = (label)?g_string_new(label->str):NULL;
82   graph_add_graph_point(graph, pt);
83   return 0;
84 }
85
86 int graph_add_graph_line(Graph* graph, struct graph_line *l) {
87   if(l == NULL) return 1;
88   if(graph->lines == NULL) {
89     if((graph->lines = g_ptr_array_new()) == NULL) return 1;
90   }
91   g_ptr_array_add(graph->lines, l);
92   if((graph->points->len == 0) && 
93      (graph->lines->len == 1)) {
94     graph->maxx = graph->minx = l->p0_x;
95     graph->maxy = graph->miny = l->p0_y;
96   }else{
97     if(l->p0_x > graph->maxx) graph->maxx = l->p0_x;
98     if(l->p0_x < graph->minx) graph->minx = l->p0_x;
99     if(l->p0_y > graph->maxy) graph->maxy = l->p0_y;
100     if(l->p0_y < graph->miny) graph->miny = l->p0_y;
101   }
102   if(l->p3_x > graph->maxx) graph->maxx = l->p3_x;
103   if(l->p3_x < graph->minx) graph->minx = l->p3_x;
104   if(l->p3_y > graph->maxy) graph->maxy = l->p3_y;
105   if(l->p3_y < graph->miny) graph->miny = l->p3_y;
106   return 0;
107 }
108
109
110 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) {
111   #ifdef DEBUG
112   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);
113   #endif
114   struct graph_line *l;
115   if((l=(struct graph_line*)malloc(sizeof(struct graph_line))) == NULL) return 2;
116   l->p0_x=p0_x;
117   l->p0_y=p0_y;
118   l->p1_x=p1_x;
119   l->p1_y=p1_y;
120   l->p2_x=p2_x;
121   l->p2_y=p2_y;
122   l->p3_x=p3_x;
123   l->p3_y=p3_y;
124   return graph_add_graph_line(graph, l);
125 }
126     
127 struct cxt {
128   Graph *graph;
129   struct graph_point *p0;
130 };
131     
132 static void linear_interpolate(gpointer data, gpointer user_data) {
133   struct cxt *cxt = user_data;
134   struct graph_point *p3 = data;
135   if(cxt->p0 != NULL) {
136     double p1_x = (p3->x + 2*cxt->p0->x)/3.0;
137     double p1_y = (p3->y + 2*cxt->p0->y)/3.0;
138     double p2_x = (2*p3->x + cxt->p0->x)/3.0;
139     double p2_y = (2*p3->y + cxt->p0->y)/3.0;
140     graph_add_line(cxt->graph, cxt->p0->x, cxt->p0->y, p1_x, p1_y, p2_x, p2_y, p3->x, p3->y);
141   }
142   cxt->p0 = p3;
143 }
144     
145 void graph_add_linear_connectors(Graph* graph) {
146   struct cxt cxt;
147   cxt.graph = graph;
148   cxt.p0 = NULL;
149   g_ptr_array_foreach(graph->points, &linear_interpolate, &cxt);
150 }
151
152 void graph_bezier_linear_to_cubic(double x0, double x3, double *x1, double *x2) {
153   *x1 = (x3 + 2.0*x0)/3.0;
154   *x2 = (2.0*x3 + x0)/3.0;
155 }
156
157 void graph_bezier_linear_to_quadratic(double x0, double x3, double *x1) {
158   *x1 = (x3 + x0)/2.0;
159 }
160
161 void graph_bezier_quadratic_to_cubic(double x0, double x3, double *x1, double *x2) {
162   *x2 = (x3-2.0*(*x1))/3.0;
163   *x1 = (x0-2.0*(*x1))/3.0;
164 }
165
166 void graph_set_xaxis(Graph *g, struct graph_axis *axis) {
167   if(g->xaxis != NULL) {
168     free(g->xaxis);
169   }
170   g->xaxis=axis;
171 }
172 void graph_set_yaxis(Graph *g, struct graph_axis *axis) {
173   if(g->yaxis != NULL) {
174     free(g->yaxis);
175   }
176   g->yaxis=axis;
177 }
178
179 int graph_autoset_xaxis(Graph *g, GString *title) {
180   struct graph_axis *axis;
181   double range_mag;
182   double start_mag;
183   double stop_mag;
184   if(g->points->len == 0) {
185     return 2;
186   }
187   if((axis=(struct graph_axis *)malloc(sizeof(struct graph_axis))) == NULL) {
188     return 1;
189   }
190   if(g->points->len == 1) {
191     axis->major=1;
192     axis->minor=0;
193     axis->subminor=0;
194     axis->major_start=g->minx;
195     axis->major_step=g->maxx;
196   }else{
197     range_mag = round(pow(10, floor(log10(g->maxx-g->minx))));
198     start_mag = copysign(round(pow(10, floor(log10(fabs(g->minx))))), g->minx);
199     stop_mag = copysign(round(pow(10, floor(log10(fabs(g->maxx))))), g->maxx);
200     axis->major=10;
201     axis->minor=0;
202     axis->subminor=0;
203     axis->major_start=start_mag;
204     axis->major_step=range_mag/(axis->major);
205   }
206   axis->title=g_string_new(title->str);
207   graph_set_xaxis(g, axis);
208   return 0;
209 }
210
211 int graph_autoset_yaxis(Graph *g, GString *title) {
212   return 0;
213 }