* Not ideal, but it is functional.
[scdataviz] / graphwidget.c
index fe209ca..1c97eee 100644 (file)
@@ -44,12 +44,6 @@ struct drawing_context {
   double yoffset;
   double xscaling;
   double yscaling;
-  double bezier_p0_x;
-  double bezier_p0_y;
-  double bezier_p1_x;
-  double bezier_p1_y;
-  int draw_line;
-  int not_first_point;
 };
 
 static void draw_point(gpointer data, gpointer user_data) {
@@ -57,43 +51,43 @@ static void draw_point(gpointer data, gpointer user_data) {
   struct graph_point *pt = data;
   double x = (pt->x + cxt->xoffset)*cxt->xscaling;
   double y = (pt->y + cxt->yoffset)*cxt->yscaling;
-  double bezier_p2_x = (pt->bezier_to_x + cxt->xoffset)*cxt->xscaling;
-  double bezier_p2_y = (pt->bezier_to_y + cxt->yoffset)*cxt->yscaling;
-  #ifdef DEBUG
-  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);
-  #endif
-  /*Draw line to here if we need to.*/
-  if(cxt->draw_line && cxt->not_first_point) {
-    /*Note that the cxt points are already normalized*/
-    //cairo_move_to(cxt->cr, cxt->bezier_p0_x, cxt->bezier_p0_y);
-    cairo_curve_to(cxt->cr, cxt->bezier_p1_x, cxt->bezier_p1_y, bezier_p2_x, bezier_p2_y, x, y);
-  }else{
-    cairo_move_to(cxt->cr, x, y);
-  }    
-  #ifdef DEBUG
-  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);
-  fprintf(stderr, "\t\tx=%g, y=%g, p2_x=%g, p2_y=%g\n", x, y, bezier_p2_x, bezier_p2_y);
-  #endif
+  cairo_move_to(cxt->cr, x, y);
   cairo_arc(cxt->cr, x, y, cxt->radius, 0, 2*M_PI);
   if(pt->label != NULL) {
     cairo_save(cxt->cr);
-    cairo_translate(cxt->cr, 0, 1);
     cairo_scale(cxt->cr, 1, -1);
     cairo_show_text(cxt->cr, pt->label->str);
     cairo_restore(cxt->cr);
   }
-  /*Set up the context for the next point*/
-  cxt->bezier_p0_x = x;
-  cxt->bezier_p0_y = y;
-  cxt->bezier_p1_x = (pt->bezier_from_x + cxt->xoffset)*cxt->xscaling;
-  cxt->bezier_p1_y = (pt->bezier_from_y + cxt->yoffset)*cxt->yscaling;
-  cxt->not_first_point=1;
+}
+
+static void draw_lines(gpointer data, gpointer user_data) {
+  struct drawing_context *cxt = user_data;
+  struct graph_line *l = data;
+  double p0_x = (l->p0_x + cxt->xoffset)*cxt->xscaling;
+  double p0_y = (l->p0_y + cxt->yoffset)*cxt->yscaling;
+  double p1_x = (l->p1_x + cxt->xoffset)*cxt->xscaling;
+  double p1_y = (l->p1_y + cxt->yoffset)*cxt->yscaling;
+  double p2_x = (l->p2_x + cxt->xoffset)*cxt->xscaling;
+  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(gpointer data, gpointer user_data) {
+  struct graph_axis *xs = data;
+  struct drawing_context *cxt = user_data;
 }
 
 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
@@ -101,18 +95,27 @@ 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);
-  /*Signal that the point is the first point.*/
-  cxt.draw_line=0;
-  cxt.not_first_point=0;
+  cxt.xscaling = (gw->graph->points->len == 1)? 1 : (1/(maxx - minx));
+  cxt.yscaling = (gw->graph->points->len == 1)? 1 : (1/(maxy - miny));
+  cxt.xoffset = (gw->graph->points->len == 1)? (-minx/2) : (-minx);
+  cxt.yoffset = (gw->graph->points->len == 1)? (-miny/2) : (-miny);
   #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
@@ -127,10 +130,15 @@ static void draw(GtkWidget *graph, cairo_t *cr) {
   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);
   cairo_stroke(cr);
   cairo_restore(cr);
 }