[GRASS-SVN] r60140 - grass/trunk/ps/ps.map

svn_grass at osgeo.org svn_grass at osgeo.org
Wed May 7 00:33:05 PDT 2014


Author: hamish
Date: 2014-05-07 00:33:05 -0700 (Wed, 07 May 2014)
New Revision: 60140

Modified:
   grass/trunk/ps/ps.map/ps.map.html
   grass/trunk/ps/ps.map/ps_fclrtbl.c
Log:
add support for generating horizontal gradient legends, tweak tickbar style

Modified: grass/trunk/ps/ps.map/ps.map.html
===================================================================
--- grass/trunk/ps/ps.map/ps.map.html	2014-05-07 05:32:42 UTC (rev 60139)
+++ grass/trunk/ps/ps.map/ps.map.html	2014-05-07 07:33:05 UTC (rev 60140)
@@ -239,19 +239,20 @@
 the colors for each of a raster map's category values along with its
 associated category label. For a floating point (FCELL or DCELL) map a
 continuous gradient legend will be created.
-<p>If <b>raster</b> is omitted, the colortable defaults to the previously
+<p>
+If <b>raster</b> is omitted, the colortable defaults to the previously
 registered raster layer.
-
+<p>
 The default location for the colortable is immediately below any other
 map legend information, starting at the left margin.
 The default text color is black.
-
-Omitting the <b>colortable</b> instruction would result in
-no color table.
-<p>If the colortable is turned off with a "<tt>colortable N</tt>"
-instruction the <b>end</b> command should not be given as the  
+<p>
+Omitting the <b>colortable</b> instruction would result in no color table.
+If the colortable is turned off with a "<tt>colortable N</tt>"
+instruction the <b>end</b> command should not be given as the
 main command will be treated as a single line instruction.
-<p>See also the <a href="#vlegend">vlegend</a> command for creating vector map
+<p>
+See also the <a href="#vlegend">vlegend</a> command for creating vector map
 legends.
 
 
@@ -266,7 +267,8 @@
 This could result in the printing of an extremely long color table!
 In this situation it is useful to use the <b>discrete N</b> instruction
 to force a continuous color gradient legend.
-<p>Be aware that the color table only includes categories which
+<p>
+Be aware that the color table only includes categories which
 have a label. You can use the <em>r.category</em> module to add labels.
 
 
@@ -283,6 +285,8 @@
 
 For floating point legends <b>width</b> is width of color
 band only. <b>height</b> is used only for floating point legend.
+A horizontal gradient legend can be achieved by setting the legend
+width greater than its height.
 
 Adding the <b>tickbar Y</b> instruction will change the tick mark style
 so that ticks are drawn across the color table instead of protruding out 

Modified: grass/trunk/ps/ps.map/ps_fclrtbl.c
===================================================================
--- grass/trunk/ps/ps.map/ps_fclrtbl.c	2014-05-07 05:32:42 UTC (rev 60139)
+++ grass/trunk/ps/ps.map/ps_fclrtbl.c	2014-05-07 07:33:05 UTC (rev 60140)
@@ -25,8 +25,8 @@
     int i, k;
     int R, G, B;
     DCELL dmin, dmax, val;
-    double t, l;		/* legend top, left, right */
-    double x1, x2, y, dy, xu, yu;
+    double t, l;		/* legend top, left */
+    double x1, x2, y1, y2, x, y, dy, xu, yu;
     double width;		/* width of legend in map units */
     double height;		/* width of legend in map units */
     double cwidth;		/* width of one color line */
@@ -37,7 +37,7 @@
     struct Colors colors;
     struct FPRange range;
     double ex, cur_d, cur_ex;
-    int do_color;
+    int do_color, horiz = FALSE;
     double grey_color_val, margin;
     unsigned int max_label_length = 0;
     int label_posn, label_xref, label_yref;
@@ -119,14 +119,30 @@
     width = 72.0 * ct.width;
     height = 72.0 * ct.height;
     cwidth = 0.1;
