* graphwidget.c: fixed a bug where we'd divide by 0 if min and max x and y were the...
[scdataviz] / graphwidget.c
index 7dbfbbd..beba3c9 100644 (file)
@@ -26,7 +26,7 @@
 **  at http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28
 
 ** Started on  Thu Jul 17 10:51:32 2008 Johnny Q. Hacker
-** Last update Sun May 12 01:17:25 2002 Speed Blue
+** Last update Thu Oct  8 17:13:50 2009 Johnny Q. Hacker
 */
 
 #include <graphwidget.h>
@@ -35,6 +35,8 @@
 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
 
 #define INSET_PERCENT 0.1
+#define TITLE_PERCENT 0.15
+#define DEBUG_DRAW_AXIS
 
 struct drawing_context {
   GtkWidget *widget;
@@ -44,6 +46,11 @@ struct drawing_context {
   double yoffset;
   double xscaling;
   double yscaling;
+  /*Semi-normalized maxima*/
+  double xmax;
+  double ymax;
+  cairo_matrix_t data_scale;
+  cairo_matrix_t data_xlat;
 };
 
 static void draw_point(gpointer data, gpointer user_data) {
@@ -72,14 +79,50 @@ static void draw_lines(gpointer data, gpointer user_data) {
   double p2_y = (l->p2_y + cxt->yoffset)*cxt->yscaling;
   double p3_x = (l->p3_x + cxt->xoffset)*cxt->xscaling;
   double p3_y = (l->p3_y + cxt->yoffset)*cxt->yscaling;
+  #ifdef DEBUG_DRAW
+  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);
+  #endif
   cairo_move_to(cxt->cr, p0_x, p0_y);
   cairo_curve_to(cxt->cr, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y);
 }
 
+static void draw_axis(struct drawing_context* cxt, struct graph_axis *axis, double scaling, double offset, double max) {
+  #ifdef DEBUG_DRAW_AXIS
+  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);
+  #endif
+  cairo_move_to(cxt->cr, 0, 0);
+  cairo_line_to(cxt->cr, max, 0);
+  register int i, j, k;
+  register double pos, mpos, smpos;
+  for(i=0; i<axis->major; i++) {
+    pos=i*axis->major_step + axis->major_start;
+    cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
+    cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
+    for(k=0; k<axis->subminor; k++) {
+      smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + pos;
+      cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
+      cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
+    }
+    for(j=0; j<axis->minor; j++) {
+      mpos=(j+1)*(axis->major_step/(axis->minor+1)) + pos;
+      cairo_move_to(cxt->cr, (mpos + offset)*scaling, 0);
+      cairo_line_to(cxt->cr, (mpos + offset)*scaling, 0.025);
+      for(k=0; k<axis->subminor; k++) {
+       smpos=(k+1)*(axis->major_step/(axis->minor+1)/(axis->subminor+1)) + mpos;
+       cairo_move_to(cxt->cr, (smpos + offset)*scaling, 0);
+       cairo_line_to(cxt->cr, (smpos + offset)*scaling, 0.0125);
+      }
+    }
+  }
+  pos=i*axis->major_step + axis->major_start;
+  cairo_move_to(cxt->cr, (pos + offset)*scaling, 0);
+  cairo_line_to(cxt->cr, (pos + offset)*scaling, 0.05);
+}
+
 static void draw(GtkWidget *graph, cairo_t *cr) {
   struct drawing_context cxt;
   GraphWidget *gw = GRAPH_WIDGET(graph);
-  #ifdef DEBUG
+  #ifdef DEBUG_DRAW
   fprintf(stderr, "draw(%d, %d)\n", (int)graph, (int)cr);
   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);
   #endif
