Skip to content
GalleryOpen Garphield

Binding grammar

A binding connects a source to a visual channel. The workbench writes bindings for you; this page is the wire format for authoring them directly - in a .gph document, over the embed protocol, through the MCP tools, or from the Python package. The GPH schema constrains a binding’s shape, but not whether a channel accepts a source. Garphield resolves that against the catalogue below. garphield_style, channel.bind and apply() refuse a binding that does not resolve, with an error code and suggested ids, and the Python package raises BindingError at build time.

Read the ids a graph accepts before writing a binding. Each route lists the graph’s fields with their target and type, the algorithms, and the channels with the types they accept:

  • MCP tools (the hosted connector, garphield-mcp, or WebMCP in an open tab): garphield_inspect with the document’s doc. Add "include": ["algorithms"] for the algorithm catalogue.
  • An open workbench: window.garphield.fields().
  • Python: project.sources().

garphield_style and channel.bind take a bare field name and resolve it to the field on the channel’s target, so you rarely write a field id yourself:

{ "channel": "edgeWidth", "field": "amount_bn" }

resolves to efield:amount_bn. A failed binding names its problem: UNKNOWN_CHANNEL, UNKNOWN_SOURCE, TARGET_MISMATCH (a node source on an edge channel, or the reverse), TYPE_MISMATCH (a type the channel does not accept), INVALID_PARAM or INVALID_SCALE (see Scales).

Bindings live in config.bindings as an array. Each entry is:

{
"channel": "color",
"source": { "kind": "field", "id": "field:team" },
"resultType": "cat"
}
  • channel - the mark or layer the source drives (see Channels).
  • source - { "kind", "id", "params?", "target?" } (see Source kinds).
  • resultType - what the source produces: num, cat, or set. It must be a type the channel accepts, and for an algorithm source it must match what that algorithm produces.
  • scale (optional) - which mark each value draws as, with legend labels, or a pinned numeric range (see Scales).

Channels are camelCase on the wire. The result types a channel accepts and its target (node or edge) are fixed. The Python package also accepts the snake_case alias in the last column as a keyword argument; both resolve to the same camelCase channel on the wire.

Channel (wire) Target Accepts Python alias
size node num node_size
color node cat, num, set node_color
shape node cat, num, set -
image node cat -
glyph node cat, num -
nodeLabel node cat, num node_label
border node cat, set -
pieSlices node cat, num -
edgeColor edge cat, num edge_color
edgeWidth edge num edge_width
edgeStyle edge cat, set -
edgeLabel edge cat, num -
hull node cat, set -
contour node cat, set -
heatmap node num -

A source’s target must match the channel’s target: a node source cannot drive an edge channel, and the reverse. A source’s result type must be in the channel’s Accepts list - for example, size and edgeWidth take only num, so binding a category to either does not resolve.

In the stored form, every edge source carries "target": "edge", an efield: field included; a source without target is a node source. Garphield does not infer it from the id or the channel:

{
"channel": "edgeColor",
"source": { "kind": "field", "id": "efield:relation", "target": "edge" },
"resultType": "cat"
}

Without it, garphield_validate and validateState() report TARGET_MISMATCH at source.target. The workbench and the embed skip a binding they cannot draw, log it to the console, and the workbench names it in a notice when the document opens.

Without a scale, a binding draws with the defaults: palette colours by frequency, shapes in turn, and a numeric range stretched over the data. A scale names the marks instead. Values a categorical scale leaves out keep the defaults, which skip the colours and shapes the scale already uses.

A cat or set binding takes categories, in legend order, and an optional missing entry for items with no value:

{
"channel": "shape",
"source": { "kind": "field", "id": "field:kind" },
"resultType": "cat",
"scale": {
"categories": [
{ "value": "Song", "mark": "circle", "label": "Songs" },
{ "value": "Phrase", "mark": "diamond" },
{ "value": "Album", "label": "Albums" }
],
"missing": { "mark": "triangle", "label": "Unknown" }
}
}
  • value is matched by its text form, so 3 and "3" are the same value. A set binding’s values are true (members) and false.
  • mark is optional; an entry with only a label keeps the default mark and renames the value in the legend. Each entry needs a mark, a label or both.
  • The legend and the sidebar show label in place of the value.
