Opened 9 years ago

Closed 8 years ago

#3013 closed enhancement (fixed)

support background and border for d.legend

Reported by: annakrat Owned by: grass-dev@…
Priority: normal Milestone: 7.2.0
Component: Display Version: svn-trunk
Keywords: d.legend, background, gsoc2016, cartography Cc:
CPU: Unspecified Platform: All

Description

It would be nice to have background box for legend. Ideally there would be option to add border too.

Attachments (10)

legend_border.png (8.3 KB ) - added by lazaa 8 years ago.
d.legend_bg.diff (18.6 KB ) - added by lazaa 8 years ago.
patch with border option
bg_ver.png (16.1 KB ) - added by lazaa 8 years ago.
For vertical legend the title is aligned to the left edge of the legend.
bg_hor.png (10.3 KB ) - added by lazaa 8 years ago.
For horizontal legend the title is aligned to the center
landcover.png (25.6 KB ) - added by lazaa 8 years ago.
d.legend raster=landcover_1m brdcolor=green bgcolor=yellow title=landcover -b at=25,75,10,15
d.leg_bg.diff (62.1 KB ) - added by lazaa 8 years ago.
patch for support background option in d.legend module
d.legend_bg2.diff (91.5 KB ) - added by lazaa 8 years ago.
patch for support background option in d.legend module
d.legend_background.diff (68.2 KB ) - added by lazaa 8 years ago.
legend_histogram_left_border.png (23.0 KB ) - added by wenzeslaus 8 years ago.
Legend missing left background border when histogram is active
sinr_legend.png (23.2 KB ) - added by lazaa 8 years ago.
sinr=sin(row()) legend with/without histogram

Download all attachments as: .zip

Change History (35)

by lazaa, 8 years ago

Attachment: legend_border.png added

by lazaa, 8 years ago

Attachment: d.legend_bg.diff added

patch with border option

comment:1 by lazaa, 8 years ago

I added -b flag to display legend background. Then brdcolor to choose border color. So far it works only for non-categoric rasters and displays only border color but no background. I draw background at the end of the script when I know position of legend, title and all labels at the display. But when I want draw background, the background rectangular overlaps all. Is there any option how to define not to overlap other lines? Or the only solution is to draw background rectangular first?

in reply to:  1 ; comment:2 by annakrat, 8 years ago

Replying to lazaa:

I added -b flag to display legend background. Then brdcolor to choose border color. So far it works only for non-categoric rasters and displays only border color but no background. I draw background at the end of the script when I know position of legend, title and all labels at the display. But when I want draw background, the background rectangular overlaps all. Is there any option how to define not to overlap other lines? Or the only solution is to draw background rectangular first?

Look at the other d.* modules with background - how they do it. But I am afraid you might have to draw it first, so you would need to do the computations twice.

in reply to:  2 ; comment:3 by lazaa, 8 years ago

Replying to annakrat:

Replying to lazaa:

I added -b flag to display legend background. Then brdcolor to choose border color. So far it works only for non-categoric rasters and displays only border color but no background. I draw background at the end of the script when I know position of legend, title and all labels at the display. But when I want draw background, the background rectangular overlaps all. Is there any option how to define not to overlap other lines? Or the only solution is to draw background rectangular first?

Look at the other d.* modules with background - how they do it. But I am afraid you might have to draw it first, so you would need to do the computations twice.