@@ -87,33 +130,94 @@ static void draw(GtkWidget *graph, cairo_t *cr) {
   double y0=graph->allocation.y;
   double height=graph->allocation.height;
   double width=graph->allocation.width;
+  double minx = gw->graph->minx;
+  double maxx = gw->graph->maxx;
+  double miny = gw->graph->miny;
+  double maxy = gw->graph->maxy;
+  if(gw->graph->xaxis != NULL) {
+    minx = gw->graph->xaxis->major_start;
+    maxx = gw->graph->xaxis->major_start + gw->graph->xaxis->major_step*gw->graph->xaxis->major;
+  }
+  if(gw->graph->yaxis != NULL) {
+    miny = gw->graph->yaxis->major_start;
+    maxy = gw->graph->yaxis->major_start + gw->graph->yaxis->major_step*gw->graph->yaxis->major;
+  }
   cxt.widget = graph;
   cxt.cr = cr;
   cxt.radius = 0.01;
-  cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxx - gw->graph->minx));
-  cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(gw->graph->maxy - gw->graph->miny));
-  cxt.xoffset = (gw->graph->points->len == 1)? (-gw->graph->minx/2) : (-gw->graph->minx);
-  cxt.yoffset = (gw->graph->points->len == 1)? (-gw->graph->miny/2) : (-gw->graph->miny);
+  /*Set up our semi-normalized scaling.  The shortest direction range
+    [0, 1]; the other range depends upon the relative sizes.*/
+  double effective_height = height;
+  double effective_width = width;
+  double inset, axis_inset;
+  if((INSET_PERCENT > 0) && (INSET_PERCENT < 1.0)) {
+    inset = ((effective_height > effective_width)?effective_width:effective_height)*INSET_PERCENT;
+    effective_height -= inset;
+    effective_width -= inset;
+  }
+  if((gw->graph->xaxis != NULL) && (gw->graph->yaxis != NULL)) {
+    /*key off the smaller size.*/
+    axis_inset = ((effective_height > effective_width)?effective_width:effective_height)*TITLE_PERCENT;
+    effective_height -= axis_inset;
+    effective_width -= axis_inset;
+  }else if(gw->graph->xaxis != NULL) {
+    effective_height -= effective_height*TITLE_PERCENT;
+  }else if(gw->graph->yaxis != NULL) {
+    effective_width -= effective_width*TITLE_PERCENT;
+  }
+  if(effective_width > effective_height) {
+    cxt.xmax = effective_width/effective_height;
+    cxt.ymax = 1.0;
+  }else{
+    cxt.xmax = 1.0;
+    cxt.ymax = effective_height/effective_width;
+  }
+  /*Convert from data space to the semi-normalized space*/
+  double xscaling = (gw->graph->points->len == 1)? cxt.xmax : (cxt.xmax/(maxx-minx));
+  double yscaling = (gw->graph->points->len == 1)? cxt.ymax : (cxt.ymax/(maxy-miny));
+  double xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
+  double yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
+  /*Finish up our context information.*/
+  cxt.xscaling = (gw->graph->points->len == 1)? cxt.xmax : (cxt.xmax/(((maxx - minx)==0)?1:(maxx-minx)));
+  cxt.yscaling = (gw->graph->points->len == 1)? cxt.ymax : (cxt.ymax/(((maxy - miny)==0)?1:(maxy-miny)));
+  cxt.xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
+  cxt.yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
+  cairo_matrix_translate(&(cxt.data_xlat), xoffset, yoffset);
+  cairo_matrix_scale(&(cxt.data_scale), xscaling, yscaling);
   #ifdef DEBUG
-  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);
+  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);
   fprintf(stderr, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
   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));
-#endif
+  #endif
   double offset_height =  ((width>=height)?height*INSET_PERCENT:0);;
   double offset_width = ((height>=width)?width*INSET_PERCENT:0); 
   double inset_height = height - offset_height;
   double inset_width = width - offset_width;
   cairo_save(cr);
-  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 );
-  cairo_scale(cr, (inset_width > inset_height)?inset_height:inset_width, (inset_width > inset_height)?-inset_height:-inset_width);
+  cairo_translate(cr, x0 + (width - effective_width) - inset/2, y0 + effective_height + inset/2);
+  cairo_scale(cr, effective_width/cxt.xmax, -effective_height/cxt.ymax);
   cairo_set_line_width(cr, 0.005);
   cairo_select_font_face (cr, "Georgia",
                          CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
   cairo_set_font_size (cr, 0.05);
-  g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
+  if(gw->graph->lines != NULL) {
+    cairo_set_source_rgba(cr, 0, 0, 0, 0.25);
+    g_ptr_array_foreach(gw->graph->lines, &draw_lines, (gpointer)&cxt);
+    cairo_stroke(cr);
+  }
   cairo_set_source_rgb(cr, 1, 1, 1);
   cairo_fill_preserve(cr);
   cairo_set_source_rgb(cr, 0, 0, 0);
+  g_ptr_array_foreach(gw->graph->points, &draw_point, (gpointer)&cxt);
+  if(gw->graph->xaxis != NULL) 
+    draw_axis(&cxt, gw->graph->xaxis, cxt.xscaling, cxt.xoffset, cxt.xmax);
+  if(gw->graph->yaxis != NULL) {
+    cairo_save(cr);
+    cairo_matrix_t matrix = { .xx=0, .xy = 1, .x0 = 0, .yx = 1, .yy = 0, .y0 = 0 };
+    cairo_transform(cr, &matrix);
+    draw_axis(&cxt, gw->graph->yaxis, cxt.yscaling, cxt.yoffset, cxt.ymax);
+    cairo_restore(cr);
+  }
   cairo_stroke(cr);
   cairo_restore(cr);
 }