Skip to main content

draw_legend

Function draw_legend 

Source
pub fn draw_legend(
    renderer: &mut impl Renderer,
    entries: &[LegendEntry],
    plot_area: &Rect,
    loc: Loc,
    theme: &Theme,
)
Expand description

Renders a legend box at the specified location within the plot area.

The legend is drawn with a semi-transparent white background and a thin border so that it does not fully obscure data underneath. Text styling is derived from the active Theme (specifically tick_label_size and text_color).

If entries is empty this function returns immediately without drawing anything.

§Arguments

  • renderer - The rendering backend to draw into.
  • entries - The legend entries to display (swatch + label each).
  • plot_area - The rectangle describing the axes / data area.
  • loc - Where to place the legend relative to the plot area.
  • theme - The active theme (used for font size, text color).

§Example

use plotkit_core::legend::{LegendEntry, draw_legend};
use plotkit_core::primitives::{Color, Rect};
use plotkit_core::theme::{Loc, Theme};

let entries = vec![
    LegendEntry::line("Temperature", Color::TAB_BLUE),
    LegendEntry::filled("Rainfall", Color::TAB_GREEN),
];
let area = Rect::new(60.0, 40.0, 680.0, 520.0);
draw_legend(&mut renderer, &entries, &area, Loc::UpperRight, &Theme::default());