[GRASS-SVN] r64862 - grass/trunk/display/d.vect.chart

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 15 06:01:43 PDT 2015


Author: mmetz
Date: 2015-03-15 06:01:43 -0700 (Sun, 15 Mar 2015)
New Revision: 64862

Modified:
   grass/trunk/display/d.vect.chart/bar.c
   grass/trunk/display/d.vect.chart/global.h
   grass/trunk/display/d.vect.chart/main.c
   grass/trunk/display/d.vect.chart/pie.c
   grass/trunk/display/d.vect.chart/plot.c
Log:
d.vect.chart: bug fixes, +3D charts

Modified: grass/trunk/display/d.vect.chart/bar.c
===================================================================
--- grass/trunk/display/d.vect.chart/bar.c	2015-03-15 13:01:07 UTC (rev 64861)
+++ grass/trunk/display/d.vect.chart/bar.c	2015-03-15 13:01:43 UTC (rev 64862)
@@ -7,11 +7,12 @@
 
 int
 bar(double cx, double cy, int size, double scale, double *val, int ncols,
-    COLOR * ocolor, COLOR * colors, int y_center, double *max_reference)
+    COLOR * ocolor, COLOR * colors, int y_center, double *max_reference,
+    int do3d)
 {
     int i;
     double max;
-    double x0, y0;
+    double x0, y0, x1, y1, dx, dy;
     double bw;			/* bar width */
     double pixel;		/* pixel size */
     struct line_pnts *Points, *max_Points;
@@ -44,6 +45,8 @@
     x0 = cx - size * pixel / 2;
 
     bw = size * pixel / ncols;
+    dx = bw / 5.0;
+    dy = dx * 1.5;
 
     if (max_reference) {
 	/* Draw polygon outlining max value in dataset with no fill color */
@@ -81,6 +84,46 @@
 
 	D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
 	D_polyline_abs(Points->x, Points->y, Points->n_points);
+	
+	if (do3d) {
+	    /* up */
+	    Vect_reset_line(Points);
+	    y1 = y0 + scale * val[i] * pixel;
+	    Vect_append_point(Points, x0 + i * bw, y1, 0);
+	    Vect_append_point(Points, x0 + i * bw + dx, y1 + dy, 0);
+	    Vect_append_point(Points, x0 + (i + 1) * bw + dx, y1 + dy, 0);
+	    Vect_append_point(Points, x0 + (i + 1) * bw, y1, 0);
+	    Vect_append_point(Points, x0 + i * bw, y1, 0);
+
+	    if (!colors[i].none) {
+		D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
+		D_polygon_abs(Points->x, Points->y, Points->n_points);
+	    }
+
+	    D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
+	    /* do not draw the same line twice */
+	    Points->n_points = 4;
+	    D_polyline_abs(Points->x, Points->y, Points->n_points);
+
+	    /* right */
+	    Vect_reset_line(Points);
+	    x1 = x0 + (i + 1) * bw;
+	    Vect_append_point(Points, x1 + dx + 0.5 * pixel, y1 + dy, 0);
+	    Vect_append_point(Points, x1 + dx + 0.5 * pixel, y0 + dy, 0);
+	    Vect_append_point(Points, x1, y0, 0);
+	    Vect_append_point(Points, x1, y1, 0);
+	    Vect_append_point(Points, x1 + dx + 0.5 * pixel, y1 + dy, 0);
+
+	    if (!colors[i].none && val[i] > 0) {
+		D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
+		D_polygon_abs(Points->x, Points->y, Points->n_points);
+	    }
+
+	    D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
+	    /* do not draw the same line twice */
+	    Points->n_points = 3;
+	    D_polyline_abs(Points->x, Points->y, Points->n_points);
+	}
     }
 
     /* tidy up */

Modified: grass/trunk/display/d.vect.chart/global.h
===================================================================
--- grass/trunk/display/d.vect.chart/global.h	2015-03-15 13:01:07 UTC (rev 64861)
+++ grass/trunk/display/d.vect.chart/global.h	2015-03-15 13:01:43 UTC (rev 64862)
@@ -10,8 +10,10 @@
 
 int plot(int ctype, struct Map_info *Map, int type, int field,
 	 char *columns, int ncols, char *sizecol, int size, double scale,
-	 COLOR * ocolor, COLOR * colors, int y_center, double *max_reference);
+	 COLOR * ocolor, COLOR * colors, int y_center,
+	 double *max_reference, int do3d);
 int pie(double cx, double cy, int size, double *val, int ncols,
-	COLOR * ocolor, COLOR * colors);
+	COLOR * ocolor, COLOR * colors, int do3d);
 int bar(double cx, double cy, int size, double scale, double *val, int ncols,
-	COLOR * ocolor, COLOR * colors, int y_center, double *max_reference);
+	COLOR * ocolor, COLOR * colors, int y_center, double *max_reference,
+	int do3d);