Channel Categorical mark
color, edgeColor, border, hull, contour a colour: #rrggbb, #rgb, rgb() or hsl()
shape circle, square, triangle or diamond
edgeStyle solid or dashed
glyph a few characters, an inline <svg>, or an image URL

A num binding takes a domain and a range. Values in the domain map onto the range, and values outside it are clamped:

{
"channel": "size",
"source": { "kind": "algorithm", "id": "pagerank" },
"resultType": "num",
"scale": { "domain": [0, 0.05], "range": [4, 18] }
}
Channel Range
size two node radii in px (default [6, 22])
edgeWidth two link widths in px (default [0.6, 9])
color, edgeColor two or more colours, evenly spaced along the ramp

Other channels (image, nodeLabel, edgeLabel, pieSlices, heatmap, and shape bound to numbers) take no scale. A scale of the wrong form, or with a mark the channel cannot draw, fails with INVALID_SCALE at its path, such as bindings.1.scale.categories.2.mark: "star" is not a shape; use circle, square, triangle or diamond. The workbench and the embed draw such a binding without its scale and report the scale as skipped.

To see what a binding draws now, in the same form, call garphield_inspect with "include": ["scales"], or read scales from window.garphield.view(). Copy an entry into the binding’s scale to pin it. channel.bind takes a scale, channel.scale sets or clears one on a bound channel, and the sidebar’s marks button edits one value at a time.

  • num - a numeric value per node or edge. Drives size, edge width, heatmap, or any channel that lists num.
  • cat - a categorical value. Drives colour, shape, labels, contours, and hulls.
  • set - a membership set. Drives colour, shape, borders, hulls, contours, and link style. To capture link membership, select nodes and use Save set → Include links between selected nodes, or run set.add with includeEdges: true. Only links whose two endpoints are selected are captured; the set keeps that snapshot through later selections, undo/redo, and project saves.
  • field - a value already on the graph. The id is field:<name> for a node attribute or efield:<name> for an edge attribute (see Field ids).
  • algorithm - a value computed in the browser, such as degree or louvain. The id is the bare algorithm id; optional params tune it (for example {"resolution": 1.2} for louvain). An algorithm’s own target and produced type are fixed - see Sources and algorithms for the catalogue. Edge algorithms have "target": "edge".
  • transform - a graph transformation. These live in config.filterStack, not in a visual channel, and produce trf.
  • set - a saved set. A set can anchor nodes or edges; its target is resolved from the channel it is dropped on.

A field id names an attribute by its internal, on-the-wire name:

  • Node attribute team becomes field:team.
  • Edge attribute weight becomes efield:weight.

Most attribute names pass through unchanged. A few names are reserved because the wire format uses them for structure: id on a node; source, target, and key on an edge; and any name beginning with _gph: or @gf:. When your data uses a reserved name, Garphield escapes it to @gf:attr:v1:<encoded> in the node or edge row and records the original name in config.interop.attributeNames, so the field id becomes field:@gf:attr:v1:<encoded>. You rarely write these by hand. Read the exact ids from one of the discovery routes.

Project.from_networkx() and Project.from_pandas() take the snake_case channel keywords and write the bindings for you, so you do not hand-author the JSON:

import garphield as gph
project = gph.Project.from_networkx(
graph,
node_color="kind", # an existing attribute -> field:kind
node_size=gph.algorithm("degree"), # a computed source
layout="force",
)

from_networkx also accepts a mapping, a callable, or a partition (a list of node sets) for a channel; it materializes those into a node field and binds it. Its scales keyword gives a binding a scale, keyed by the channel keyword: scales={"node_color": {"categories": [{"value": "a", "mark": "#3b87e5", "label": "Team A"}]}}. from_pandas binds by column name, an algorithm(...) spec, layout, or pos; for a per-row value, add a column to the frame and bind it by name.

Discover the bindable ids and their types without opening a browser:

for source in project.sources():
print(source.id, source.kind, source.target, source.result_type)
# field:kind field node cat
# efield:weight field edge num
# degree algorithm node num
# ...

Every binding is validated when the Project is built. An unknown channel, a missing field, an unknown algorithm, a result type the channel does not accept, or a scale the channel cannot draw raises BindingError in Python, so a dead binding never renders in a browser you cannot see.

See Visual channels for what each channel is for, Sources and algorithms for the algorithm catalogue, and The .gph document for the surrounding project shape.