Function: encodeFigure()
ts
function encodeFigure(figure, options?): EncodedFigure;Encode an input figure as JSON-safe data: JSON.parse(JSON.stringify(out)) deep-equals out. The input is never mutated.
- Typed arrays become Plotly's
{ dtype, bdata }(bit-exact, so NaN, -0 and ±Infinity survive). An array of equal-length typed arrays of one type (a heatmapzasFloat64Array[]) becomes one spec withshape: 'rows,cols'.Uint8ClampedArrayis'u1c'.BigInt64Array/BigUint64Arrayhave no Plotly dtype: they become plain number arrays (with abigintwarning), as do BigInt scalars. - Plain numbers follow JSON: NaN and ±Infinity become
null(a gap, as in Plotly), -0 becomes 0. Use a typed array to keep them. - Dates become ISO strings (
toISOString(), UTC), which Holochart reads back as dates. An invalid Date becomesnull. - Functions (ADR-012). A function on an
arrayOkattribute of a trace whose type is inregistryis evaluated into a per-point array (E8.6):fn(point, i, trace)for each pointi, wherepointholdspointNumber: iand the i-th element of each of the trace's top-level data arrays (x,y,customdata, …) and per-point arrays (text, …;'@column'references resolved), andtraceis the input trace. The point count is the shorter ofx/y, else the first data array's length; frame traces without data arrays use the figure trace they update. Every other function (config callbacks such asrenderHover, non-arrayOkattributes, traces without data or registry) is dropped, as is one that throws. - Everything else:
undefinedkeys are omitted andundefinedarray elements becomenull(as in JSON). Objects with atoJSONmethod are encoded through it. Symbols, Maps, Sets, DataViews, ArrayBuffers, other class instances and circular references are dropped (nullin arrays).
Every lossy step reports a SerializeWarning, once per path.
Parameters
| Parameter | Type |
|---|---|
figure | AnyFigure |
options | EncodeFigureOptions |
Returns
Example
ts
const json = encodeFigure(
{
data: [
{
type: 'scatter',
x: Float64Array.of(1, 2),
y: [3, 4],
marker: { color: (p) => (p.y > 3 ? 'red' : 'gray') },
},
],
},
{ registry },
);
// json.data[0].x → { dtype: 'f8', bdata: 'AAAAAAAA8D8AAAAAAAAAQA==' }
// json.data[0].marker.color → ['gray', 'red'] (with a 'function-evaluated' warning)