It seems that I have to draw background first :(. Currently all calculation is in main function. I would move all calculation into a new function and add one parameter to decide whether to draw or not. First time I would call the function to get position, second time to draw it. New function should be in separate file or it doesn't matter?

in reply to:  3 comment:4 by annakrat, 8 years ago

Replying to lazaa:

It seems that I have to draw background first :(. Currently all calculation is in main function. I would move all calculation into a new function and add one parameter to decide whether to draw or not. First time I would call the function to get position, second time to draw it. New function should be in separate file or it doesn't matter?

Separate file is probably better. BTW, do not hesitate to add comments in the code about what is which part doing.

by lazaa, 8 years ago

Attachment: bg_ver.png added

For vertical legend the title is aligned to the left edge of the legend.

by lazaa, 8 years ago

Attachment: bg_hor.png added

For horizontal legend the title is aligned to the center

comment:5 by lazaa, 8 years ago

What should be the behaviour of the title position for vertical/horizontal legend? For horizontal legend the title is aligned to the center so even the title is longer then legend it's not a problem. For vertical legend the title is aligned to the left edge of legend. That looks OK only if the title is not too long.

in reply to:  5 comment:6 by annakrat, 8 years ago

Replying to lazaa:

What should be the behaviour of the title position for vertical/horizontal legend? For horizontal legend the title is aligned to the center so even the title is longer then legend it's not a problem. For vertical legend the title is aligned to the left edge of legend. That looks OK only if the title is not too long.

Let's keep it that way. Could you also add an option title_fontsize? I think often the font size of the title is bigger.

by lazaa, 8 years ago

Attachment: landcover.png added

d.legend raster=landcover_1m brdcolor=green bgcolor=yellow title=landcover -b at=25,75,10,15

by lazaa, 8 years ago

Attachment: d.leg_bg.diff added

patch for support background option in d.legend module

comment:7 by lazaa, 8 years ago

Added flag -b and option brdcolor and bgcolor to draw background. Also added optionfont_size as requested. Code was separeted from main.c to background.c and draw.c. For background purpose in case of -d flag (histogram) added function calc_histogram in histrogram2.c

in reply to:  7 ; comment:8 by annakrat, 8 years ago

Replying to lazaa:

Added flag -b and option brdcolor and bgcolor to draw background. Also added optionfont_size as requested. Code was separeted from main.c to background.c and draw.c. For background purpose in case of -d flag (histogram) added function calc_histogram in histrogram2.c

Something is missing in the patch, I can't compile it.

in reply to:  8 ; comment:9 by lazaa, 8 years ago

Replying to annakrat:

Replying to lazaa:

Added flag -b and option brdcolor and bgcolor to draw background. Also added optionfont_size as requested. Code was separeted from main.c to background.c and draw.c. For background purpose in case of -d flag (histogram) added function calc_histogram in histrogram2.c

Something is missing in the patch, I can't compile it.

My bad, I didn't add all files. Now it should be OK.

by lazaa, 8 years ago

Attachment: d.legend_bg2.diff added

patch for support background option in d.legend module

in reply to:  9 ; comment:10 by annakrat, 8 years ago

Replying to lazaa:

Added flag -b and option brdcolor and bgcolor to draw background. Also added optionfont_size as requested. Code was separeted from main.c to background.c and draw.c. For background purpose in case of -d flag (histogram) added function calc_histogram in histrogram2.c

Tested, it works. However, the code is not maintainable like this, you have too large overlap between draw and background functions. I suggest to merge those functions and use ifs and put some parts into separate functions. Also check during compilation for warnings, there were a couple of them, probably not serious, but better to fix them.

in reply to:  10 comment:11 by lazaa, 8 years ago

Replying to annakrat:

Replying to lazaa:

Added flag -b and option brdcolor and bgcolor to draw background. Also added optionfont_size as requested. Code was separeted from main.c to background.c and draw.c. For background purpose in case of -d flag (histogram) added function calc_histogram in histrogram2.c

Tested, it works. However, the code is not maintainable like this, you have too large overlap between draw and background functions. I suggest to merge those functions and use ifs and put some parts into separate functions. Also check during compilation for warnings, there were a couple of them, probably not serious, but better to fix them.

That's right, I didn't realize possible problems with maintaining code. So I moved it to one function and used parameter draw and if conditions. Draw=0 is for calculation and draw=1 for calculation and drawing. I don't get any warnings now, so I hope it's OK.

by lazaa, 8 years ago

Attachment: d.legend_background.diff added

comment:12 by annakrat, 8 years ago

Thank you, I committed it in r68699. I did some minor changes in guisections and fixed one warning (-Wreturn-type). I have two more suggestions:

  • if you use different font than the default one, the background box is larger than it should be. You don't use D_get_text_box there, I wonder if this would give you better estimate?
  • The ticks are currently rendered in the same color as text, but they should be black as the legend outline.

comment:13 by neteler, 8 years ago

I just tried the updated d.legend, it looks really great!

One wish: would it be possible to turn the "Font name" field into a drop down list? If yes, also these modules could benefit from that:

  • d.rast.num
  • d.text
  • d.vect
  • v.label

in reply to:  12 comment:14 by lazaa, 8 years ago

Replying to annakrat:

Thank you, I committed it in r68699. I did some minor changes in guisections and fixed one warning (-Wreturn-type). I have two more suggestions:

  • if you use different font than the default one, the background box is larger than it should be. You don't use D_get_text_box there, I wonder if this would give you better estimate?

Thank you! Before there was a constant for font height/width ratio, which works only for romans font. With D_get_text_box it works now for all fonts much better.

  • The ticks are currently rendered in the same color as text, but they should be black as the legend outline.

I changed it to black color.

in reply to:  13 comment:15 by veroandreo, 8 years ago

Replying to neteler:

I just tried the updated d.legend, it looks really great!

One wish: would it be possible to turn the "Font name" field into a drop down list? If yes, also these modules could benefit from that:

  • d.rast.num
  • d.text
  • d.vect
  • v.label

I would also add d.grid to that list... IMHO, it would be more comfortable to change font there than to change it globally, and would allow to have different font settings for different map elements displayed.

in reply to:  13 ; comment:16 by lazaa, 8 years ago

Replying to neteler:

I just tried the updated d.legend, it looks really great!

One wish: would it be possible to turn the "Font name" field into a drop down list? If yes, also these modules could benefit from that:

  • d.rast.num
  • d.text
  • d.vect
  • v.label

I think it's possible. Only catch I can see, is there any list of fonts which are available within GRASS on every platform? I found one in d.font documentation with approx. 20 fonts. On the other hand if I run d.font -l I get hundreds of fonts (on my Ubuntu 16.04)

in reply to:  16 ; comment:17 by annakrat, 8 years ago

Replying to lazaa:

Replying to neteler:

I just tried the updated d.legend, it looks really great!

One wish: would it be possible to turn the "Font name" field into a drop down list? If yes, also these modules could benefit from that:

  • d.rast.num
  • d.text
  • d.vect
  • v.label

I think it's possible. Only catch I can see, is there any list of fonts which are available within GRASS on every platform? I found one in d.font documentation with approx. 20 fonts. On the other hand if I run d.font -l I get hundreds of fonts (on my Ubuntu 16.04)

I would add a button to the font option field which opens the same font dialog which is in settings. So any d.* module with font would have that automatically.

comment:18 by annakrat, 8 years ago

There is still a bug - when you use categorical legend (eg landuse96_28m@PERMANENT) the background doesn't take into account the labels.

in reply to:  17 comment:19 by annakrat, 8 years ago

Replying to annakrat:

I would add a button to the font option field which opens the same font dialog which is in settings. So any d.* module with font would have that automatically.

I added a button to interactively select font from d.* dialogs in r68757.

by wenzeslaus, 8 years ago

Legend missing left background border when histogram is active

comment:20 by wenzeslaus, 8 years ago

The -d flag (histogram) causes d.legend not to draw left border of the background (-b flag).

by lazaa, 8 years ago

Attachment: sinr_legend.png added

sinr=sin(row()) legend with/without histogram

comment:21 by lazaa, 8 years ago

I tested it with my data sample and it works fine both with or without histogram. Could you send me your g.region output, so I can generate exactly same raster map and test it?

sinr=sin(row()) legend with/without histogram

comment:22 by annakrat, 8 years ago

The problem is that the part of background is not rendered at all because of its position. The at option specifies the position and size of the legend bar, so if it's placed too close to the edge, the problem appears. Not sure what to do about it.

in reply to:  22 ; comment:23 by lazaa, 8 years ago

Replying to annakrat:

The problem is that the part of background is not rendered at all because of its position. The at option specifies the position and size of the legend bar, so if it's placed too close to the edge, the problem appears. Not sure what to do about it.

For GUI we can change the default position and move the legend closer to the center so the legend will be drawn with complete border and user can later move it interactively to wherever needs. For d.mon user can use the at option to move the legend further from the edge.

in reply to:  23 comment:24 by annakrat, 8 years ago

Replying to lazaa:

Replying to annakrat:

The problem is that the part of background is not rendered at all because of its position. The at option specifies the position and size of the legend bar, so if it's placed too close to the edge, the problem appears. Not sure what to do about it.

For GUI we can change the default position and move the legend closer to the center so the legend will be drawn with complete border and user can later move it interactively to wherever needs. For d.mon user can use the at option to move the legend further from the edge.

I hopefully fixed that in r68958.

comment:25 by annakrat, 8 years ago

Milestone: 7.3.07.2.0
Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.