|
| 1 | +# Plotly.NET.ImageExport |
| 2 | + |
| 3 | +Render [Plotly.NET](https://plotly.net/) charts as PNG, JPEG, or SVG images using a headless browser powered by PuppeteerSharp. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```shell |
| 8 | +dotnet add package Plotly.NET.ImageExport --version 7.0.0 |
| 9 | +``` |
| 10 | + |
| 11 | +Version 7 targets **.NET Standard 2.0** and depends on **Plotly.NET 6.x**, which NuGet installs automatically. Use a compatible .NET application runtime and an operating system that can run the browser described below. |
| 12 | + |
| 13 | +## Export a chart |
| 14 | + |
| 15 | +This F# script writes `chart.png` in the current directory: |
| 16 | + |
| 17 | +```fsharp |
| 18 | +#r "nuget: Plotly.NET.ImageExport, 7.0.0" |
| 19 | +
|
| 20 | +open Plotly.NET |
| 21 | +open Plotly.NET.ImageExport |
| 22 | +
|
| 23 | +let chart = |
| 24 | + Chart.Line([ (1., 2.); (2., 4.); (3., 3.) ]) |
| 25 | + |> Chart.withDisplayOptions( |
| 26 | + DisplayOptions.init(PlotlyJSReference = PlotlyJSReference.Full) |
| 27 | + ) |
| 28 | +
|
| 29 | +chart |> Chart.savePNG("chart", Width = 800, Height = 450) |
| 30 | +``` |
| 31 | + |
| 32 | +Pass a path **without an extension**; the save functions append `.png`, `.jpg`, or `.svg`. The output directory must already exist and be writable. The example embeds plotly.js so the browser does not need to fetch it from a CDN. |
| 33 | + |
| 34 | +## Output formats |
| 35 | + |
| 36 | +| Format | Save to a file | Return an image string | |
| 37 | +| --- | --- | --- | |
| 38 | +| PNG | `Chart.savePNG` | `Chart.toBase64PNGString` — PNG data URI | |
| 39 | +| JPEG | `Chart.saveJPG` | `Chart.toBase64JPGString` — JPEG data URI | |
| 40 | +| SVG | `Chart.saveSVG` | `Chart.toSVGString` — SVG markup | |
| 41 | + |
| 42 | +These functions accept `Width`, `Height`, and `Scale` options, defaulting to 600, 600, and 1.0. Corresponding `Async` functions, such as `Chart.savePNGAsync`, return tasks that you can await. |
| 43 | + |
| 44 | +## Browser requirements |
| 45 | + |
| 46 | +The default renderer uses PuppeteerSharp 24.40.0 to download a compatible Chrome browser when needed and launch it for each export. Browser setup requires network access and a writable browser download/cache location. The host must permit browser processes and provide the required operating-system libraries; see the [PuppeteerSharp prerequisites](https://github.com/hardkoded/puppeteer-sharp#prerequisites). |
| 47 | + |
| 48 | +To use a preinstalled compatible Chrome/Chromium executable and skip the automatic download, set `PuppeteerSharpRendererOptions.localBrowserExecutablePath` to `Some "/absolute/path/to/chrome"` before exporting. Launch and navigation settings are available through `PuppeteerSharpRendererOptions.launchOptions` and `PuppeteerSharpRendererOptions.navigationOptions`. |
| 49 | + |
| 50 | +Charts that reference external images, fonts, map resources, or a plotly.js CDN may still need network access during rendering. |
| 51 | + |
| 52 | +See the [image export guide](https://plotly.net/general/image-export.html) for more examples and the [release notes](https://github.com/plotly/Plotly.NET/blob/dev/src/Plotly.NET.ImageExport/RELEASE_NOTES.md) for version changes. |
| 53 | + |
| 54 | +Licensed under the [MIT License](https://github.com/plotly/Plotly.NET/blob/dev/LICENSE). |
0 commit comments