-    ncols = (int)height / cwidth;
+
+    if (width > height) {
+	horiz = TRUE;
+	ncols = (int)width / cwidth;
+	dy *= 1.4;  /* leave a bit more space so the tick labels don't overlap */
+    }
+    else
+	ncols = (int)height / cwidth;
+
     step = (dmax - dmin) / (ncols - 1);
-    lwidth = ct.lwidth;
+    lwidth = ct.lwidth;  /* line width */
 
     /* Print color band */
-    y = t;
-    x1 = l;
-    x2 = x1 + width;
+    if (horiz) {
+	x = l + width;
+	y1 = t + height;
+	y2 = t;
+    }
+    else {  /* vertical */
+	y = t;
+	x1 = l;
+	x2 = x1 + width;
+    }
+
     fprintf(PS.fp, "%.8f W\n", cwidth);
 
     for (i = 0; i < ncols; i++) {
@@ -144,19 +160,33 @@
 	}
 
 	fprintf(PS.fp, "NP\n");
-	fprintf(PS.fp, "%f %f M\n", x1, y);
-	fprintf(PS.fp, "%f %f LN\n", x2, y);
+	if (horiz) {
+	    fprintf(PS.fp, "%f %f M\n", x, y1);
+	    fprintf(PS.fp, "%f %f LN\n", x, y2);
+	    x -= cwidth;
+	}
+	else {  /* vertical */
+	    fprintf(PS.fp, "%f %f M\n", x1, y);
+	    fprintf(PS.fp, "%f %f LN\n", x2, y);
+	    y -= cwidth;
+	}
 	fprintf(PS.fp, "D\n");
-	y -= cwidth;
     }
 
     /* Frame around */
     fprintf(PS.fp, "NP\n");
     set_ps_color(&ct.color);
     fprintf(PS.fp, "%.8f W\n", lwidth);
-    fprintf(PS.fp, "%f %f %f %f B\n", x1,
-	    t - (ncols - 1) * cwidth - (cwidth + lwidth) / 2, x2,
-	    t + (cwidth + lwidth) / 2);
+    if (horiz) {
+	fprintf(PS.fp, "%f %f %f %f B\n",
+		l + width + (cwidth + lwidth) / 2, y1,
+		l + width - (ncols - 1) * cwidth - (cwidth + lwidth) / 2, y2);
+    }
+    else {
+	fprintf(PS.fp, "%f %f %f %f B\n", x1,
+		t - (ncols - 1) * cwidth - (cwidth + lwidth) / 2, x2,
+		t + (cwidth + lwidth) / 2);
+    }
     fprintf(PS.fp, "D\n");
 
     /* Print labels */
@@ -198,15 +228,26 @@
     if (val < dmin)
 	val += step;
 
-    x1 = l + width + 0.1;
-    if (ct.tickbar)		/* switch to draw tic all the way through bar */
-	x2 = x1 - width;
+    if (horiz) {
+	y2 = t - 0.37 * height;
+	if (height > 36)
+	    y2 = t - 0.37 * 36;
+
+	if (ct.tickbar)		/* this is the switch to draw tic all the way through bar */
+	    y1 = t + height;
+	else
+	    y1 = t;
+    }
     else {
+	x1 = l + width + 0.1;
 	x2 = x1 + 0.37 * width;
 	if (width > 36)
 	    x2 = x1 + 0.37 * 36;
-    }    
 
+	if (ct.tickbar)
+	    x1 -= width;
+    }
+
     /* do nice label: we need so many decimal places to hold all step decimal digits */
     if (step > 100) {		/* nice steps do not have > 2 digits, important separate, otherwise */
 	ddig = 0;		/* we can get something like 1000000.00000000765239 */
@@ -221,12 +262,22 @@
     }
 
     fprintf(PS.fp, "%.8f W\n", lwidth);
