* Now wit hlabels and line drawing!
[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
39 struct drawing_context {
40   GtkWidget *widget;
41   cairo_t *cr;
42   double radius;
43   double xoffset;
44   double yoffset;
45   double xscaling;
46   double yscaling;
47   double bezier_p0_x;
48   double bezier_p0_y;
49   double bezier_p1_x;
50   double bezier_p1_y;
51   int draw_line;
52   int not_first_point;
53 };
54
55 static void draw_point(gpointer data, gpointer user_data) {
56   struct drawing_context *cxt = user_data;
57   struct graph_point *pt = data;
58   double x = (pt->x + cxt->xoffset)*cxt->xscaling;
59   double y = (pt->y + cxt->yoffset)*cxt->yscaling;
60   double bezier_p2_x = (pt->bezier_to_x + cxt->xoffset)*cxt->xscaling;
61   double bezier_p2_y = (pt->bezier_to_y + cxt->yoffset)*cxt->yscaling;
62   #ifdef DEBUG
63   fprintf(stderr, "\tcxt=(cr=%x, rad=%g, xoffset=%g, yoffset=%g, xscaling=%g, yscaling=%g, p0_x=%g, p0_y=%g, p1_x=%g, p1_y=%g, draw_line=%d, not_first_point=%d)\n", (unsigned int)cxt->cr, cxt->radius, cxt->xoffset, cxt->yoffset, cxt->xscaling, cxt->yscaling, cxt->bezier_p0_x, cxt->bezier_p0_y, cxt->bezier_p1_x, cxt->bezier_p1_y, cxt->draw_line, cxt->not_first_point);
64   #endif
65   /*Draw line to here if we need to.*/
66   if(cxt->draw_line && cxt->not_first_point) {
67     /*Note that the cxt points are already normalized*/
68     //cairo_move_to(cxt->cr, cxt->bezier_p0_x, cxt->bezier_p0_y);
69     cairo_curve_to(cxt->cr, cxt->bezier_p1_x, cxt->bezier_p1_y, bezier_p2_x, bezier_p2_y, x, y);
70   }else{
71     cairo_move_to(cxt->cr, x, y);
72   }    
73   #ifdef DEBUG
74   fprintf(stderr, "\t\tpt=(x=%g, y=%g, label=%s, from_x=%g, from_y=%g, to_x=%g, to_y=%g)\n", pt->x, pt->y, pt->label->str, pt->bezier_from_x, pt->bezier_from_y, pt->bezier_to_x, pt->bezier_to_y);
75   fprintf(stderr, "\t\tx=%g, y=%g, p2_x=%g, p2_y=%g\n", x, y, bezier_p2_x, bezier_p2_y);
76   #endif
77   cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
78   if(pt->label != NULL) {
79     cairo_save(cxt->cr);
80     cairo_translate(cxt->cr, 0, 1);
81     cairo_scale(cxt->cr, 1, -1);
82     cairo_show_text(cxt->cr, pt->label->str);
83     cairo_restore(cxt->cr);
84   }
85   /*Set up the context for the next point*/
86   cxt->bezier_p0_x = x;
87   cxt->bezier_p0_y = y;
88   cxt->bezier_p1_x = (pt->bezier_from_x + cxt->xoffset)*cxt->xscaling;
89   cxt->bezier_p1_y = (pt->bezier_from_y + cxt->yoffset)*cxt->yscaling;
90   cxt->not_first_point=1;
91 }
92
93 static void draw(GtkWidget *graph, cairo_t *cr) {
94   struct drawing_context cxt;
95   GraphWidget *gw = GRAPH_WIDGET(graph);
96   #ifdef DEBUG
97   fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
98   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);
99   #endif
100   double x0=graph->allocation.x;
101   double y0=graph->allocation.y;
102   double height=graph->allocation.height;
103   double width=graph->allocation.width;
104   cxt.widget = graph;
105   cxt.cr = cr;
106   cxt.radius = 0.01;
107   cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx));
108   cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny));
109   cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx);
110   cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny);
111   /*Signal that the point is the first point.*/
112   cxt.draw_line=0;
113   cxt.not_first_point=0;
114   #ifdef DEBUG
115   fprintf(stderr, "minx=%g, maxx=%g, miny=%g, maxy=%g, xscaling=%g, yscaling=%g, xoffset=%g, yoffset=%g\n", gw->graph->minx, gw->graph->maxx, gw->graph->miny, gw->graph->maxy, cxt.xscaling, cxt.yscaling, cxt.xoffset, cxt.yoffset);
116   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
117   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));
118 #endif
119   double offset_height =  ((width>=height)?height*INSET_PERCENT:0);;
120   double offset_width = ((height>=width)?width*INSET_PERCENT:0); 
121   double inset_height = height - offset_height;
122   double inset_width = width - offset_width;
123   cairo_save(cr);
124   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 );
125   cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
126   cairo_set_line_width(cr, 0.005);
127   cairo_select_font_face (cr, "Georgia",
128                           CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
129   cairo_set_font_size (cr, 0.05);
130   g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
131   cairo_set_source_rgb(cr, 1, 1, 1);
132   cairo_fill_preserve(cr);
133   cairo_set_source_rgb(cr, 0, 0, 0);
134   cairo_stroke(cr);
135   cairo_restore(cr);
136 }
137
138 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
139   cairo_t *cr = gdk_cairo_create(graph->window);
140   cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
141   cairo_clip(cr);
142   draw(graph, cr);
143   cairo_destroy(cr);
144   return FALSE;
145 }
146
147 static void graph_widget_class_init(GraphWidgetClass *klass) {
148   GtkWidgetClass *widget_class;
149   widget_class = GTK_WIDGET_CLASS(klass);
150   widget_class->expose_event = graph_widget_expose;
151 }
152
153 static void graph_widget_init(GraphWidget *graph) {
154   /*Get a graph.*/
155   graph->graph = graph_new();
156 }
157
158 GtkWidget *graph_widget_new(void) {
159   return g_object_new(GRAPH_WIDGET_TYPE, NULL);
160 }
161
162 Graph* graph_widget_get_graph(GraphWidget* gw) {
163   return gw->graph;
164 }
165