Expressions
NetPanorama allows expressions to be used in several places, including when specifying data/network/layout transforms, and when specifying visual encodings.
Network Panorama handles expressions in a very similar way to expressions in Vega.
Internally, it uses the vega-expression
module to parse a string and generate a function that evaluates it; this is a safer alternative to using eval
.
The generated function has access to several variables:
datum
is the data object for which the expression is being evaluatedindex
is the array index for thedatum
inside its parent arrayparams
is an object containing the current values of parametersbounds
is an object containing the dimensions of the parent graphical element. For example, this can be used to scale and position parts of a glyph within a cell of an adjacency matrix.state
is the global state of the visualization constructed from the specification.
The datum
is always defined.
However, depending on where the expression is being used, others may be undefined when the expression is evaluated (e.g., bounds
will be undefined when applying a data transform as part of the initial data import).
The state
object gives access to all objects defined in the specification. It can be used to access specific measures such as the number of nodes (or links) of a network with length(state.network.nodes)
.
Examples
An expression that checks if the degree
attribute of a data element is above 10
, used to filter out low-degree nodes:
{"type": "filterNodes", "expression": "datum.degree > 10 "}
An expression that returns a number, which is the middle position of a parent's bounding box:
"x": {"expression": "bounds.x + bounds.width/2"}
An expression that returns a string, which is the text of a tooltip combing values from several fields:
"tooltip": {"expression": "datum.target.name + '<-->' + datum.source.name "}