As Plotly.NET generates static html pages that contain charts rendered by plotly.js, static image export needs a lot more overhead under the hood than you might expect. The underlying renderer needs to execute javascript, leading to the usage of headless browsers.
The package Plotly.NET.ImageExport
contains extensions for Plotly.NET to render static images. It is designed with extensibility in mind and
it is very easy to add a new rendering engine. The current engines are provided:
Rendering engine |
Type |
Prerequisites |
---|---|---|
headless browser |
By referencing the Plotly.NET.ImageExport
package, you get access to:
Chart.SaveJPG
Chart.SavePNG
Chart.SaveSVG
(and Extensions for C# style fluent interfaces by opening the GenericChartExtensions
namespace)
The parameters for all three functions are exactly the same.
open Plotly.NET
open Plotly.NET.ImageExport
let exampleChart =
Chart.Histogram2DContour([ 1.; 2.; 2.; 4.; 5. ], [ 1.; 2.; 2.; 4.; 5. ])
exampleChart
|> Chart.saveJPG ("/your/path/without/extension/here", Width = 300, Height = 300)
By referencing the Plotly.NET.ImageExport
package, you get access to:
Chart.toBase64JPGString
Chart.toBase64PNGString
Chart.toSVGString
(and Extensions for C# style fluent interfaces by opening the GenericChartExtensions
namespace)
let base64JPG = exampleChart |> Chart.toBase64JPGString (Width = 300, Height = 300)
It is very easy to construct a html tag that includes this image via a base64 uri. For SVGs, not even that is necessary and just the SVG string can be used.
$"""<img
src= "{base64JPG}"
/>"""