[genius] Mon Dec 10 16:20:53 2012 Jiri (George) Lebl <jirka 5z com>



commit eea4e20f317d654ce71ed200ec1abd01a43865f1
Author: Jiri (George) Lebl <jirka 5z com>
Date:   Mon Dec 10 17:48:37 2012 -0600

    Mon Dec 10 16:20:53 2012  Jiri (George) Lebl <jirka 5z com>
    
    	* src/graphing.c: when number of digits of labels is too much
    	  use the scientific notation.  Avoids ugly huge numbers.
    	  Also make the graph slightly bigger (700x500) and in lineplot mode
    	  make the margins bigger so that we don't lose any digits on axes
    	  when zooming out or in.  Also fixup labels for gradient.  Gradient
    	  is now done only for the range of the function not the whole range
    	  that makes it more useful.
    
    	* gtkextra/gtkplot.c, gtkextra/gtkplot3d.c, gtkextra/gtkplotsurface.c:
    	  minor cleanup and fix crash when labels too big.  Also fix gradient
    	  label style settings, and zero out data we just freed.

 ChangeLog                 |   14 ++++++
 NEWS                      |    7 +++
 gtkextra/gtkplot.c        |   73 ++++++++++++++---------------
 gtkextra/gtkplot3d.c      |    4 +-
 gtkextra/gtkplotdata.c    |    4 +-
 gtkextra/gtkplotsurface.c |   14 +++++-
 src/graphing.c            |  114 +++++++++++++++++++++++++++++++-------------
 7 files changed, 155 insertions(+), 75 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 81e947b..488ee52 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+Mon Dec 10 16:20:53 2012  Jiri (George) Lebl <jirka 5z com>
+
+	* src/graphing.c: when number of digits of labels is too much
+	  use the scientific notation.  Avoids ugly huge numbers.
+	  Also make the graph slightly bigger (700x500) and in lineplot mode
+	  make the margins bigger so that we don't lose any digits on axes
+	  when zooming out or in.  Also fixup labels for gradient.  Gradient
+	  is now done only for the range of the function not the whole range
+	  that makes it more useful.
+
+	* gtkextra/gtkplot.c, gtkextra/gtkplot3d.c, gtkextra/gtkplotsurface.c:
+	  minor cleanup and fix crash when labels too big.  Also fix gradient
+	  label style settings, and zero out data we just freed.
+
 Fri Dec 07 21:36:50 2012  Jiri (George) Lebl <jirka 5z com>
 
 	* src/graphing.c, src/gnome-genius.c: kill our own overwrite dialog
diff --git a/NEWS b/NEWS
index d69cafe..db4f780 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,12 @@ Changes to 1.0.16
 * Add ExportPlot function to export the current contents of the plot window
   to a file from GEL
 * Add sinc, BesselJ0, BesselJ1, BesselJn, BesselY0, BesselY1, BesselYn functions
+* Plot window slightly bigger (700x500 now) and there are wider
+  side margins in the lineplot (2d) version to make tick labels always fit,
+* In both 2D and 3D plots, tick labels now use scientific notation when
+  needed to avoid ugly labels
+* In surface plot the gradient always shows only the range of the function, so
+  color is more useful when zoomed out.
 * Simpler output when typing "help foo" when foo is neither defined nor
   documented.
 * Handle wider matrices than 2^15 columns in expansion
@@ -15,6 +21,7 @@ Changes to 1.0.16
   (should never happen, but ...)
 * Fix LinearRecursiveSequence and allow it to take vector for n
 * Fix crash on uninitialized variables in conjugate transpose
+* Fix crash on extreme zoom out or zoom in of a graph
 * Fix derivatives of Im and Re
 * Fix file chooser dialogs not starting in current directory
 * Avoid double error about uninitialized variables
