4 ** Made by (Johnny Q. Hacker)
5 ** Login <solarion@johnathan>
10 Copyright (C) 2008 Joseph Pingenot
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.
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.
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/>.
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
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
32 #include <graphwidget.h>
35 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
37 #define INSET_PERCENT 0.1
38 #define TITLE_PERCENT 0.1
39 #define DEBUG_DRAW_AXIS
41 struct drawing_context {
47 cairo_matrix_t data_scale;
48 cairo_matrix_t data_xlat;
51 static void draw_point(gpointer data, gpointer user_data) {
52 struct drawing_context *cxt = user_data;
53 struct graph_point *pt = data;
55 cairo_transform(cxt->cr, cxt->data_xlat);
56 cairo_transform(cxt->cr, cxt->data_scale);
59 cairo_move_to(cxt->cr, x, y);
60 cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
61 if(pt->label != NULL) {
63 cairo_scale(cxt->cr, 1, -1);
64 cairo_show_text(cxt->cr, pt->label->str);
65 cairo_restore(cxt->cr);
70 static void draw_lines(gpointer data, gpointer user_data) {
71 struct drawing_context *cxt = user_data;
72 struct graph_line *l = data;
74 cairo_transform(cxt->cr, cxt->data_xlat);
75 cairo_transform(cxt->cr, cxt->data_scale);
76 double p0_x = l->p0_x;
77 double p0_y = l->p0_y;
78 double p1_x = l->p1_x;
79 double p1_y = l->p1_y;
80 double p2_x = l->p2_x;
81 double p2_y = l->p2_y;
82 double p3_x = l->p3_x;
83 double p3_y = l->p3_y;
85 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);
87 cairo_move_to(cxt->cr, p0_x, p0_y);
88 cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
89 cairo_restore(cxt->cr);
92 static void draw_axis(struct drawing_context* cxt, struct graph_axis *axis) {
94 cairo_transform(cxt->cr, cxt->data_xlat);
95 cairo_transform(cxt->cr, cxt->data_scale);
96 #ifdef DEBUG_DRAW_AXIS
97 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);
99 cairo_move_to(cxt->cr, 0, 0);
100 cairo_line_to(cxt->cr, 1, 0);
101 register int i, j, k;
102 register double pos, mpos, smpos;
103 for(i=0; i<axis->major; i++) {
104 pos=i*axis->major_step + axis->major_start;
105 cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
106 cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
107 for(k=0; k<axis->subminor; k++) {
108 smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + pos;
109 cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
110 cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
112 for(j=0; j<axis->minor; j++) {
113 mpos=(j+1)*(axis->major_step/(axis->minor+1)) + pos;
114 cairo_move_to(cxt->cr, (mpos + offset)*scaling, 0);
115 cairo_line_to(cxt->cr, (mpos + offset)*scaling, 0.025);
116 for(k=0; k<axis->subminor; k++) {
117 smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + mpos;
118 cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
119 cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
123 pos=i*axis->major_step + axis->major_start;
124 cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
125 cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
126 cairo_restore(cxt->cr);
129 static void draw(GtkWidget *graph, cairo_t *cr) {
130 struct drawing_context cxt;
131 GraphWidget *gw = GRAPH_WIDGET(graph);
133 fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
134 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);
136 double x0=graph->allocation.x;
137 double y0=graph->allocation.y;
138 double height=graph->allocation.height;
139 double width=graph->allocation.width;
140 double minx = gw->graph->minx;
141 double maxx = gw->graph->maxx;
142 double miny = gw->graph->miny;
143 double maxy = gw->graph->maxy;
144 if(gw->graph->xaxis != NULL) {
145 minx = gw->graph->xaxis->major_start;
146 maxx = gw->graph->xaxis->major_start + gw->graph->xaxis->major_step*gw->graph->xaxis->major;
148 if(gw->graph->yaxis != NULL) {
149 miny = gw->graph->yaxis->major_start;
150 maxy = gw->graph->yaxis->major_start + gw->graph->yaxis->major_step*gw->graph->yaxis->major;
155 /*Set up our semi-normalized scaling. The shortest direction range
156 [0, 1]; the other range depends upon the relative sizes.*/
157 double effective_height = height;
158 double effective_width = width;
159 double inset, axis_inset;
160 if((INSET_PERCENT > 0) && (INSET_PERCENT < 1.0)) {
161 inset = ((effective_height > effective_width)?effective_width:effective_height)*INSET_PERCENT;
162 effective_height -= inset;
163 effective_width -= inset;
165 if((gw->graph->xaxis != NULL) && (gw->graph->yaxis != NULL)) {
166 /*key off the smaller size.*/
167 axis_inset = ((effective_height > effective_width)?effective_width:effective_height)*TITLE_PERCENT;
168 effective_height -= axis_inset;
169 effective_width -= axis_inset;
170 }elsif(gw->graph->xaxis != NULL) {
171 effective_height -= effective_height*TITLE_PERCENT;
172 }elsif(gw->graph->yaxis != NULL) {
173 effective_width -= effective_width*TITLE_PERCENT;
175 if(effective_width > effective_height) {
176 cxt.xmax = effective_width/effective_height;
180 cxt.ymax = effective_height/effective_width;
182 /*Convert from data space to the semi-normalized space*/
183 double xscaling = (gw->graph->points->len == 1)? cxt.xmax : (cxt.xmax/(maxx-minx));
184 double yscaling = (gw->graph->points->len == 1)? cxt.ymax : (cxt.ymax/(maxy-miny));
185 double xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
186 double yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
187 cairo_matrix_translate(&(cxt.data_xlat), xoffset, yoffset);
188 cairo_matrix_scale(&(cxt.data_scale), xscaling, yscaling);
190 cairo_translate(cr, effective_width - inset/2, effective_height - inset/2);
191 cairo_scale(cr, effective_width, effective_height);
192 cairo_set_line_width(cr, 0.005);
193 cairo_select_font_face (cr, "Sans Serif",
194 CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
195 cairo_set_font_size (cr, 0.05);
196 if(gw->graph->lines != NULL) {
197 cairo_set_source_rgba(cr, 0, 0, 0, 0.25);
198 g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
201 cairo_set_source_rgb(cr, 1, 1, 1);
202 cairo_fill_preserve(cr);
203 cairo_set_source_rgb(cr, 0, 0, 0);
204 g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
205 if(gw->graph->xaxis != NULL)
206 draw_axis(&cxt, gw->graph->xaxis, cxt.xscaling, cxt.xoffset);
207 if(gw->graph->yaxis != NULL) {
209 cairo_matrix_t matrix = { .xx=0, .xy = 1, .x0 = 0, .yx = 1, .yy = 0, .y0 = 0 };
210 cairo_transform(cr, &matrix);
211 draw_axis(&cxt, gw->graph->yaxis, cxt.yscaling, cxt.yoffset);
218 static gboolean graph_widget_expose(GtkWidget *graph, GdkEventExpose *event) {
219 cairo_t *cr = gdk_cairo_create(graph->window);
220 cairo_rectangle(cr, event->area.x, event->area.y, event->area.width, event->area.height);
227 static void graph_widget_class_init(GraphWidgetClass *klass) {
228 GtkWidgetClass *widget_class;
229 widget_class = GTK_WIDGET_CLASS(klass);
230 widget_class->expose_event = graph_widget_expose;
233 static void graph_widget_init(GraphWidget *graph) {
235 graph->graph = graph_new();
238 GtkWidget *graph_widget_new(void) {
239 return g_object_new(GRAPH_WIDGET_TYPE, NULL);
242 Graph* graph_widget_get_graph(GraphWidget* gw) {