Modified: grass/trunk/display/d.vect.chart/main.c
===================================================================
--- grass/trunk/display/d.vect.chart/main.c	2015-03-15 13:01:07 UTC (rev 64861)
+++ grass/trunk/display/d.vect.chart/main.c	2015-03-15 13:01:43 UTC (rev 64862)
@@ -43,7 +43,7 @@
     struct Option *field_opt;
     struct Option *ocolor_opt, *colors_opt;
     struct Option *columns_opt, *sizecol_opt;
-    struct Flag *y_center_flag, *legend_flag;
+    struct Flag *y_center_flag, *legend_flag, *chart3d_flag;
 
     /*   struct Flag *horizontal_bar_flag; */
     struct Map_info Map;
@@ -141,6 +141,12 @@
     legend_flag->description =
 	_("Create legend information and send to stdout");
 
+    chart3d_flag = G_define_flag();
+    chart3d_flag->key = '3';
+    chart3d_flag->description =
+	_("Create 3D charts");
+    chart3d_flag->guisection = _("Chart properties");
+
     /*
        horizontal_bar_flag = G_define_flag();
        horizontal_bar_flag->key = 'h';
@@ -189,7 +195,7 @@
     colors = (COLOR *) G_malloc(ncols * sizeof(COLOR));
 
     /* Fill max_reference values */
-    max_reference = (double *)G_malloc(ncols * sizeof(double));
+    max_reference = NULL;
 
     /* default colors */
     j = 0;
