Skip to content
render_inset

render_inset

Mojo function 🡭

render_inset

fn def render_inset[B: ChartLike, I: ChartLike](base: B, inset: I, x: Float64, y: Float64, width: Float64, height: Float64) -> Canvas

Render base as render() would, then inset over it inside a sub-rect of the base’s plot rect: a zoomed detail inside the chart it details (#347).

The four fractions are of the base’s plot rect, the area inside its margins and axes where the marks are, not of the canvas, and they describe the inset’s own plot rect, its axes area. x and y place that area’s top-left corner from the base plot rect’s top-left, in the row-down order every grid in this library reads, so x=0.55, y=0.05, width=0.4, height=0.4 is the top-right quarter. Only that area is painted with the inset’s background, so it covers the base beneath it; the inset’s tick labels, axis titles and title sit outside it, over the base, rather than on a blank panel that would punch a larger hole than the axes. The inset draws its own legend and annotations, and its own .size() is ignored, as a cell’s is in render_grid().

The canvas is the base’s size, supersampled by the larger of the two plots’ factors so a curved inset over a bar chart is drawn at the curve’s factor.

from dataviz import Plot, render_inset
from canvas.io.png import write_png

def main():
    var x = List[Float64](0.0, 1.0, 2.0, 3.0, 4.0, 5.0)
    var y = List[Float64](1.0, 4.0, 2.0, 5.0, 3.0, 6.0)
    var base = Plot().mark_line().encode(x=x, y=y).size(640, 420)
    var detail = Plot().mark_point().encode(
        x=List[Float64](2.0, 3.0), y=List[Float64](2.0, 5.0)
    )
    write_png(render_inset(base, detail, 0.55, 0.05, 0.4, 0.4), "inset.png")

Parameters:

  • B (ChartLike)
  • I (ChartLike)

Args:

  • base (B): The chart drawn first, at its own size.
  • inset (I): The chart drawn over it.
  • x (Float64): The inset’s left edge, as a fraction of the base’s plot-rect width from the plot rect’s left.
  • y (Float64): The inset’s top edge, as a fraction of the plot-rect height from the plot rect’s top.
  • width (Float64): The inset’s width as a fraction of the plot-rect width.
  • height (Float64): The inset’s height as a fraction of the plot-rect height.

Returns:

Canvas: The rendered figure, base.canvas_width() by base.canvas_height().

Raises:

Error: A fraction outside [0, 1], a non-positive size, an inset that runs past the plot rect or rounds to nothing, or anything rendering either plot raises.