* Seems to be coming along. Not convinced that the bowing conversions are right.
[scdataviz] / graphwidget.c
index 6be20f7..95b9722 100644 (file)
@@ -5,20 +5,22 @@
 ** Login   <solarion@johnathan>
 ** 
 
-Copyright (C) 2008 Joseph Pingenot
 
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Lesser General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Lesser General Public License for more details.
+    Copyright (C) 2008 Joseph Pingenot
 
-You should have received a copy of the GNU Lesser General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU Affero General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU Affero General Public License for more details.
+
+    You should have received a copy of the GNU Affero General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 **Many thanks to Davyd Madeley for the excellent Cairo Widget Tutorial
 **  at http://gnomejournal.org/article/34/writing-a-widget-using-cairo-and-gtk28
@@ -27,26 +29,62 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** Last update Sun May 12 01:17:25 2002 Speed Blue
 */
 
-#include "graphwidget.h"
+#include <graphwidget.h>
+#include <math.h>
 
 G_DEFINE_TYPE(GraphWidget, graph_widget, GTK_TYPE_DRAWING_AREA);
 
+#define INSET_PERCENT 0.1
+
 struct drawing_context {
   GtkWidget *widget;
   cairo_t *cr;
   double radius;
+  double xoffset;
+  double yoffset;
+  double xscaling;
+  double yscaling;
 };
 
 static void draw_point(gpointer data, gpointer user_data) {
-  struct drawing_context *cxt = (struct drawing_context *) user_data;
-  cairo_arc(cxt->cr, ((*double)data)[0], ((*double)data)[1], cxt->radius, 0, 2*M_PI);
+  struct drawing_context *cxt = user_data;
+  struct graph_point *pt = data;
+  double x = (pt->x + cxt->xoffset)*cxt->xscaling;
+  double y = (pt->y + cxt->yoffset)*cxt->yscaling;
+  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_scale(cxt->cr, 1, -1);
+    cairo_show_text(cxt->cr, pt->label->str);
+    cairo_restore(cxt->cr);
+  }
+}
+
+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
+  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(GtkWidget *graph, cairo_t *cr) {
   struct drawing_context cxt;
+  GraphWidget *gw = GRAPH_WIDGET(graph);
   #ifdef DEBUG
-    fprintf(stderr, "draw(%d, %d)\n", graph, 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);
+  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
   double x0=graph->allocation.x;
   double y0=graph->allocation.y;
@@ -54,16 +92,36 @@ static void draw(GtkWidget *graph, cairo_t *cr) {
   double width=graph->allocation.width;
   cxt.widget = graph;
   cxt.cr = cr;
-  cxt.radius = ((height < width)? height : width)*0.01;
+  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);
   #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, "x0=%g, y0=%g, width=%g, height=%g\n", x0, y0, height, width);
-  fprintf(stderr, "move_to(%d, %g->%g, %g->%g)\n", cr, graph_data[0], graph->allocation.x + graph_data[0]*(graph->allocation.width), graph_data[1], graph->allocation.y + graph_data[1]*(graph->allocation.height));
-  #endif
+  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
+  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, y0);
-  cairo_scale(cr, width, height);
-  g_ptr_array_foreach(GRAPH_WIDGET(graph)->graph->points, &draw_point, (gpointer)&cxt);
+  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_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);
+  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);
 }
@@ -88,10 +146,11 @@ static void graph_widget_init(GraphWidget *graph) {
   graph->graph = graph_new();
 }
 
-GtkWidget *graph_widget_new() {
-  return = g_object_new(GRAPH_WIDGET_TYPE, NULL);
+GtkWidget *graph_widget_new(void) {
+  return g_object_new(GRAPH_WIDGET_TYPE, NULL);
 }
 
-Graph* get_graph(GraphWidget* gw) {
+Graph* graph_widget_get_graph(GraphWidget* gw) {
   return gw->graph;
 }
+