diff --git a/gtkextra/gtkplot.c b/gtkextra/gtkplot.c
index c6fbb27..f416f0c 100644
--- a/gtkextra/gtkplot.c
+++ b/gtkextra/gtkplot.c
@@ -2611,9 +2611,9 @@ gtk_plot_draw_labels(GtkPlot *plot,
               gchar **array;
               array = gtk_plot_array_get_string(axis->tick_labels);
               if(array && n < gtk_plot_array_get_size(axis->tick_labels) && array[n]) {
-                g_snprintf(label, 100, "%s", array[n++]);
+                g_snprintf(label, LABEL_MAX_LENGTH, "%s", array[n++]);
               } else {
-                g_snprintf(label, 100, " ");
+                g_snprintf(label, LABEL_MAX_LENGTH, " ");
               }
             } else {
               gtk_plot_axis_parse_label(axis, x_tick, axis->label_precision, axis->label_style, label);
@@ -2622,12 +2622,12 @@ gtk_plot_draw_labels(GtkPlot *plot,
         }
 
         if(axis->labels_prefix){
-          g_snprintf(new_label, 100, "%s%s", axis->labels_prefix, label);
-          g_snprintf(label, 100, "%s", new_label);
+          g_snprintf(new_label, LABEL_MAX_LENGTH, "%s%s", axis->labels_prefix, label);
+          g_snprintf(label, LABEL_MAX_LENGTH, "%s", new_label);
         }
         if(axis->labels_suffix){
-          g_snprintf(new_label, 100, "%s%s", label, axis->labels_suffix);
-          g_snprintf(label, 100, "%s", new_label);
+          g_snprintf(new_label, LABEL_MAX_LENGTH, "%s%s", label, axis->labels_suffix);
+          g_snprintf(label, LABEL_MAX_LENGTH, "%s", new_label);
         }
   
         tick.text = label;
@@ -2982,50 +2982,49 @@ gtk_plot_parse_label(GtkPlotAxis *axis, gdouble val, gint precision, gint style,
 {
   gdouble auxval;
   gint intspace = 0;
-  gint power;
-  gfloat v;
+  gint power = 0.0;
+  gfloat v = 0.0;
   GtkPlotScale scale = axis->ticks.scale;
 
   auxval = fabs(val);
 
-  power = 0.0;
-  if(auxval != 0.0)
-       power = (gint)log10(auxval);
-
-  v = val / pow(10.0, power); 
-  if(fabs(v) < 1.0 && v != 0.0){
-     v *= 10.0;
-     power -= 1;
-  }
-  if(fabs(v) >= 10.0){
-     v /= 10.0;
-     power += 1;
-  }
-/*
-  if(power < -12){
-     power = 0;
-     v = 0.0f;
-  }
-*/
-
-  if(auxval > 1)
-    intspace = (gint)log10(auxval);
-
-
   switch(style){
     case GTK_PLOT_LABEL_EXP:    
-      sprintf (label, "%*.*E", 1, precision, val);
+      g_snprintf (label, LABEL_MAX_LENGTH, "%*.*E", 1, precision, val);
       break;
     case GTK_PLOT_LABEL_POW:    
+      power = 0.0;
+      if(auxval != 0.0)
+        power = (gint)log10(auxval);
+
+      v = val / pow(10.0, power); 
+      if(fabs(v) < 1.0 && v != 0.0){
+        v *= 10.0;
+        power -= 1;
+      }
+      if(fabs(v) >= 10.0){
+        v /= 10.0;
+	power += 1;
+      }
+/*
+      if(power < -12){
+         power = 0;
+         v = 0.0f;
+      }
+*/
       if(scale == GTK_PLOT_SCALE_LOG10)
-        sprintf (label, "10\\S%i", power);
+        g_snprintf (label, LABEL_MAX_LENGTH, "10\\S%i", power);
       else
-        sprintf (label, "%*.*f\\4x\\N10\\S%i", 1, precision, v, power);
+        g_snprintf (label, LABEL_MAX_LENGTH, "%*.*f\\4x\\N10\\S%i", 1, precision, v, power);
       break;
     case GTK_PLOT_LABEL_FLOAT:
     default:
-      if(fabs(val) < pow(10,-precision)) val = 0.0f;
-      sprintf (label, "%*.*f", intspace, precision, val);
+
+      if (auxval > 1)
+        intspace = (gint)log10(auxval);
+      else if (auxval < pow(10,-precision))
+	val = 0.0f;
+      g_snprintf (label, LABEL_MAX_LENGTH, "%*.*f", intspace, precision, val);
   }
 
 }
diff --git a/gtkextra/gtkplot3d.c b/gtkextra/gtkplot3d.c
index 8b4c954..5c983fd 100644
--- a/gtkextra/gtkplot3d.c
+++ b/gtkextra/gtkplot3d.c
@@ -31,6 +31,8 @@
 #define DEFAULT_WIDTH 420
 #define DEFAULT_HEIGHT 340
 #define DEFAULT_FONT_HEIGHT 10 
+/* This should be same as in gtkplot.c */
+#define LABEL_MAX_LENGTH 100
 
 #ifndef PI
 #define PI 3.141592653589793238462643383279502884197
@@ -1720,7 +1722,7 @@ gtk_plot3d_draw_labels(GtkPlot3D *plot,
   GtkWidget *widget;
   GtkPlotPC *pc;
   GtkPlotText title, tick;
-  gchar label[100];
+  gchar label[LABEL_MAX_LENGTH];
   gdouble tick_value;
   gdouble xx;
   gint text_height, text_width, ascent, descent;
diff --git a/gtkextra/gtkplotdata.c b/gtkextra/gtkplotdata.c
index a0ed325..9ed6fd7 100644
--- a/gtkextra/gtkplotdata.c
+++ b/gtkextra/gtkplotdata.c
@@ -5254,8 +5254,8 @@ gtk_plot_data_gradient_set_style        (GtkPlotData *data,
                                          GtkPlotLabelStyle style,
                                          gint precision)
 {
-  data->legends_style = style,
-  data->legends_precision = style;
+  data->gradient->label_style = style;
+  data->gradient->label_precision = precision;
 }
 
 void            gtk_plot_data_gradient_set_scale        (GtkPlotData *data,
diff --git a/gtkextra/gtkplotsurface.c b/gtkextra/gtkplotsurface.c
index 6d1f243..64ae99e 100644
--- a/gtkextra/gtkplotsurface.c
+++ b/gtkextra/gtkplotsurface.c
@@ -469,7 +469,11 @@ gtk_plot_surface_update_range (GtkPlotData *data)
      gtk_plot_data_set_z(GTK_PLOT_DATA(surface), fz);
      gtk_plot_data_set_numpoints(GTK_PLOT_DATA(surface), npoints);
 
-     gtk_plot_surface_build_mesh(surface);
+     gtk_plot_surface_real_build_mesh(surface);
+
+     gtk_plot_data_set_x(GTK_PLOT_DATA(surface), NULL);
+     gtk_plot_data_set_y(GTK_PLOT_DATA(surface), NULL);
+     gtk_plot_data_set_z(GTK_PLOT_DATA(surface), NULL);
 
      g_free(fx);
      g_free(fy);
@@ -1443,6 +1447,10 @@ gtk_plot_surface_build_mesh(GtkPlotSurface *surface)
      gtk_plot_data_set_numpoints(data, npoints);
      gtk_plot_surface_real_build_mesh(surface);
 
+     gtk_plot_data_set_x(data, NULL);
+     gtk_plot_data_set_y(data, NULL);
+     gtk_plot_data_set_z(data, NULL);
+
      g_free(fx);
      g_free(fy);
      g_free(fz);
@@ -1484,6 +1492,10 @@ gtk_plot_surface_build_mesh(GtkPlotSurface *surface)
 
      gtk_plot_surface_real_build_mesh(surface);
 
+     gtk_plot_data_set_x(GTK_PLOT_DATA(surface), NULL);
+     gtk_plot_data_set_y(GTK_PLOT_DATA(surface), NULL);
+     gtk_plot_data_set_z(GTK_PLOT_DATA(surface), NULL);
+
      g_free(fx);
      g_free(fy);
      g_free(fz);
diff --git a/src/graphing.c b/src/graphing.c
index 8c4756a..6189b99 100644
--- a/src/graphing.c
+++ b/src/graphing.c
@@ -328,13 +328,14 @@ create_range_spinboxes (const char *title, GtkWidget **titlew,
 static void set_lineplot_labels (void);
 static void set_surface_labels (void);
 
-#define WIDTH 640
-#define HEIGHT 480
+#define WIDTH 700
+#define HEIGHT 500
 #define ASPECT ((double)HEIGHT/(double)WIDTH)
 
 #define PROPORTION 0.85
 #define PROPORTION3D 0.80
-#define PROPORTION_OFFSET 0.075
+#define PROPORTION_OFFSETX 0.1
+#define PROPORTION_OFFSETY 0.075
 #define PROPORTION3D_OFFSET 0.1
 
 #include "funclibhelper.cP"
@@ -1572,22 +1573,22 @@ line_plot_move_about (void)
 		/* move plot out of the way if we are in parametric mode and
 		 * there is a legend */
 		gtk_plot_move (GTK_PLOT (line_plot),
-			       PROPORTION_OFFSET,
-			       PROPORTION_OFFSET);
+			       PROPORTION_OFFSETX,
+			       PROPORTION_OFFSETY);
 		gtk_plot_resize (GTK_PLOT (line_plot),
-				 1.0-2*PROPORTION_OFFSET,
-				 1.0-2*PROPORTION_OFFSET-0.05);
+				 1.0-2*PROPORTION_OFFSETX,
+				 1.0-2*PROPORTION_OFFSETY-0.05);
 
 		gtk_plot_legends_move (GTK_PLOT (line_plot),
 				       0.0,
 				       1.07);
 	} else {
 		gtk_plot_move (GTK_PLOT (line_plot),
-			       PROPORTION_OFFSET,
-			       PROPORTION_OFFSET);
+			       PROPORTION_OFFSETX,
+			       PROPORTION_OFFSETY);
 		gtk_plot_resize (GTK_PLOT (line_plot),
-				 1.0-2*PROPORTION_OFFSET,
-				 1.0-2*PROPORTION_OFFSET);
+				 1.0-2*PROPORTION_OFFSETX,
+				 1.0-2*PROPORTION_OFFSETY);
 		gtk_plot_legends_move (GTK_PLOT (line_plot), 0.80, 0.05);
 	}
 }
