Skip to content

text

Draws a text label for each data item. Used for value labels on bars, point annotations, node names in diagrams, and axis titles.

python
from gofish import chart, text

chart([{"label": "GoFish"}]).mark(
    text(text="label", fontSize=28, fill="steelblue")
).render(w=240, h=80)

Signature

python
text(*, text=None, fill=None, stroke=None, strokeWidth=None, filter=None,
     fontSize=None, fontFamily=None, debugBoundingBox=None, rotate=None,
     x=None, cx=None, x2=None, w=None, emX=None,
     y=None, cy=None, y2=None, h=None, emY=None,
     theta=None, thetaSize=None, r=None, rSize=None, key=None) -> Mark

Keyword-only (matches every existing call site, which already passes text=... by keyword). Note: the previously-documented fontWeight kwarg never existed on the JS side and has been removed — it silently serialized and was dropped on the floor at render.

Parameters

ParameterTypeDescription
textstr | intThe string to render — a constant, a field name, or a (row) -> str callable
fillstrFill color — a constant or a field name
stroke, strokeWidthstr, intOutline color / width
filterstrRaw SVG filter attribute
fontSizeint | strFont size in pixels (default 12)
fontFamilystrFont family (default "system-ui, sans-serif")
debugBoundingBoxboolDraw the text's bounding box (for layout debugging)
rotateintRotation in degrees about the text anchor
x, cx, x2, w, emX, y, cy, y2, h, emYint | strBox-geometry position channels (position the text anchor)
theta, thetaSize, r, rSizeint | strPolar coord-space aliases for x/w/y/h
keystrInternal per-node key override

Returns a Mark for use in .mark().

Encoding

The text option takes a constant, a field name (a string column in your data), or a callable (row) -> str evaluated per row:

python
text(text="Hello")               # constant string
text(text="name")                # content from a field
text(text=lambda row: f"{row['amount']}%")  # computed per row

Examples

python
# Static label
chart([{"label": "GoFish"}]).mark(text(text="label", fontSize=24, fill="steelblue"))

# Value labels: layer text totals on top of bars
layer([
    chart(seafood)
        .flow(spread(by="lake", dir="x"))
        .mark(rect(h="count").name("bars")),
    chart(selectAll("bars"))
        .flow(group(by="datum.lake"))
        .mark(lambda d: spread(
            [d[0], text(text=str(sum(r["count"] for r in d[0].datum)))],
            dir="y", alignment="middle", spacing=10,
        )),
])

# Computed per-row label
chart(bottles).mark(text(text=lambda d: f"{d['amount']}%", fontSize=35, fill="#666"))