@@ -247,6 +253,7 @@
     
     /* should we plot the maximum reference on bar plots? */
     if (max_reference_opt->answer != NULL) {
+	max_reference = (double *)G_malloc(ncols * sizeof(double));
 
 	/* loop through the given values */
 	for (i = 0; i < ncols; i++) {
@@ -263,7 +270,8 @@
     ret = plot(ctype, &Map, type, field,
 	       columns_opt->answer, ncols,
 	       sizecol_opt->answer, size, scale,
-	       &ocolor, colors, y_center, max_reference);
+	       &ocolor, colors, y_center, max_reference,
+	       chart3d_flag->answer);
 
     D_save_command(G_recreate_command());
     D_close_driver();

Modified: grass/trunk/display/d.vect.chart/pie.c
===================================================================
--- grass/trunk/display/d.vect.chart/pie.c	2015-03-15 13:01:07 UTC (rev 64861)
+++ grass/trunk/display/d.vect.chart/pie.c	2015-03-15 13:01:43 UTC (rev 64862)
@@ -8,12 +8,41 @@
 
 #define PI  M_PI
 
+static int init = 0;
+static double sa = 0.0;
+static double ca = 1.0;
+
+
+/* rotates x and y about the origin (xo,yo) by angle radians */
+static void rotate(double *x, double *y, double xo, double yo, int do3d)
+{
+    double tmpx, tmpy;
+
+    if (!do3d)
+	return;
+
+    /* first translate */
+    tmpx = *x - xo;
+    tmpy = *y - yo;
+
+    /* now rotate */
+    *x = tmpx * ca - tmpy * sa;
+    *y = tmpx * sa + tmpy * ca;
+
+    /* now translate back */
+    *x += xo;
+    *y += yo;
+
+    return;
+}
+
+
 int
 pie(double cx, double cy, int size, double *val, int ncols, COLOR * ocolor,
-    COLOR * colors)
+    COLOR * colors, int do3d)
 {
     int i, j, n;
-    double a, end_ang, ang, tot_sum, sum, step, r;
+    double a, end_ang, ang, tot_sum, sum, step, r, rminor;
     double x, y;
     struct line_pnts *Points;
 
@@ -21,39 +50,155 @@
 
     Points = Vect_new_line_struct();
 
+    if (!init) {
+	sa = sin(-6 / 180.0 * PI);
+	ca = cos(-6 / 180.0 * PI);
+	init = 1;
+    }
+
     /* Calc sum */
     tot_sum = 0;
     for (i = 0; i < ncols; i++)
 	tot_sum += val[i];
+    
+    if (tot_sum == 0) {
+	Vect_destroy_line_struct(Points);
+	return 0;    /* nothing to draw */
+    }
 
     step = PI / 180;
     r = (D_d_to_u_col(2) - D_d_to_u_col(1)) * size / 2;	/* do it better */
+    rminor = r;
+
+    if (do3d) {
+	int first, np;
+
+	rminor = r * 2.0 / 3.0;
+
+	/* Draw lower polygon for each value */
+	sum = 0;
+	ang = 0;
+	for (i = 0; i < ncols; i++) {
+	    if (val[i] == 0)
+		continue;
+
+	    sum += val[i];
+
+	    end_ang = 2 * PI * sum / tot_sum;
+
+	    if (end_ang <= PI) {
+		ang = end_ang;
+		continue;
+	    }
+
+	    Vect_reset_line(Points);
+
+	    n = (int)ceil((end_ang - ang) / step);
+
+	    /* upper -> lower */
+	    a = ang;
+	    if (ang < PI)
+		a = PI;
+
+	    x = cx + r * cos(a);
+	    y = cy + rminor * sin(a);
+	    rotate(&x, &y, cx, cy, do3d);
+	    Vect_append_point(Points, x, y, 0);
+
+	    /* lower */
+	    first = ang < PI ? 1 : 0;
+	    for (j = 0, a = ang; j <= n; j++, a += step) {
+		if (j == n)
+		    a = end_ang;
+		if (a > PI) {
+		    if (first) {
+			x = cx + r * cos(PI);
+			y = cy + rminor * sin(PI) - r / 5;
+			rotate(&x, &y, cx, cy, do3d);
+			Vect_append_point(Points, x, y, 0);
+			first = 0;
+		    }
+		    x = cx + r * cos(a);
+		    y = cy + rminor * sin(a) - r / 5;
+		    rotate(&x, &y, cx, cy, do3d);
+		    Vect_append_point(Points, x, y, 0);
+		}
+	    }
+	    np = Points->n_points + 1;
+
+	    /* upper */
+	    first = end_ang > PI ? 1 : 0;
+	    for (j = 0, a = end_ang; j <= n; j++, a -= step) {
+		if (j == n)
+		    a = ang;
+		if (a > PI) {
+		    x = cx + r * cos(a);
+		    y = cy + rminor * sin(a);
+		    rotate(&x, &y, cx, cy, do3d);
+		    Vect_append_point(Points, x, y, 0);
+		}
+		else if (first) {
+		    x = cx + r * cos(PI);
+		    y = cy + rminor * sin(PI);
+		    rotate(&x, &y, cx, cy, do3d);
+		    Vect_append_point(Points, x, y, 0);
+		    first = 0;
+		}
+	    }
+
+	    ang = end_ang;
+	    
+	    if (Points->n_points == 0)
+		continue;
+
+	    if (!colors[i].none) {
+		D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
+		D_polygon_abs(Points->x, Points->y, Points->n_points);
+	    }
+
+	    D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
+	    Points->n_points = np;
+	    D_polyline_abs(Points->x, Points->y, Points->n_points);
+	}
+    }
+
     /* Draw polygon for each value */
     sum = 0;
     ang = 0;
     for (i = 0; i < ncols; i++) {
+	if (val[i] == 0)
+	    continue;
+
 	sum += val[i];
-	if (tot_sum > 0.0)
-           end_ang = 2 * PI * sum / tot_sum;
-        else
-           end_ang = 0;
+
+	end_ang = 2 * PI * sum / tot_sum;
+
 	Vect_reset_line(Points);
 
-	if (val[0] != tot_sum)	/* all in one slice, don't draw line to center */
-	    Vect_append_point(Points, cx, cy, 0);
+	if (val[i] != tot_sum) {    /* all in one slice, don't draw line to center */
+	    x = cx;
+	    y = cy;
+	    rotate(&x, &y, cx, cy, do3d);
+	    Vect_append_point(Points, x, y, 0);
+	}
 
 	n = (int)ceil((end_ang - ang) / step);
 	for (j = 0, a = ang; j <= n; j++, a += step) {
 	    if (a > end_ang)
 		a = end_ang;
 	    x = cx + r * cos(a);
-	    y = cy + r * sin(a);
+	    y = cy + rminor * sin(a);
+	    rotate(&x, &y, cx, cy, do3d);
 	    Vect_append_point(Points, x, y, 0);
 	}
 	ang = end_ang;
 
-	if (val[0] != tot_sum)
-	    Vect_append_point(Points, cx, cy, 0);
+	if (val[i] != tot_sum) {    /* all in one slice, don't draw line to center */
+	    x = cx;
+	    y = cy;
+	    rotate(&x, &y, cx, cy, do3d);
+	    Vect_append_point(Points, x, y, 0);
+	}
 
 	if (!colors[i].none) {
 	    D_RGB_color(colors[i].r, colors[i].g, colors[i].b);

Modified: grass/trunk/display/d.vect.chart/plot.c
===================================================================
--- grass/trunk/display/d.vect.chart/plot.c	2015-03-15 13:01:07 UTC (rev 64861)
+++ grass/trunk/display/d.vect.chart/plot.c	2015-03-15 13:01:43 UTC (rev 64862)
@@ -11,7 +11,8 @@
 int
 plot(int ctype, struct Map_info *Map, int type, int field,
      char *columns, int ncols, char *sizecol, int size, double scale,
-     COLOR * ocolor, COLOR * colors, int y_center, double *max_reference)
+     COLOR * ocolor, COLOR * colors, int y_center, double *max_reference,
+     int do3d)
 {
     int ltype, nlines, line, col, more, coltype, nselcols;
     double x, y, csize, len;
@@ -126,11 +127,11 @@
 		csize = val[ncols];
 		size = scale * csize;
 	    }
-	    pie(x, y, size, val, ncols, ocolor, colors);
+	    pie(x, y, size, val, ncols, ocolor, colors, do3d);
 	}
 	else {
 	    bar(x, y, size, scale, val, ncols, ocolor, colors, y_center,
-		max_reference);
+		max_reference, do3d);
 	}
     }
 



More information about the grass-commit mailing list