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.

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

Signature ​

ts
text({ text, fill = "black", stroke?, strokeWidth = 0, fontSize = 12,
       fontFamily = "system-ui, sans-serif", fontStyle?, fontWeight?, rotate = 0,
       textAnchor = "start", debugBoundingBox = false, x?, y?, w?, h? })

Parameters ​

A text label. Box geometry via the shared dims channels positions the text anchor.

OptionTypeDefaultDescription
keystringInternal per-node key override.
textstring | FieldExprRequired. Text content (raw channel — a literal, field name, or accessor).
fillstring | FieldExpr"black"Fill color, or a field name for a color scale.
strokestring | FieldExprStroke color.
strokeWidthnumber0Stroke width in pixels.
filterstringRaw SVG filter attribute.
fontSizenumber12Font size in pixels.
fontFamilystring"system-ui, sans-serif"Font family.
fontStylestringRaw CSS font-style (e.g. "italic").
fontWeightnumber | stringCSS font-weight (e.g. 300, 700, "bold").
debugBoundingBoxbooleanfalseDraw the text's bounding box, for layout debugging.
rotatenumber0Rotation in degrees, applied in the chart's y-up world frame about the text anchor.
textAnchor"start" | "middle" | "end""start"Where the text anchor — the local origin rotate pivots about and dims channels position — sits along the string: its first character, center, or last character.

Box dimensions ​

OptionTypeDefaultDescription
xnumber | string | FieldExprLeft edge position.
cxnumber | string | FieldExprCenter x.
x2number | string | FieldExprRight edge position.
wnumber | string | FieldExprWidth.
emXbooleanEmbed x in the parent's x space.
ynumber | string | FieldExprTop/bottom edge position (y-up: bottom).
cynumber | string | FieldExprCenter y.
y2number | string | FieldExprOther y edge position.
hnumber | string | FieldExprHeight.
emYbooleanEmbed y in the parent's y space.
dimsRecord<string, AxisDimsValue>Box dimensions by axis name: x/y, or a name the enclosing coordinate space declares (polar theta/r, geo lon/lat). Each value is a position (like x) or an interval {min, center, max, size, embedded}.

Examples ​

ts
// Static label
.mark(text({ text: "Hello", fontSize: 24, fill: "steelblue" }))

// Text content read from a data field
.mark(text({ text: "name" }))

// 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: "lake" }))
    .mark((d) =>
      spread({ dir: "y", alignment: "middle", spacing: 10 }, [
        d[0],
        text({ text: String(sumBy(d[0].datum, "count")) }),
      ])
    ),
]);

// Rotated y-axis title (reads bottom-to-top)
.mark(text({ text: "count", rotate: 90, fontSize: 13 }))

// Italic label
.mark(text({ text: "note", fontStyle: "italic" }))

// Light-weight label
.mark(text({ text: "caption", fontWeight: 300 }))