@@ -1607,10 +1608,10 @@ add_line_plot (void)
 	plot_child = gtk_plot_canvas_plot_new (GTK_PLOT (line_plot));
 	gtk_plot_canvas_put_child (GTK_PLOT_CANVAS (plot_canvas),
 				   plot_child,
-				   PROPORTION_OFFSET,
-				   PROPORTION_OFFSET,
-				   1.0-PROPORTION_OFFSET,
-				   1.0-PROPORTION_OFFSET);
+				   PROPORTION_OFFSETX,
+				   PROPORTION_OFFSETY,
+				   1.0-PROPORTION_OFFSETX,
+				   1.0-PROPORTION_OFFSETY);
 
 	top = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_TOP);
 	right = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_RIGHT);
@@ -2123,7 +2124,8 @@ clear_graph (void)
 }
 
 static void
-get_ticks (double start, double end, double *tick, int *prec)
+get_ticks (double start, double end, double *tick, int *prec,
+	   int *style)
 {
 	int incs;
 	double len = end-start;
@@ -2162,9 +2164,17 @@ get_ticks (double start, double end, double *tick, int *prec)
 		}
 	}
 
-	if (tickprec + extra_prec <= 0) {
+	if (tickprec + extra_prec <= -6) {
+		*prec = 1;
+		*style = GTK_PLOT_LABEL_EXP;
+	} else if (tickprec + extra_prec <= 0) {
+		*style = GTK_PLOT_LABEL_FLOAT;
 		*prec = 0;
+	} else if (tickprec + extra_prec >= 6) {
+		*prec = 1;
+		*style = GTK_PLOT_LABEL_EXP;
 	} else {
+		*style = GTK_PLOT_LABEL_FLOAT;
 		*prec = tickprec + extra_prec;
 	}
 }
