* Not ideal, but it is functional.
authorJoseph Pingenot <bzrdude@digitasaru.net>
Sun, 27 Jul 2008 01:51:54 +0000 (20:51 -0500)
committerJoseph Pingenot <bzrdude@digitasaru.net>
Sun, 27 Jul 2008 01:51:54 +0000 (20:51 -0500)
graph.c
graphwidget.c
scdataviz.c

diff --git a/graph.c b/graph.c
index fa7029e..df419b9 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -34,6 +34,10 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <stdio.h>
 #include <math.h>
 
+#ifdef DEBUG
+#define DEBUG_AUTOAXIS
+#endif
+
 G_DEFINE_TYPE(Graph, graph, G_TYPE_OBJECT);
 
 static void graph_class_init(GraphClass *klass) {
@@ -176,38 +180,55 @@ void graph_set_yaxis(Graph *g, struct graph_axis *axis) {
   g->yaxis=axis;
 }
 
-int graph_autoset_xaxis(Graph *g, GString *title) {
+struct graph_axis* autoset_axis(Graph* g, GString* title, double min, double max) {
   struct graph_axis *axis;
   double range_mag;
   double start_mag;
   double stop_mag;
   if(g->points->len == 0) {
-    return 2;
+    return NULL;
   }
   if((axis=(struct graph_axis *)malloc(sizeof(struct graph_axis))) == NULL) {
-    return 1;
+    return NULL;
   }
   if(g->points->len == 1) {
     axis->major=1;
     axis->minor=0;
     axis->subminor=0;
-    axis->major_start=g->minx;
-    axis->major_step=g->maxx;
+    axis->major_start=min;
+    axis->major_step=max;
   }else{
-    range_mag = round(pow(10, floor(log10(g->maxx-g->minx))));
-    start_mag = copysign(round(pow(10, floor(log10(fabs(g->minx))))), g->minx);
-    stop_mag = copysign(round(pow(10, floor(log10(fabs(g->maxx))))), g->maxx);
+    range_mag = logb(max - min);
+    if(min == 0) 
+      start_mag = 0;
+    else
+      start_mag = copysign(pow(10, floor(log10(min))), min);
+    if(max == 0) 
+      stop_mag = 0;
+    else
+      stop_mag = copysign(pow(10, ceil(log10(max))), max);
+    if(stop_mag == start_mag) stop_mag *= 10;
     axis->major=10;
     axis->minor=0;
     axis->subminor=0;
     axis->major_start=start_mag;
-    axis->major_step=range_mag/(axis->major);
+    axis->major_step=(stop_mag - start_mag)/(axis->major);
   }
   axis->title=g_string_new(title->str);
-  graph_set_xaxis(g, axis);
+  #ifdef DEBUG_AUTOAXIS
+  fprintf(stderr, "graph_autoset_xaxis: xmin=%g, log10(xmin)=%g, floor=%g, pow=%g round=%g\n", min, log10(min), floor(log10(min)), pow(10, floor(log10(min))), round(pow(10, floor(log10(min)))));
+  fprintf(stderr, "graph_autoset_xaxis: xmax=%g, log10(xmax)=%g, floor=%g, pow=%g round=%g\n", max, log10(max), floor(log10(max)), pow(10, floor(log10(max))), round(pow(10, floor(log10(max)))));
+  fprintf(stderr, "graph_autoset_xaxis: xmin=%g,max=%g start_mag=%g, stop_mag=%g range_mag=%g\n", min, max, start_mag, stop_mag, range_mag);
+  #endif
+  return axis;
+}
+
+int graph_autoset_xaxis(Graph *g, GString *title) {
+  graph_set_xaxis(g, autoset_axis(g, title, g->minx, g->maxx));
   return 0;
 }
 
 int graph_autoset_yaxis(Graph *g, GString *title) {
+  graph_set_yaxis(g, autoset_axis(g, title, g->miny, g->maxy));
   return 0;
 }
index f4a7d70..1c97eee 100644 (file)
@@ -72,7 +72,7 @@ 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
+  #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);
@@ -87,7 +87,7 @@ static void draw_axis(gpointer data, gpointer 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
index e141e26..a243a98 100644 (file)
@@ -54,13 +54,13 @@ static void put_mat_in_graph(gpointer key, gpointer value, gpointer user_data) {
   if(!strcasecmp(mat->name->str, "vacuum")) return;
   if(!strcasecmp(mat->name->str, "pvb")) return;
   double *x, *y;
-  #ifdef DEBUG
+  #ifdef DEBUG_ASSIGNMENT
   fprintf(stderr, "put_mat_in_graph(%s) (x->%s, y->%s): ", (char*)key, propmap->xprop->str, propmap->yprop->str);
   #endif
   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &x, &y);
   if((x != NULL) && (y != NULL)) {
     graph_add_point(propmap->graph, *x, *y, mat->name);
-  #ifdef DEBUG
+  #ifdef DEBUG_ASSIGNMENT
     fprintf(stderr, "added (x->%s=%g, y->%s=%g)\n", propmap->xprop->str, *x, propmap->yprop->str, *y);
   }else{
     fprintf(stderr, "no such properties (x->%s, y->%s)\n", propmap->xprop->str, propmap->yprop->str);
@@ -81,7 +81,7 @@ static void inner_link_materials(gpointer key, gpointer value, gpointer user_dat
   double *p0_x, *p0_y, *p1_x, *p1_y, *p3_x, *p3_y;
   lookup_xy(propmap->mat->properties, propmap->xprop, propmap->yprop, &p0_x, &p0_y);
   lookup_xy(mat->properties, propmap->xprop, propmap->yprop, &p3_x, &p3_y);
-#ifdef DEBUG
+#ifdef DEBUG_ASSIGNMENT
   fprintf(stderr, "%s:%s x=%s y=%s p0(%g,%g) p3(%g,%g)\n", propmap->mat->name->str, mat->name->str, propmap->xprop->str, propmap->yprop->str, (p0_x)?*p0_x:0, (p0_y)?*p0_y:0, (p3_x)?*p3_x:0, (p3_y)?*p3_y:0);
 #endif
   if((p0_x != NULL) && (p0_y != NULL) && (p3_x != NULL) && (p3_y != NULL)) {
@@ -115,11 +115,11 @@ static void inner_link_materials(gpointer key, gpointer value, gpointer user_dat
       #ifdef DEBUG_SHOW_ONLY_BOWED
       free(l);
       return;
-      #endif;
+      #endif
       graph_bezier_linear_to_cubic(l->p0_x, l->p3_x, &(l->p1_x), &(l->p2_x));
       graph_bezier_linear_to_cubic(l->p0_y, l->p3_y, &(l->p1_y), &(l->p2_y));
     }
-    #ifdef DEBUG
+    #ifdef DEBUG_ASSIGNMENT
     fprintf(stderr, "%s:%s p0(%g,%g) p1(%g,%g) p2(%g,%g), p3(%g,%g)\n", propmap->mat->name->str, mat->name->str, l->p0_x, l->p0_y, l->p1_x, l->p1_y, l->p2_x, l->p2_y, l->p3_x, l->p3_y);
     #endif
     graph_add_graph_line(propmap->graph, l);
@@ -163,6 +163,7 @@ int main(int   argc, char *argv[])
     //graph_add_linear_connectors(propmap.graph);
     g_hash_table_foreach(mdb->materials, &link_materials, &propmap);
     graph_autoset_xaxis(propmap.graph, propmap.xprop);
+    graph_autoset_yaxis(propmap.graph, propmap.yprop);
 
     //Connect signals
     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);