From: <ai...@us...> - 2010-11-03 06:36:32
|
Revision: 11304 http://plplot.svn.sourceforge.net/plplot/?rev=11304&view=rev Author: airwin Date: 2010-11-03 06:36:26 +0000 (Wed, 03 Nov 2010) Log Message: ----------- Add bounding box and nrow/ncolumn capability to pllegend. Modified Paths: -------------- trunk/examples/c/x04c.c trunk/examples/c/x26c.c trunk/include/plplot.h trunk/src/pllegend.c Modified: trunk/examples/c/x04c.c =================================================================== --- trunk/examples/c/x04c.c 2010-11-03 06:32:24 UTC (rev 11303) +++ trunk/examples/c/x04c.c 2010-11-03 06:36:26 UTC (rev 11304) @@ -141,7 +141,9 @@ // to do with boxes. plscol0a( 15, 32, 32, 32, 0.90 ); - pllegend( PL_LEGEND_BACKGROUND, 0.57, 0.85, 0.06, 15, + pllegend( PL_LEGEND_BACKGROUND | PL_LEGEND_BOUNDING_BOX, + 0.57, 0.85, 0.06, 15, + 1, 1, 0, 0, nlegend, opt_array, 1.0, 1.0, 2.0, 1., text_colors, (const char **) text, Modified: trunk/examples/c/x26c.c =================================================================== --- trunk/examples/c/x26c.c 2010-11-03 06:32:24 UTC (rev 11303) +++ trunk/examples/c/x26c.c 2010-11-03 06:36:26 UTC (rev 11304) @@ -240,7 +240,9 @@ // to do with boxes. plscol0a( 15, 32, 32, 32, 0.90 ); - pllegend( PL_LEGEND_BACKGROUND, 0.57, 0.85, 0.06, 15, + pllegend( PL_LEGEND_BACKGROUND | PL_LEGEND_BOUNDING_BOX, + 0.57, 0.85, 0.06, 15, + 1, 1, 0, 0, nlegend, opt_array, 1.0, 1.0, 2.0, 1., text_colors, (const char **) legend_text, Modified: trunk/include/plplot.h =================================================================== --- trunk/include/plplot.h 2010-11-03 06:32:24 UTC (rev 11303) +++ trunk/include/plplot.h 2010-11-03 06:36:26 UTC (rev 11304) @@ -1209,15 +1209,19 @@ // Routine for drawing line, symbol, cmap0, or cmap1 legends // Flags for pllegend. -#define PL_LEGEND_NONE 1 -#define PL_LEGEND_COLOR_BOX 2 -#define PL_LEGEND_LINE 4 -#define PL_LEGEND_SYMBOL 8 -#define PL_LEGEND_TEXT_LEFT 16 -#define PL_LEGEND_BACKGROUND 32 +#define PL_LEGEND_NONE 1 +#define PL_LEGEND_COLOR_BOX 2 +#define PL_LEGEND_LINE 4 +#define PL_LEGEND_SYMBOL 8 +#define PL_LEGEND_TEXT_LEFT 16 +#define PL_LEGEND_BACKGROUND 32 +#define PL_LEGEND_BOUNDING_BOX 64 +#define PL_LEGEND_ROW_MAJOR 128 PLDLLIMPEXP void -c_pllegend( PLINT opt, PLFLT x, PLFLT y, PLFLT plot_width, PLINT bg_color, +c_pllegend( PLINT opt, PLFLT x, PLFLT y, PLFLT plot_width, + PLINT bg_color, PLINT bb_color, PLINT bb_style, + PLINT nrow, PLINT ncolumn, PLINT nlegend, const PLINT *opt_array, PLFLT text_offset, PLFLT text_scale, PLFLT text_spacing, PLFLT text_justification, Modified: trunk/src/pllegend.c =================================================================== --- trunk/src/pllegend.c 2010-11-03 06:32:24 UTC (rev 11303) +++ trunk/src/pllegend.c 2010-11-03 06:36:26 UTC (rev 11304) @@ -164,7 +164,9 @@ //! void -c_pllegend( PLINT opt, PLFLT x, PLFLT y, PLFLT plot_width, PLINT bg_color, +c_pllegend( PLINT opt, PLFLT x, PLFLT y, PLFLT plot_width, + PLINT bg_color, PLINT bb_color, PLINT bb_style, + PLINT nrow, PLINT ncolumn, PLINT nlegend, const PLINT *opt_array, PLFLT text_offset, PLFLT text_scale, PLFLT text_spacing, PLFLT text_justification, @@ -185,8 +187,8 @@ PLFLT text_x, text_y, text_x_world, text_y_world; // Character height (world coordinates) PLFLT character_height, character_width, symbol_width; - // y-position of the current legend entry - PLFLT ty, dty; + // x, y-position of the current legend entry + PLFLT ty, xshift, drow, dcolumn; // Positions of the legend entries PLFLT dxs, *xs, *ys, xl[2], yl[2], xbox[4], ybox[4]; PLINT i, j; @@ -203,11 +205,25 @@ PLFLT xdmin_save, xdmax_save, ydmin_save, ydmax_save; PLFLT x_world_per_mm, y_world_per_mm, text_width0 = 0., text_width; - PLFLT total_width_border, total_width, total_height; + PLFLT width_border, column_separation, total_width, total_height; PLINT some_boxes = 0, some_lines = 0, some_symbols = 0; PLINT max_symbol_numbers = 0; + PLINT irow = 0, icolumn = 0; + // Default nrow, ncolumn. + nrow = MAX( nrow, 1 ); + ncolumn = MAX( ncolumn, 1 ); + if ( nrow * ncolumn < nlegend ) + { + // Make smaller one large enough to accomodate nlegend. + if ( ncolumn < nrow ) + ncolumn = ( nlegend % nrow ) ? ( nlegend / nrow ) + 1 : nlegend / nrow; + else + nrow = ( nlegend % ncolumn ) ? ( nlegend / ncolumn ) + 1 : nlegend / ncolumn; + } + // fprintf(stdout, "nrow, ncolumn = %d, %d\n", nrow, ncolumn); + plgvpd( &xdmin_save, &xdmax_save, &ydmin_save, &ydmax_save ); plgvpw( &xwmin_save, &xwmax_save, &ywmin_save, &ywmax_save ); // viewport corresponds to sub-page so that all legends will @@ -262,9 +278,16 @@ // make small border area where only the background is plotted // for left and right of legend. 0.4 seems to be a reasonable factor // that gives a good-looking result. - total_width_border = 0.4 * character_width; - total_width = 2. * total_width_border + text_width + ( xmax - xmin ) * plot_width; - total_height = nlegend * text_spacing * character_height; + width_border = 0.4 * character_width; + // Separate columns (if any) by 2.0 * character_width. + column_separation = 2.0 * character_width; + total_width = 2. * width_border + ( ncolumn - 1 ) * column_separation + + ncolumn * ( text_width + ( xmax - xmin ) * plot_width ); + total_height = nrow * text_spacing * character_height; + // dcolumn is the spacing from one column to the next and + // drow is the spacing from one row to the next. + dcolumn = column_separation + text_width + ( xmax - xmin ) * plot_width; + drow = text_spacing * character_height; if ( opt & PL_LEGEND_BACKGROUND ) { @@ -286,6 +309,28 @@ plcol0( col0_save ); } + if ( opt & PL_LEGEND_BOUNDING_BOX ) + { + PLFLT xbb[5] = { + plot_x_world, + plot_x_world, + plot_x_world + total_width, + plot_x_world + total_width, + plot_x_world, + }; + PLFLT ybb[5] = { + plot_y_world, + plot_y_world - total_height, + plot_y_world - total_height, + plot_y_world, + plot_y_world, + }; + pllsty( bb_style ); + plcol0( bb_color ); + plline( 5, xbb, ybb ); + plcol0( col0_save ); + } + if ( opt & PL_LEGEND_TEXT_LEFT ) { // text area on left, plot area on right. @@ -294,24 +339,10 @@ plot_x_end_world += text_width; } // adjust border after background is drawn. - plot_x_world += total_width_border; - plot_x_end_world += total_width_border; - text_x_world += total_width_border; + plot_x_world += width_border; + plot_x_end_world += width_border; + text_x_world += width_border; - if ( some_boxes ) - { - xbox[0] = plot_x_world; - xbox[1] = plot_x_world; - xbox[2] = plot_x_end_world; - xbox[3] = plot_x_end_world; - } - - if ( some_lines ) - { - xl[0] = plot_x_world; - xl[1] = plot_x_end_world; - } - if ( some_symbols ) { max_symbol_numbers = MAX( 2, max_symbol_numbers ); @@ -330,16 +361,15 @@ fabs( ( xmax - xmin ) / ( ymax - ymin ) ); } - dty = text_spacing * character_height; - ty = text_y_world + 0.5 * dty; // Draw each legend entry for ( i = 0; i < nlegend; i++ ) { // y position of text, lines, symbols, and/or centre of cmap0 box. - ty = ty - dty; + ty = text_y_world - ( (double) irow + 0.5 ) * drow; + xshift = (double) icolumn * dcolumn; // Label/name for the legend plcol0( text_colors[i] ); - plptex( text_x_world + text_justification * text_width0, ty, 0.1, 0.0, text_justification, text[i] ); + plptex( text_x_world + xshift + text_justification * text_width0, ty, 0.1, 0.0, text_justification, text[i] ); if ( !( opt_array[i] & PL_LEGEND_NONE ) ) { @@ -347,10 +377,14 @@ { plcol0( box_colors[i] ); plpsty( box_patterns[i] ); - ybox[0] = ty + 0.5 * dty * box_scales[i]; - ybox[1] = ty - 0.5 * dty * box_scales[i]; - ybox[2] = ty - 0.5 * dty * box_scales[i]; - ybox[3] = ty + 0.5 * dty * box_scales[i]; + xbox[0] = plot_x_world + xshift; + xbox[1] = xbox[0]; + xbox[2] = plot_x_end_world + xshift; + xbox[3] = xbox[2]; + ybox[0] = ty + 0.5 * drow * box_scales[i]; + ybox[1] = ty - 0.5 * drow * box_scales[i]; + ybox[2] = ty - 0.5 * drow * box_scales[i]; + ybox[3] = ty + 0.5 * drow * box_scales[i]; plfill( 4, xbox, ybox ); } if ( opt_array[i] & PL_LEGEND_LINE ) @@ -358,6 +392,8 @@ plcol0( line_colors[i] ); pllsty( line_styles[i] ); plwid( line_widths[i] ); + xl[0] = plot_x_world + xshift; + xl[1] = plot_x_end_world + xshift; yl[0] = ty; yl[1] = ty; plline( 2, xl, yl ); @@ -372,12 +408,33 @@ dxs = ( plot_x_end_world - plot_x_world - symbol_width ) / (double) ( MAX( symbol_numbers[i], 2 ) - 1 ); for ( j = 0; j < symbol_numbers[i]; j++ ) { - xs[j] = plot_x_world + 0.5 * symbol_width + dxs * (double) j; + xs[j] = plot_x_world + xshift + + 0.5 * symbol_width + dxs * (double) j; ys[j] = ty; } plpoin( symbol_numbers[i], xs, ys, symbols[i] ); } } + + // Set irow, icolumn for next i value. + if ( opt & PL_LEGEND_ROW_MAJOR ) + { + icolumn++; + if ( icolumn >= ncolumn ) + { + icolumn = 0; + irow++; + } + } + else + { + irow++; + if ( irow >= nrow ) + { + irow = 0; + icolumn++; + } + } } if ( some_symbols ) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |