Functional Weave
Code in Python

charts.layout@1.0.0

impl/python/plot_area.py

906 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from .charts_layout_types import Margins, PlotArea
from .math_round_float import round_float  ← from math.round-float ^1.0.0 · built alongside by fune


def plot_area(width: float, height: float, margins: Margins) -> PlotArea:
    """The rectangle left for data once the margins are taken off the chart."""
    top, right, bottom, left = (float(margins.top), float(margins.right), float(margins.bottom), float(margins.left))
    if not (top >= 0 and right >= 0 and bottom >= 0 and left >= 0):
        raise ValueError("margins must not be negative")
    w = float(width) - left - right
    h = float(height) - top - bottom
    # A zero or negative plot would draw nothing, or draw everything flipped.
    if not (w > 0 and h > 0):
        raise ValueError("margins leave no room to plot: %r x %r less margins is %r x %r" % (width, height, w, h))
    return PlotArea(x=round_float(left, 2), y=round_float(top, 2), width=round_float(w, 2), height=round_float(h, 2))