|
| 1 | +(** |
| 2 | +--- |
| 3 | +title: Encoded typed arrays |
| 4 | +category: General |
| 5 | +categoryindex: 1 |
| 6 | +index: 10 |
| 7 | +--- |
| 8 | +*) |
| 9 | + |
| 10 | +(*** hide ***) |
| 11 | + |
| 12 | +(*** condition: prepare ***) |
| 13 | +#r "nuget: Newtonsoft.JSON, 13.0.3" |
| 14 | +#r "nuget: DynamicObj, 7.0.1" |
| 15 | +#r "nuget: Giraffe.ViewEngine, 1.4.0" |
| 16 | +#r "../../src/Plotly.NET/bin/Release/netstandard2.0/Plotly.NET.dll" |
| 17 | + |
| 18 | +Plotly.NET.Defaults.DefaultDisplayOptions <- |
| 19 | + Plotly.NET.DisplayOptions.init (PlotlyJSReference = Plotly.NET.PlotlyJSReference.NoReference) |
| 20 | + |
| 21 | +(*** condition: ipynb ***) |
| 22 | +#if IPYNB |
| 23 | +#r "nuget: Plotly.NET, {{fsdocs-package-version}}" |
| 24 | +#r "nuget: Plotly.NET.Interactive, {{fsdocs-package-version}}" |
| 25 | +#endif // IPYNB |
| 26 | + |
| 27 | +(** |
| 28 | +# Encoded typed arrays |
| 29 | +
|
| 30 | +[](https://mybinder.org/v2/gh/plotly/plotly.net/gh-pages?urlpath=/tree/home/jovyan/{{fsdocs-source-basename}}.ipynb)  |
| 31 | +[]({{root}}{{fsdocs-source-basename}}.ipynb) |
| 32 | +
|
| 33 | +*Summary:* This page explains how to use `EncodedTypedArray` for efficient data transfer with plotly.js 2.28+. |
| 34 | +
|
| 35 | +### Table of contents |
| 36 | +
|
| 37 | +- [What are encoded typed arrays?](#What-are-encoded-typed-arrays) |
| 38 | +- [Creating EncodedTypedArray values](#Creating-EncodedTypedArray-values) |
| 39 | +- [Using encoded arrays with Scatter](#Using-encoded-arrays-with-Scatter) |
| 40 | +- [Using encoded arrays with Bar and Column charts](#Using-encoded-arrays-with-Bar-and-Column-charts) |
| 41 | +- [Using encoded arrays with Heatmap](#Using-encoded-arrays-with-Heatmap) |
| 42 | +
|
| 43 | +## What are encoded typed arrays? |
| 44 | +
|
| 45 | +plotly.js 2.28.0 introduced support for passing data arrays as **base64-encoded typed arrays** instead of plain JSON arrays. |
| 46 | +This format (`bdata`/`dtype`/`shape`) is significantly more compact and faster to parse for large numeric datasets, |
| 47 | +since it avoids the overhead of JSON number serialization. |
| 48 | +
|
| 49 | +For example, a float64 array `[1.0, 2.0, 3.0]` transmitted as plain JSON looks like `[1.0,2.0,3.0]`, |
| 50 | +while the encoded form is `{"bdata":"AAAAAAAA8D8AAAAAAAAAQA==...","dtype":"f8"}` — smaller and faster to deserialize in the browser. |
| 51 | +
|
| 52 | +Plotly.NET exposes this via the `EncodedTypedArray` type. |
| 53 | +
|
| 54 | +## Creating EncodedTypedArray values |
| 55 | +
|
| 56 | +`EncodedTypedArray` can be constructed from any standard .NET typed array: |
| 57 | +*) |
| 58 | + |
| 59 | +open Plotly.NET |
| 60 | + |
| 61 | +// Float64 (double) arrays — most common for continuous data |
| 62 | +let xEncoded = EncodedTypedArray.ofFloat64Array [| 1.0; 2.0; 3.0; 4.0; 5.0 |] |
| 63 | +let yEncoded = EncodedTypedArray.ofFloat64Array [| 2.0; 4.0; 1.0; 5.0; 3.0 |] |
| 64 | + |
| 65 | +// Int32 arrays — for integer data like indices or counts |
| 66 | +let sourceEncoded = EncodedTypedArray.ofInt32Array [| 0; 1; 2 |] |
| 67 | + |
| 68 | +// Float32 arrays — smaller footprint when full precision is not needed |
| 69 | +let valuesEncoded = EncodedTypedArray.ofFloat32Array [| 1.0f; 2.5f; 0.8f |] |
| 70 | + |
| 71 | +(** |
| 72 | +Supported constructors: |
| 73 | +
|
| 74 | +| Constructor | .NET type | plotly.js dtype | |
| 75 | +|---|---|---| |
| 76 | +| `EncodedTypedArray.ofFloat64Array` | `float[]` | `f8` (64-bit float) | |
| 77 | +| `EncodedTypedArray.ofFloat32Array` | `float32[]` | `f4` (32-bit float) | |
| 78 | +| `EncodedTypedArray.ofInt32Array` | `int[]` | `i4` (32-bit int) | |
| 79 | +| `EncodedTypedArray.ofInt16Array` | `int16[]` | `i2` (16-bit int) | |
| 80 | +| `EncodedTypedArray.ofInt8Array` | `sbyte[]` | `i1` (8-bit int) | |
| 81 | +| `EncodedTypedArray.ofUInt32Array` | `uint32[]` | `u4` (unsigned 32-bit) | |
| 82 | +| `EncodedTypedArray.ofUInt16Array` | `uint16[]` | `u2` (unsigned 16-bit) | |
| 83 | +| `EncodedTypedArray.ofUInt8Array` | `byte[]` | `u1` (unsigned 8-bit) | |
| 84 | +
|
| 85 | +## Using encoded arrays with Scatter |
| 86 | +
|
| 87 | +The encoded convenience overload for `Chart.Scatter` takes `xEncoded` and `yEncoded` as required |
| 88 | +positional arguments instead of plain `x`/`y` sequences: |
| 89 | +*) |
| 90 | + |
| 91 | +let scatterEncoded = |
| 92 | + Chart.Scatter( |
| 93 | + xEncoded = EncodedTypedArray.ofFloat64Array [| 1.0; 2.0; 3.0; 4.0; 5.0 |], |
| 94 | + yEncoded = EncodedTypedArray.ofFloat64Array [| 2.0; 4.0; 1.0; 5.0; 3.0 |], |
| 95 | + mode = StyleParam.Mode.Markers, |
| 96 | + Name = "encoded scatter", |
| 97 | + UseDefaults = false |
| 98 | + ) |
| 99 | + |
| 100 | +(*** condition: ipynb ***) |
| 101 | +#if IPYNB |
| 102 | +scatterEncoded |
| 103 | +#endif // IPYNB |
| 104 | + |
| 105 | +(***hide***) |
| 106 | +scatterEncoded |> GenericChart.toChartHTML |
| 107 | +(***include-it-raw***) |
| 108 | + |
| 109 | +(** |
| 110 | +The same encoded overload is available for `Chart.Point`, `Chart.Line`, `Chart.Bubble`, `Chart.Area`, `Chart.SplineArea`, and `Chart.StackedArea`. |
| 111 | +
|
| 112 | +## Using encoded arrays with Bar and Column charts |
| 113 | +
|
| 114 | +For bar and column charts, the main data array (`values`) is always required and encoded. |
| 115 | +The keys array is optional: |
| 116 | +*) |
| 117 | + |
| 118 | +let barEncoded = |
| 119 | + Chart.Bar( |
| 120 | + valuesEncoded = EncodedTypedArray.ofFloat64Array [| 5.0; 3.0; 7.0; 2.0 |], |
| 121 | + KeysEncoded = EncodedTypedArray.ofInt32Array [| 0; 1; 2; 3 |], |
| 122 | + Name = "encoded bar", |
| 123 | + UseDefaults = false |
| 124 | + ) |
| 125 | + |
| 126 | +(*** condition: ipynb ***) |
| 127 | +#if IPYNB |
| 128 | +barEncoded |
| 129 | +#endif // IPYNB |
| 130 | + |
| 131 | +(***hide***) |
| 132 | +barEncoded |> GenericChart.toChartHTML |
| 133 | +(***include-it-raw***) |
| 134 | + |
| 135 | +(** |
| 136 | +The same pattern applies to `Chart.Column`, `Chart.StackedBar`, and `Chart.StackedColumn`. |
| 137 | +
|
| 138 | +## Using encoded arrays with Heatmap |
| 139 | +
|
| 140 | +For heatmaps, the z matrix is required and encoded; x and y axes are optional and encoded: |
| 141 | +*) |
| 142 | + |
| 143 | +let heatmapEncoded = |
| 144 | + Chart.Heatmap( |
| 145 | + zEncoded = EncodedTypedArray.ofFloat64Array [| 1.0; 2.0; 3.0; 4.0; 5.0; 6.0; 7.0; 8.0; 9.0 |], |
| 146 | + Name = "encoded heatmap", |
| 147 | + UseDefaults = false |
| 148 | + ) |
| 149 | + |
| 150 | +(*** condition: ipynb ***) |
| 151 | +#if IPYNB |
| 152 | +heatmapEncoded |
| 153 | +#endif // IPYNB |
| 154 | + |
| 155 | +(***hide***) |
| 156 | +heatmapEncoded |> GenericChart.toChartHTML |
| 157 | +(***include-it-raw***) |
| 158 | + |
| 159 | +(** |
| 160 | +Note that for heatmaps the z data is passed as a flat 1D encoded array. plotly.js uses the `shape` field |
| 161 | +(rows × columns) to interpret the layout. If you need to specify the shape, build the `EncodedTypedArray` |
| 162 | +manually: |
| 163 | +
|
| 164 | +```fsharp |
| 165 | +// Explicit 3x3 shape |
| 166 | +let z3x3 = |
| 167 | + { EncodedTypedArray.ofFloat64Array [| 1.0..9.0 |] with Shape = Some "3,3" } |
| 168 | +``` |
| 169 | +
|
| 170 | +For more advanced usage including encoded arrays on 3D, domain, and map traces, see the trace-level |
| 171 | +style modules (`Trace2DStyle`, `Trace3DStyle`, `TraceDomainStyle`, etc.) which accept `*Encoded` optional parameters |
| 172 | +for most data-array fields. |
| 173 | +*) |
0 commit comments