* Axes work for what I have implemented thus far.
[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 DEBUG_DRAW_AXIS
39
40 struct drawing_context {
41   GtkWidget *widget;
42   cairo_t *cr;
43   double radius;
44   double xoffset;
45   double yoffset;
46   double xscaling;
47   double yscaling;
48 };
49
50 static void draw_point(gpointer data, gpointer user_data) {
51   struct drawing_context *cxt = user_data;
52   struct graph_point *pt = data;
53   double x = (pt->x + cxt->xoffset)*cxt->xscaling;
54   double y = (pt->y + cxt->yoffset)*cxt->yscaling;
55   cairo_move_to(cxt->cr, x, y);
56   cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
57   if(pt->label != NULL) {
58     cairo_save(cxt->cr);
59     cairo_scale(cxt->cr, 1, -1);
60     cairo_show_text(cxt->cr, pt->label->str);
61     cairo_restore(cxt->cr);
62   }
63 }
64
65 static void draw_lines(gpointer data, gpointer user_data) {
66   struct drawing_context *cxt = user_data;
67   struct graph_line *l = data;
68   double p0_x = (l->p0_x + cxt->xoffset)*cxt->xscaling;
69   double p0_y = (l->p0_y + cxt->yoffset)*cxt->yscaling;
70   double p1_x = (l->p1_x + cxt->xoffset)*cxt->xscaling;
71   double p1_y = (l->p1_y + cxt->yoffset)*cxt->yscaling;
72   double p2_x = (l->p2_x + cxt->xoffset)*cxt->xscaling;
73   double p2_y = (l->p2_y + cxt->yoffset)*cxt->yscaling;
74   double p3_x = (l->p3_x + cxt->xoffset)*cxt->xscaling;
75   double p3_y = (l->p3_y + cxt->yoffset)*cxt->yscaling;
76   #ifdef DEBUG_DRAW
77   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);
78   #endif
79   cairo_move_to(cxt->cr, p0_x, p0_y);
80   cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
81 }
82
83 static void draw_axis(struct drawing_context* cxt, struct graph_axis *axis, double scaling, double offset) {
84   #ifdef DEBUG_DRAW_AXIS
85   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);
86   #endif
87   cairo_move_to(cxt->cr, 0, 0);
88   cairo_line_to(cxt->cr, 1, 0);
89   register int i, j, k;
90   register double pos, mpos, smpos;
91   for(i=0; i<axis->major; i++) {
92     pos=i*axis->major_step + axis->major_start;
93     cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
94     cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
95     for(k=0; k<axis->subminor; k++) {
96       smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + pos;
97       cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
98       cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
99     }
100     for(j=0; j<axis->minor; j++) {
101       mpos=(j+1)*(axis->major_step/(axis->minor+1)) + pos;
102       cairo_move_to(cxt->cr, (mpos + offset)*scaling, 0);
103       cairo_line_to(cxt->cr, (mpos + offset)*scaling, 0.025);
104       for(k=0; k<axis->subminor; k++) {
105         smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + mpos;
106         cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
107         cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
108       }
109     }
110   }
111   pos=i*axis->major_step + axis->major_start;
112   cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
113   cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
114 }
115
116 static void draw(GtkWidget *graph, cairo_t *cr) {
117   struct drawing_context cxt;
118   GraphWidget *gw = GRAPH_WIDGET(graph);
119   #ifdef DEBUG_DRAW
120   fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
121   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);
122   #endif
123   double x0=graph->allocation.x;
124   double y0=graph->allocation.y;
125   double height=graph->allocation.height;
126   double width=graph->allocation.width;
127   double minx = gw->graph->minx;
128   double maxx = gw->graph->maxx;
129   double miny = gw->graph->miny;
130   double maxy = gw->graph->maxy;
131   if(gw->graph->xaxis != NULL) {
132     minx = gw->graph->xaxis->major_start;
133     maxx = gw->graph->xaxis->major_start + gw->graph->xaxis->major_step*gw->graph->xaxis->major;
134   }
135   if(gw->graph->yaxis != NULL) {
136     miny = gw->graph->yaxis->major_start;
137     maxy = gw->graph->yaxis->major_start + gw->graph->yaxis->major_step*gw->graph->yaxis->major;
138   }
139   cxt.widget = graph;
140   cxt.cr = cr;
141   cxt.radius = 0.01;
142   cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(maxx - minx));
143   cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(maxy - miny));
144   cxt.xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
145   cxt.yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
146   #ifdef DEBUG
147   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);
148   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
149   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));
150 #endif
151   double offset_height =  ((width>=height)?height*INSET_PERCENT:0);;
152   double offset_width = ((height>=width)?width*INSET_PERCENT:0); 
153   double inset_height = height - offset_height;
154   double inset_width = width - offset_width;
155   cairo_save(cr);
156   cairo_translate(cr, x0 + ((inset_width>inset_height)?(inset_width-inset_height)/2.0:0) + offset_width/2, y0+((inset_width > inset_height)?inset_height:inset_width) + ((inset_width>=inset_height)?0:(inset_height-inset_width)/2.0) + offset_height/2 );
157   cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
158   cairo_set_line_width(cr, 0.005);
159   cairo_select_font_face (cr, "Georgia",
160                           CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
161   cairo_set_font_size (cr, 0.05);
162   if(gw->graph->lines != NULL) {
163     cairo_set_source_rgba(cr, 0, 0, 0, 0.25);
164     g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
165     cairo_stroke(cr);
166   }
167   cairo_set_source_rgb(cr, 1, 1, 1);
168   cairo_fill_preserve(cr);
169   cairo_set_source_rgb(cr, 0, 0, 0);
170   g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
171   if(gw->graph->xaxis != NULL) 
172     draw_axis(&cxt, gw->graph->xaxis, cxt.xscaling, cxt.xoffset);
173   if(gw->graph->yaxis != NULL) {
174     cairo_save(cr);
175     cairo_matrix_t matrix = { .xx=0, .xy = 1, .x0 = 0, .yx = 1, .yy = 0, .y0 = 0 };
176     cairo_transform(cr, &matrix);
177     draw_axis(&cxt, gw->graph->yaxis, cxt.yscaling, cxt.yoffset);
178     cairo_restore(cr);
179   }
180   cairo_stroke(cr);
181   cairo_restore(cr);
182 }
183
184 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
185   cairo_t *cr = gdk_cairo_create(graph->window);
186   cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
187   cairo_clip(cr);
188   draw(graph, cr);
189   cairo_destroy(cr);
190   return FALSE;
191 }
192
193 static void graph_widget_class_init(GraphWidgetClass *klass) {
194   GtkWidgetClass *widget_class;
195   widget_class = GTK_WIDGET_CLASS(klass);
196   widget_class->expose_event = graph_widget_expose;
197 }
198
199 static void graph_widget_init(GraphWidget *graph) {
200   /*Get a graph.*/
201   graph->graph = graph_new();
202 }
203
204 GtkWidget *graph_widget_new(void) {
205   return g_object_new(GRAPH_WIDGET_TYPE, NULL);
206 }
207
208 Graph* graph_widget_get_graph(GraphWidget* gw) {
209   return gw->graph;
210 }
211