from .charts_layout_types import Margins, PlotArea from .math_round_float import round_float 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))