Opened 4 years ago

Closed 3 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 3 years ago.
d.legend_bg.diff (18.6 KB) - added by lazaa 3 years ago.
patch with border option
bg_ver.png (16.1 KB) - added by lazaa 3 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 3 years ago.
For horizontal legend the title is aligned to the center
landcover.png (25.6 KB) - added by lazaa 3 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 3 years ago.
patch for support background option in d.legend module
d.legend_bg2.diff (91.5 KB) - added by lazaa 3 years ago.
patch for support background option in d.legend module
d.legend_background.diff (68.2 KB) - added by lazaa 3 years ago.
legend_histogram_left_border.png (23.0 KB) - added by wenzeslaus 3 years ago.
Legend missing left background border when histogram is active
sinr_legend.png (23.2 KB) - added by lazaa 3 years ago.
sinr=sin(row()) legend with/without histogram

Download all attachments as: .zip

Change History (35)

Changed 3 years ago by lazaa

Attachment: legend_border.png added

Changed 3 years ago by lazaa

Attachment: d.legend_bg.diff added

patch with border option

comment:1 Changed 3 years ago by 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?

comment:2 in reply to:  1 ; Changed 3 years ago by 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.

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

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?

comment:4 in reply to:  3 Changed 3 years ago by annakrat

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.

Changed 3 years ago by lazaa

Attachment: bg_ver.png added

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

Changed 3 years ago by lazaa

Attachment: bg_hor.png added

For horizontal legend the title is aligned to the center

comment:5 Changed 3 years ago by 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.

comment:6 in reply to:  5 Changed 3 years ago by annakrat

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.

Changed 3 years ago by lazaa

Attachment: landcover.png added

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

Changed 3 years ago by lazaa

Attachment: d.leg_bg.diff added

patch for support background option in d.legend module

comment:7 Changed 3 years ago by 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

comment:8 in reply to:  7 ; Changed 3 years ago by 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.

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

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.

Changed 3 years ago by lazaa

Attachment: d.legend_bg2.diff added

patch for support background option in d.legend module

comment:10 in reply to:  9 ; Changed 3 years ago by 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.

comment:11 in reply to:  10 Changed 3 years ago by lazaa

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.

Changed 3 years ago by lazaa

Attachment: d.legend_background.diff added

comment:12 Changed 3 years ago by 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?
  • The ticks are currently rendered in the same color as text, but they should be black as the legend outline.

comment:13 Changed 3 years ago by 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

comment:14 in reply to:  12 Changed 3 years ago by lazaa

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.

comment:15 in reply to:  13 Changed 3 years ago by veroandreo

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.

comment:16 in reply to:  13 ; Changed 3 years ago by 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)

comment:17 in reply to:  16 ; Changed 3 years ago by annakrat

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 Changed 3 years ago by annakrat

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

comment:19 in reply to:  17 Changed 3 years ago by annakrat

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.

Changed 3 years ago by wenzeslaus

Legend missing left background border when histogram is active

comment:20 Changed 3 years ago by wenzeslaus

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

Changed 3 years ago by lazaa

Attachment: sinr_legend.png added

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

comment:21 Changed 3 years ago by lazaa

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 Changed 3 years ago by 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.

comment:23 in reply to:  22 ; Changed 3 years ago by 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.

comment:24 in reply to:  23 Changed 3 years ago by annakrat

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 Changed 3 years ago by annakrat

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