[GRASS-dev] d.histogram problem [bug #1977]

Paul Kelly paul-grass at stjohnspoint.co.uk
Wed May 16 16:52:13 EDT 2007


Attached is a patch that seems to solve the worst of the d.histogram 
problems for me. I.e.
* the tick spacing is done correctly for large number of bins in a 
floating point map
* Range boundaries are calculated correctly (not off by one) and printed 
to 3 significant figures rather than being truncated to integers
* For integer maps the cell value covered by each bar is printed in the 
centre of the bar rather than to the left-hand side of it

But I haven't touched pie.c so it will only be fixed for bar graphs. And 
in general the code of the whole program is terribly convoluted and there 
could definitely be other subtle errors lurking in there I think. Also the 
text printing, ticks etc. could definitely be tidied up and made prettier.

But does it solve the problems others were having?

Paul
-------------- next part --------------
Index: bar.c
===================================================================
RCS file: /home/grass/grassrepository/grass6/display/d.histogram/bar.c,v
retrieving revision 2.2
diff -u -r2.2 bar.c
--- bar.c	15 Apr 2007 19:35:19 -0000	2.2
+++ bar.c	16 May 2007 20:45:31 -0000
@@ -112,14 +112,11 @@
 		   if(G_is_d_null_value(&range_dmin) || G_is_d_null_value(&range_dmax))
 		        G_fatal_error("Floating point data range is empty");
 
-                   while((range_dmax - range_dmin)/tics[i].every > max_tics)
-		       i++;
-                }
-		else
-		{
-		   while ((num_cats/tics[i].every) > max_tics)
-			i++;
                 }
+	   
+		while ((num_cats/tics[i].every) > max_tics)
+		    i++;
+	   
 		tic_every = tics[i].every;
 		tic_unit = tics[i].unit;
 		strcpy(tic_name,tics[i].name);
@@ -234,9 +231,9 @@
 						 &dmin, &dmax);
                                      else
 				     {
-				       dmin=range_dmin+(double)i*
+				       dmin=range_dmin+(double)(i-1)*
   				         (range_dmax-range_dmin)/(double)nsteps;
-				       dmax=range_dmin+(double)(i+1)*
+				       dmax=range_dmin+(double)(i)*
   				         (range_dmax-range_dmin)/(double)nsteps;
                                      }
 				     if(dmin != dmax)
@@ -282,9 +279,9 @@
 						 &dmin, &dmax);
                                      else
 				     {
-				       dmin=range_dmin+(double)i*
+				       dmin=range_dmin+(double)(i-1)*
   				         (range_dmax-range_dmin)/(double)nsteps;
-				       dmax=range_dmin+(double)(i+1)*
+				       dmax=range_dmin+(double)(i)*
   				         (range_dmax-range_dmin)/(double)nsteps;
                                      }
                                      D_d_color(dmin, colors);
@@ -307,17 +304,27 @@
 		      i==dist_stats->mincat && nodata) 
 		      && !(nodata && i==dist_stats->mincat+1))
 		{
+		        int text_middle = 0; /* Print text labels in middle
+					      * of intervals */
+		        double text_offset;
+		   
 			/* draw a numbered tic-mark */
 			R_standard_color(color);
 			R_move_abs( (int)(xoffset+(i-dist_stats->mincat)*xscale - 0.5 * xscale),
 			    (int)(b-ORIGIN_Y*(b-t)) );
 			R_cont_rel( (int)0 , (int)(BIG_TIC*(b-t)) );
 		        if(nodata && i==dist_stats->mincat)
+		        {			
 			   sprintf(txt, "null");
+			   text_middle = 1;
+			}		   
 			else if(is_fp)
-			   sprintf(txt,"%d",(int) (dmin/(double) tic_unit));
-                        else
+			   sprintf(txt,"%.3g", dmin / tic_unit);
+			else
+		        {			
 			   sprintf(txt,"%d",(int) (i/tic_unit));
+			   text_middle = 1;
+			}		   
 			text_height = (b-t)*TEXT_HEIGHT;
 			text_width = (r-l)*TEXT_WIDTH;
 			R_text_size(text_width,text_height);
@@ -329,7 +336,11 @@
 				R_text_size(text_width,text_height);
 				R_get_text_box(txt,&tt,&tb,&tl,&tr);
 			}
-			R_move_abs((int)(xoffset+(i-dist_stats->mincat)*xscale - 0.5 * xscale -(tr-tl)/2),
+		        if (text_middle)
+		            text_offset = 0;
+		        else
+		            text_offset = - (0.5 * xscale);
+			R_move_abs((int)(xoffset+(i-dist_stats->mincat)*xscale + text_offset -(tr-tl)/2),
 			    (int)(b-XNUMS_Y*(b-t)));
 			R_text(txt);
 		}


More information about the grass-dev mailing list