Functional Weave
Code in Python

charts.shape@1.0.1

impl/python/area_path.py

921 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 typing import List, Sequence

from .charts_shape_line_path import svg_pair  ← linePath, another function of this group · built into the same file, even by a slim install
from .charts_shape_types import AreaPoint


def area_path(points: Sequence[AreaPoint]) -> str:
    """A filled area as SVG path data, in d3.area's format: along the top edge
    (y1) left to right, back along the baseline (y0) right to left, closed.
    A null y1 ends one area and starts the next."""
    out = ""
    runs: List[List[AreaPoint]] = []
    run: List[AreaPoint] = []
    for p in points:
        if p.y1 is None:
            if run:
                runs.append(run)
            run = []
        else:
            run.append(p)
    if run:
        runs.append(run)
    for run in runs:
        for i, p in enumerate(run):
            out += ("M" if i == 0 else "L") + svg_pair(float(p.x), float(p.y1))
        for p in reversed(run):
            out += "L" + svg_pair(float(p.x), float(p.y0))
        out += "Z"
    return out