+
     while (val <= dmax) {
-	/*      y = t - (val - dmin) * height / (dmax - dmin) ;  flip */
-	y = t - (dmax - val) * height / (dmax - dmin);
 	fprintf(PS.fp, "NP\n");
-	fprintf(PS.fp, "%f %f M\n", x1, y);
-	fprintf(PS.fp, "%f %f LN\n", x2, y);
+
+	if (horiz) {
+	    x = l + width - (dmax - val) * width / (dmax - dmin);
+	    fprintf(PS.fp, "%f %f M\n", x, y1);
+	    fprintf(PS.fp, "%f %f LN\n", x, y2);
+	}
+	else {
+	    /*  y = t - (val - dmin) * height / (dmax - dmin) ;   *** flip */
+	    y = t - (dmax - val) * height / (dmax - dmin);
+	    fprintf(PS.fp, "%f %f M\n", x1, y);
+	    fprintf(PS.fp, "%f %f LN\n", x2, y);
+	}
+
 	fprintf(PS.fp, "D\n");
 
 	sprintf(buf, "%f", val);
@@ -236,16 +287,17 @@
 	    ch++;
 	*ch = '\0';
 
-	if (ct.tickbar)		/* switch to draw tic all the way through bar */
-	    fprintf(PS.fp, "(%s) %f %f MS\n", buf, x1 + 0.2 * ct.fontsize,
-		    y - 0.35 * ct.fontsize);
+	if(strlen(buf) > max_label_length)
+	    max_label_length = strlen(buf);
+
+	if (horiz)
+	    fprintf(PS.fp, "(%s) %f %f MS\n", buf,
+		    x + 0.2 * ct.fontsize - (strlen(buf) * 0.37 * ct.fontsize),
+		    y2 - 1.15 * ct.fontsize);
 	else
 	    fprintf(PS.fp, "(%s) %f %f MS\n", buf, x2 + 0.2 * ct.fontsize,
 		    y - 0.35 * ct.fontsize);
 
-	if(strlen(buf) > max_label_length)
-	    max_label_length = strlen(buf);
-
 	val += step;
     }
 
@@ -269,10 +321,14 @@
 	   `charpath` may overflow the maximum path length). */
 
 	/* select label position */
-	label_posn = 3;
+	if (horiz)
+	    label_posn = 5;
+	else
+	    label_posn = 3;
 	/*  1 2
 	      3
 	    5 4 */
+
 	switch (label_posn) {
 	case 1:
 	    /* above the tick numbers */
@@ -283,10 +339,7 @@
 	    break;
 	case 2:
 	    /* directly above the tick numbers */
-	    if (ct.tickbar)
-		xu = x1 + 0.2 * ct.fontsize;
-	    else
-		xu = x2 + 0.2 * ct.fontsize;
+	    xu = x2 + 0.2 * ct.fontsize;
 	    yu = t + 0.05*72;
 	    label_xref = LEFT;
 	    label_yref = LOWER;
@@ -294,34 +347,35 @@
 	case 3:
 	    /* to the right of the tick numbers */
 	    xu = 0.15*72 + max_label_length * ct.fontsize * 0.5;
-	    if (ct.tickbar)
-		xu += x1;
-	    else
-		xu += x2;
+	    xu += x2;
 	    yu = t - height/2;
 	    label_xref = LEFT;
 	    label_yref = CENTER;
 	    break;
 	case 4:
 	    /* directly below the tick numbers */
-	    if (ct.tickbar)
-		xu = x1 + 0.2 * ct.fontsize;
-	    else
-		xu = x2 + 0.2 * ct.fontsize;
+	    xu = x2 + 0.2 * ct.fontsize;
 	    yu = t - height - 0.05*72;
 	    label_xref = LEFT;
 	    label_yref = UPPER;
 	    break;
 	case 5:
 	    /* below the tick numbers */
-	    xu = x1;
-	    yu = t - height - 0.05*72;
-	    label_xref = LEFT;
+	    if (horiz) {
+		xu = l + width/2.;
+		yu = y2 - 1.85 * ct.fontsize;
+		label_xref = CENTER;
+	    }
+	    else {
+		xu = x1;
+		yu = t - height - 0.05*72;
+		label_xref = LEFT;
+	    }
 	    label_yref = UPPER;
 	    break;
 	}
 
-	text_box_path( xu, yu, label_xref, label_yref, units, 0);
+	text_box_path(xu, yu, label_xref, label_yref, units, 0);
 	fprintf(PS.fp, "TIB\n");
 	set_rgb_color(BLACK);
     }



More information about the grass-commit mailing list