4ecc55a9790dc278e6a1f70f609c86e87755f66a
[scdataviz] / graphwidget.c
1 /*
2 ** Graph.c
3 ** 
4 ** Made by (Johnny Q. Hacker)
5 ** Login   <solarion@johnathan>
6 ** 
7
8
9
10     Copyright (C) 2008 Joseph Pingenot
11
12     This program is free software: you can redistribute it and/or modify
13     it under the terms of the GNU Affero General Public License as published by
14     the Free Software Foundation, either version 3 of the License, or
15     (at your option) any later version.
16
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     GNU Affero General Public License for more details.
21
22     You should have received a copy of the GNU Affero General Public License
23     along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
25 **Many thanks to Davyd Madeley for the excellent Cairo Widget Tutorial
26 **  at http://gnomejournal.org/article/34/writing-a-widget-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 <graphwidget.h>
33 #include <math.h>
34
35 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
36
37 #define INSET_PERCENT 0.1
38 #define TITLE_PERCENT 0.15
39 #define DEBUG_DRAW_AXIS
40
41 struct drawing_context {
42   GtkWidget *widget;
43   cairo_t *cr;
44   double radius;
45   double xoffset;
46   double yoffset;
47   double xscaling;
48   double yscaling;
49   /*Semi-normalized maxima*/
50   double xmax;
51   double ymax;
52   cairo_matrix_t data_scale;
53   cairo_matrix_t data_xlat;
54 };
55
56 static void draw_point(gpointer data, gpointer user_data) {
57   struct drawing_context *cxt = user_data;
58   struct graph_point *pt = data;
59   double x = (pt->x + cxt->xoffset)*cxt->xscaling;
60   double y = (pt->y + cxt->yoffset)*cxt->yscaling;
61   cairo_move_to(cxt->cr, x, y);
62   cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
63   if(pt->label != NULL) {
64     cairo_save(cxt->cr);
65     cairo_scale(cxt->cr, 1, -1);
66     cairo_show_text(cxt->cr, pt->label->str);
67     cairo_restore(cxt->cr);
68   }
69 }
70
71 static void draw_lines(gpointer data, gpointer user_data) {
72   struct drawing_context *cxt = user_data;
73   struct graph_line *l = data;
74   double p0_x = (l->p0_x + cxt->xoffset)*cxt->xscaling;
75   double p0_y = (l->p0_y + cxt->yoffset)*cxt->yscaling;
76   double p1_x = (l->p1_x + cxt->xoffset)*cxt->xscaling;
77   double p1_y = (l->p1_y + cxt->yoffset)*cxt->yscaling;
78   double p2_x = (l->p2_x + cxt->xoffset)*cxt->xscaling;
79   double p2_y = (l->p2_y + cxt->yoffset)*cxt->yscaling;
80   double p3_x = (l->p3_x + cxt->xoffset)*cxt->xscaling;
81   double p3_y = (l->p3_y + cxt->yoffset)*cxt->yscaling;
82   #ifdef DEBUG_DRAW
83   fprintf(stderr, "draw_lines: 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", p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
84   #endif
85   cairo_move_to(cxt->cr, p0_x, p0_y);
86   cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
87 }
88
89 static void draw_axis(struct drawing_context* cxt, struct graph_axis *axis, double scaling, double offset, double max) {
90   #ifdef DEBUG_DRAW_AXIS
91   fprintf(stderr, "draw_axis; major_start=%g, major_step=%g, >major=%d, minor=%d, subminor=%d, label=%s\n", axis->major_start, axis->major_step, axis->major, axis->minor, axis->subminor, axis->title->str);
92   #endif
93   cairo_move_to(cxt->cr, 0, 0);
94   cairo_line_to(cxt->cr, max, 0);
95   register int i, j, k;
96   register double pos, mpos, smpos;
97   for(i=0; i<axis->major; i++) {
98     pos=i*axis->major_step + axis->major_start;
99     cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
100     cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
101     for(k=0; k<axis->subminor; k++) {
102       smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + pos;
103       cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
104       cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
105     }
106     for(j=0; j<axis->minor; j++) {
107       mpos=(j+1)*(axis->major_step/(axis->minor+1)) + pos;
108       cairo_move_to(cxt->cr, (mpos + offset)*scaling, 0);
109       cairo_line_to(cxt->cr, (mpos + offset)*scaling, 0.025);
110       for(k=0; k<axis->subminor; k++) {
111         smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + mpos;
112         cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
113         cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
114       }
115     }
116   }
117   pos=i*axis->major_step + axis->major_start;
118   cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
119   cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
120 }
121
122 static void draw(GtkWidget *graph, cairo_t *cr) {
123   struct drawing_context cxt;
124   GraphWidget *gw = GRAPH_WIDGET(graph);
125   #ifdef DEBUG_DRAW
126   fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
127   fprintf(stderr, "allocation.x=%d, allocation.y=%d, allocation.height=%d, allocation.width=%d\n", graph->allocation.x, graph->allocation.y, graph->allocation.height, graph->allocation.width);
128   #endif
129   double x0=graph->allocation.x;
130   double y0=graph->allocation.y;
131   double height=graph->allocation.height;
132   double width=graph->allocation.width;
133   double minx = gw->graph->minx;
134   double maxx = gw->graph->maxx;
135   double miny = gw->graph->miny;
136   double maxy = gw->graph->maxy;
137   if(gw->graph->xaxis != NULL) {
138     minx = gw->graph->xaxis->major_start;
139     maxx = gw->graph->xaxis->major_start + gw->graph->xaxis->major_step*gw->graph->xaxis->major;
140   }
141   if(gw->graph->yaxis != NULL) {
142     miny = gw->graph->yaxis->major_start;
143     maxy = gw->graph->yaxis->major_start + gw->graph->yaxis->major_step*gw->graph->yaxis->major;
144   }
145   cxt.widget = graph;
146   cxt.cr = cr;
147   cxt.radius = 0.01;
148   /*Set up our semi-normalized scaling.  The shortest direction range
149     [0, 1]; the other range depends upon the relative sizes.*/
150   double effective_height = height;
151   double effective_width = width;
152   double inset, axis_inset;
153   if((INSET_PERCENT > 0) && (INSET_PERCENT < 1.0)) {
154     inset = ((effective_height > effective_width)?effective_width:effective_height)*INSET_PERCENT;
155     effective_height -= inset;
156     effective_width -= inset;
157   }
158   if((gw->graph->xaxis != NULL) && (gw->graph->yaxis != NULL)) {
159     /*key off the smaller size.*/
160     axis_inset = ((effective_height > effective_width)?effective_width:effective_height)*TITLE_PERCENT;
161     effective_height -= axis_inset;
162     effective_width -= axis_inset;
163   }else if(gw->graph->xaxis != NULL) {
164     effective_height -= effective_height*TITLE_PERCENT;
165   }else if(gw->graph->yaxis != NULL) {
166     effective_width -= effective_width*TITLE_PERCENT;
167   }
168   if(effective_width > effective_height) {
169     cxt.xmax = effective_width/effective_height;
170     cxt.ymax = 1.0;
171   }else{
172     cxt.xmax = 1.0;
173     cxt.ymax = effective_height/effective_width;
174   }
175   /*Convert from data space to the semi-normalized space*/
176   double xscaling = (gw->graph->points->len == 1)? cxt.xmax : (cxt.xmax/(maxx-minx));
177   double yscaling = (gw->graph->points->len == 1)? cxt.ymax : (cxt.ymax/(maxy-miny));
178   double xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
179   double yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
180   /*Finish up our context information.*/
181   cxt.xscaling = (gw->graph->points->len == 1)? cxt.xmax : (cxt.xmax/(maxx - minx));
182   cxt.yscaling = (gw->graph->points->len == 1)? cxt.ymax : (cxt.ymax/(maxy - miny));
183   cxt.xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
184   cxt.yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
185   cairo_matrix_translate(&(cxt.data_xlat), xoffset, yoffset);
186   cairo_matrix_scale(&(cxt.data_scale), xscaling, yscaling);
187   #ifdef DEBUG
188   fprintf(stderr, "minx=%g, maxx=%g, miny=%g, maxy=%g, xscaling=%g, yscaling=%g, xoffset=%g, yoffset=%g\n", minx, maxx, miny, maxy, cxt.xscaling, cxt.yscaling, cxt.xoffset, cxt.yoffset);
189   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
190   fprintf(stderr, "translate=(%g, %g)\n", x0 + ((width>height)?(width-height)/2.0:0), y0+((width > height)?height:width) + ((height>width)?(height-width)/2.0:0));
191 #endif
192   double offset_height =  ((width>=height)?height*INSET_PERCENT:0);;
193   double offset_width = ((height>=width)?width*INSET_PERCENT:0); 
194   double inset_height = height - offset_height;
195   double inset_width = width - offset_width;
196   cairo_save(cr);
197   cairo_translate(cr, x0 + (width - effective_width) - inset/2, y0 + effective_height + inset/2);
198   cairo_scale(cr, effective_width/cxt.xmax, -effective_height/cxt.ymax);
199   cairo_set_line_width(cr, 0.005);
200   cairo_select_font_face (cr, "Georgia",
201                           CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
202   cairo_set_font_size (cr, 0.05);
203   if(gw->graph->lines != NULL) {
204     cairo_set_source_rgba(cr, 0, 0, 0, 0.25);
205     g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
206     cairo_stroke(cr);
207   }
208   cairo_set_source_rgb(cr, 1, 1, 1);
209   cairo_fill_preserve(cr);
210   cairo_set_source_rgb(cr, 0, 0, 0);
211   g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
212   if(gw->graph->xaxis != NULL) 
213     draw_axis(&cxt, gw->graph->xaxis, cxt.xscaling, cxt.xoffset, cxt.xmax);
214   if(gw->graph->yaxis != NULL) {
215     cairo_save(cr);
216     cairo_matrix_t matrix = { .xx=0, .xy = 1, .x0 = 0, .yx = 1, .yy = 0, .y0 = 0 };
217     cairo_transform(cr, &matrix);
218     draw_axis(&cxt, gw->graph->yaxis, cxt.yscaling, cxt.yoffset, cxt.ymax);
219     cairo_restore(cr);
220   }
221   cairo_stroke(cr);
222   cairo_restore(cr);
223 }
224
225 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
226   cairo_t *cr = gdk_cairo_create(graph->window);
227   cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
228   cairo_clip(cr);
229   draw(graph, cr);
230   cairo_destroy(cr);
231   return FALSE;
232 }
233
234 static void graph_widget_class_init(GraphWidgetClass *klass) {
235   GtkWidgetClass *widget_class;
236   widget_class = GTK_WIDGET_CLASS(klass);
237   widget_class->expose_event = graph_widget_expose;
238 }
239
240 static void graph_widget_init(GraphWidget *graph) {
241   /*Get a graph.*/
242   graph->graph = graph_new();
243 }
244
245 GtkWidget *graph_widget_new(void) {
246   return g_object_new(GRAPH_WIDGET_TYPE, NULL);
247 }
248
249 Graph* graph_widget_get_graph(GraphWidget* gw) {
250   return gw->graph;
251 }
252