Text
text draws one label per row at (x, y). format sets how a number displays, as a d3-format string or a function, without changing the data. It takes the universal edits (move(), cycle(), rotate()), and editText() lets you retype a label.
API reference
text(options) · textX(options) · textY(options)
Import plot from elicitjs. text positions from x AND y; textX / textY are 1-D labels along one axis (the other parks at centre).
text({ channels, orientation, format, wrap, lineHeight, edits, id }) → Mark
textX(options) → Mark // value on x
textY(options) → Mark // value on y| Option | Type · default | Description |
|---|---|---|
channels | objectdefault{} | Channel map. See Channels. |
orientation | 'vertical' | | Places the label along one axis and parks it at the plot centre on the other ( 'vertical' = y). textY / textX set it. Bare text places each axis through its own channel, and at the centre where a channel is unbound. |
text, | shorthand | Constant shorthands, e.g. text({ dy: -8, lineAnchor: "bottom" }). |
format | string | defaultString | Display formatter: a d3-format string (e.g. ".1f") or (v) => string. Display-only — the field stays raw. Helpers live on format (format.number, format.percent, …). |
wrap | number | | Wrap the label at this pixel width. true wraps at the plot width. A function (d, i, data) => number gives a per-datum width, for a label that has to fit the box it sits in. |
lineHeight | numberdefault1.35 × fontSize | Pixel step between wrapped lines. |
edits | Edit[] | Mark-level edits; per-channel edits live in channels[ch].edit. |
| Channel | Type | Description |
|---|---|---|
x / y | linear | | Position. A missing axis parks the label at that dimension’s centre. |
text | field | | The value to draw (raw — no scale; passed through format). |
fontSize | field | | Size in px, raw. |
textAnchor | field | | Horizontal anchor: start·middle·end. |
lineAnchor | field | | Vertical anchor: top·middle·bottom (maps to SVG dominant-baseline). |
dx / dy | field | | Pixel offsets from the encoded (x, y). Visual-only — drag still inverts the pointer through the scale. |
angle | field | | Rotation in degrees (scaled when a scale is declared, so rotate() is an exact inverse). |
fill, | const | | Standard style surface. |
A mark emitting one text per datum.
Labels from data
x/y position the label, and text is its string. text, fontSize, textAnchor, lineAnchor, dx and dy take a field or a constant and are used as-is. angle is in degrees. Offset and anchor keep a label clear of the mark it labels.
A labelled scatter
point for the dots, text for the labels above them — dy + lineAnchor park the string above the point.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ gdp: 3, life: 6, name: "Ada" },],schema: {gdp: { type: "quantitative", domain: [0, 10] },life: { type: "quantitative", domain: [0, 10] },name: { type: "categorical", domain: ["Ada", "Grace", "Alan"] },},marks: [point({ fill: "#50AEB7",channels: { x: { field: "gdp" }, y: { field: "life" } } }),text({ fontSize: 12, dy: -10, lineAnchor: "bottom",channels: {x: { field: "gdp" }, y: { field: "life" },text: { field: "name" },} }),],});mount(chart.dom());
Formatted numeric labels
format is display-only (a d3-format string or a helper from format.*). The field stays the raw number, so a later drag still inverts correctly.
const chart = Elicit({width: 360, height: 240,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ cat: "A", n: 0.42 },],schema: {cat: { type: "categorical", domain: ["A", "B", "C"] },n: { type: "quantitative", domain: [0, 1] },},marks: [barY({ fill: "#1B3F60",channels: { x: { field: "cat" }, y: { field: "n" } } }),text({ fontSize: 12, dy: -6, lineAnchor: "bottom",format: format.percent(".0%"),channels: {x: { field: "cat" }, y: { field: "n" },text: { field: "n" },} }),],});mount(chart.dom());
Line chart with value labels
lineY draws the series; text sits on the same (x, y) with dy above each point.
const chart = Elicit({width: 400, height: 260,margins: { top: 20, right: 16, bottom: 28, left: 34 },data: [{ t: 0, n: 40 }, { t: 1, n: 62 }, { t: 2, n: 48 },{ t: 3, n: 78 }, { t: 4, n: 60 }, { t: 5, n: 84 },],schema: {t: { type: "quantitative", domain: [0, 5] },n: { type: "quantitative", domain: [0, 100] },},marks: [lineY({stroke: "#50AEB7", strokeWidth: 2.5, curve: "catmullRom",channels: { x: { field: "t" }, y: { field: "n" } },}),text({fontSize: 11, dy: -10, lineAnchor: "bottom",fill: "#2A7880", format: ".0f",channels: {x: { field: "t" }, y: { field: "n" },text: { field: "n" },},}),],});mount(chart.dom());
Editing labels
A direct-pick edit makes a label interactive. Use the universal edits to drag, rotate or cycle a label, or editText() to retype its content.
Drag to reposition
A mark-level move({ channels: ["x", "y"] }) moves the label and writes x and y back through the scales. Attach an edit on a channel or in edits.
const chart = Elicit({width: 360, height: 240,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x: 3, y: 4, label: "drag me" },{ x: 6, y: 7, label: "and me" },],schema: {x: { type: "quantitative", domain: [0, 10] },y: { type: "quantitative", domain: [0, 10] },label: { type: "categorical", domain: ["drag me", "and me"] },},marks: [text({ fontSize: 13, fill: "#2A7880",channels: {x: { field: "x" }, y: { field: "y" },text: { field: "label" },},edits: [ move({ channels: ["x", "y"] }) ],}),],});mount(chart.dom());
Draggable numeric readout
text and y read the same field, and move() on y rewrites it, so the number updates as you drag. format sets how it displays.
const chart = Elicit({width: 360, height: 260,margins: { top: 16, right: 16, bottom: 28, left: 40 },data: [ { cat: "A", n: 40 }, { cat: "B", n: 70 } ],schema: {cat: { type: "categorical", domain: ["A", "B"] },n: { type: "quantitative", domain: [0, 100] },},marks: [text({ fontSize: 16, fill: "#1B3F60", format: ".1f",channels: {x: { field: "cat" },y: { field: "n", edit: move() },text: { field: "n" },} }),],});mount(chart.dom());
Type to edit content
editText() wires double-click-to-retype: an inline input opens, Enter commits, Esc cancels.
const chart = Elicit({width: 360, height: 220,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x: 3, y: 5, label: "double-click" },{ x: 7, y: 3, label: "me too" },],schema: {x: { type: "quantitative", domain: [0, 10] },y: { type: "quantitative", domain: [0, 10] },label: { type: "categorical" },},marks: [text({ fontSize: 14, fill: "#5B4A96",channels: {x: { field: "x" }, y: { field: "y" },text: { field: "label", edit: editText() },} }),],});mount(chart.dom());
Drag and retype together
Mark-level drag plus editText on the text channel: drag repositions, double-click retypes.
const chart = Elicit({width: 360, height: 220,margins: { top: 16, right: 16, bottom: 28, left: 34 },data: [{ x: 3, y: 5, label: "move me" },{ x: 7, y: 3, label: "rename me" },],schema: {x: { type: "quantitative", domain: [0, 10] },y: { type: "quantitative", domain: [0, 10] },label: { type: "categorical" },},marks: [text({ fontSize: 14, fill: "#8F6511",channels: {x: { field: "x" }, y: { field: "y" },text: { field: "label", edit: editText() },},edits: [ move({ channels: ["x", "y"] }) ],}),],});mount(chart.dom());