@@ -2172,13 +2182,13 @@ get_ticks (double start, double end, double *tick, int *prec)
 static void
 plot_setup_axis (void)
 {
-	int xprec, yprec;
+	int xprec, yprec, xstyle, ystyle;
 	double xtick, ytick;
 	GtkPlotAxis *axis;
 	GdkColor gray;
 
-	get_ticks (plotx1, plotx2, &xtick, &xprec);
-	get_ticks (ploty1, ploty2, &ytick, &yprec);
+	get_ticks (plotx1, plotx2, &xtick, &xprec, &xstyle);
+	get_ticks (ploty1, ploty2, &ytick, &yprec, &ystyle);
 
 	gtk_plot_freeze (GTK_PLOT (line_plot));
 
@@ -2221,7 +2231,7 @@ plot_setup_axis (void)
 
 	axis = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_TOP);
 	gtk_plot_axis_set_labels_style (axis,
-					GTK_PLOT_LABEL_FLOAT,
+					xstyle /* style */,
 					xprec /* precision */);
 	gtk_plot_axis_show_labels (axis, lineplot_draw_labels ?
 				      GTK_PLOT_LABEL_OUT :
@@ -2229,7 +2239,7 @@ plot_setup_axis (void)
 
 	axis = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_BOTTOM);
 	gtk_plot_axis_set_labels_style (axis,
-					GTK_PLOT_LABEL_FLOAT,
+					xstyle /* style */,
 					xprec /* precision */);
 	gtk_plot_axis_show_labels (axis, lineplot_draw_labels ?
 				      GTK_PLOT_LABEL_OUT :
@@ -2237,7 +2247,7 @@ plot_setup_axis (void)
 
 	axis = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_LEFT);
 	gtk_plot_axis_set_labels_style (axis,
-					GTK_PLOT_LABEL_FLOAT,
+					ystyle /* style */,
 					yprec /* precision */);
 	gtk_plot_axis_show_labels (axis, lineplot_draw_labels ?
 				      GTK_PLOT_LABEL_OUT :
@@ -2245,7 +2255,7 @@ plot_setup_axis (void)
 
 	axis = gtk_plot_get_axis (GTK_PLOT (line_plot), GTK_PLOT_AXIS_RIGHT);
 	gtk_plot_axis_set_labels_style (axis,
-					GTK_PLOT_LABEL_FLOAT,
+					ystyle /* style */,
 					yprec /* precision */);
 	gtk_plot_axis_show_labels (axis, lineplot_draw_labels ?
 				      GTK_PLOT_LABEL_OUT :
@@ -2264,12 +2274,13 @@ static void
 surface_setup_axis (void)
 {
 	int xprec, yprec, zprec;
+	int xstyle, ystyle, zstyle;
 	double xtick, ytick, ztick;
 	GtkPlotAxis *x, *y, *z;
 
-	get_ticks (surfacex1, surfacex2, &xtick, &xprec);
-	get_ticks (surfacey1, surfacey2, &ytick, &yprec);
-	get_ticks (surfacez1, surfacez2, &ztick, &zprec);
+	get_ticks (surfacex1, surfacex2, &xtick, &xprec, &xstyle);
+	get_ticks (surfacey1, surfacey2, &ytick, &yprec, &ystyle);
+	get_ticks (surfacez1, surfacez2, &ztick, &zprec, &zstyle);
 
 	x = gtk_plot3d_get_axis (GTK_PLOT3D (surface_plot), GTK_PLOT_AXIS_X);
 	y = gtk_plot3d_get_axis (GTK_PLOT3D (surface_plot), GTK_PLOT_AXIS_Y);
@@ -2287,13 +2298,13 @@ surface_setup_axis (void)
 	gtk_plot_axis_set_ticks (z, ztick, 1);
 
 	gtk_plot_axis_set_labels_style (x,
-					GTK_PLOT_LABEL_FLOAT,
+					xstyle,
 					xprec /* precision */);
 	gtk_plot_axis_set_labels_style (y,
-					GTK_PLOT_LABEL_FLOAT,
+					ystyle,
 					yprec /* precision */);
 	gtk_plot_axis_set_labels_style (z,
-					GTK_PLOT_LABEL_FLOAT,
+					zstyle,
 					zprec /* precision */);
 
 	gtk_plot_axis_thaw (x);
@@ -2308,11 +2319,43 @@ surface_setup_steps (void)
 	gtk_plot_surface_set_xstep (GTK_PLOT_SURFACE (surface_data), (surfacex2-surfacex1)/30);
 	gtk_plot_surface_set_ystep (GTK_PLOT_SURFACE (surface_data), (surfacey2-surfacey1)/30);
 
+	gtk_plot_surface_build_mesh (surface_data);
+
+	/* FIXME: CAN DO AUTOSCALE NOW! */
+}
+
+/* FIXME: perhaps should be smarter ? */
+static void
+surface_setup_gradient (void)
+{
+	double min, max, absminmax;
+
+	min = MAX(surfacez1, plot_minz);
+	max = MIN(surfacez2, plot_maxz);
+
 	gtk_plot_data_set_gradient (surface_data,
-				    surfacez1,
-				    surfacez2,
+				    min,
+				    max,
 				    10 /* nlevels */,
 				    0 /* nsublevels */);
+	absminmax = MAX(fabs(min),fabs(max));
+	if (absminmax < 0.0001 || absminmax > 1000000) {
+		gtk_plot_data_gradient_set_style (surface_data,
+						  GTK_PLOT_LABEL_EXP,
+						  3);
+	} else {
+		int powten = floor (log10(absminmax));
+		int prec = 0;
+		if (-powten+2 > 0) {
+			prec = -powten+2;
+		} else {
+			prec = 0;
+		}
+		gtk_plot_data_gradient_set_style (surface_data,
+						  GTK_PLOT_LABEL_FLOAT,
+						  prec);
+	}
+
 }
 
 static void
@@ -2323,6 +2366,8 @@ plot_axis (void)
 
 	plot_maxy = - G_MAXDOUBLE/2;
 	plot_miny = G_MAXDOUBLE/2;
+	plot_maxz = - G_MAXDOUBLE/2;
+	plot_minz = G_MAXDOUBLE/2;
 	plot_maxx = - G_MAXDOUBLE/2;
 	plot_minx = G_MAXDOUBLE/2;
 
@@ -2334,6 +2379,7 @@ plot_axis (void)
 	} else if (plot_mode == MODE_SURFACE) {
 		surface_setup_axis ();
 		surface_setup_steps ();
+		surface_setup_gradient ();
 		/* FIXME: this doesn't work (crashes) must fix in GtkExtra, then
 		   we can always just autoscale stuff
 		   gtk_plot3d_autoscale (GTK_PLOT3D (surface_plot));
@@ -4118,6 +4164,7 @@ plot_surface_functions (gboolean do_window_present)
 				   surface_data);
 
 		surface_setup_steps ();
+		surface_setup_gradient ();
 
 		gtk_widget_show (GTK_WIDGET (surface_data));
 
@@ -4152,11 +4199,10 @@ plot_surface_functions (gboolean do_window_present)
 		gtk_plot_surface_recalc_nodes (GTK_PLOT_SURFACE (surface_data));
 
 
-		surface_setup_steps ();
-
 		gtk_plot_add_data (GTK_PLOT (surface_plot),
 				   surface_data);
 
+		surface_setup_gradient ();
 
 		gtk_widget_show (GTK_WIDGET (surface_data));
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]