11(**
22---
3- title: Basics
3+ title: Static image export
44category: General
55categoryindex: 1
66index: 2
@@ -31,10 +31,31 @@ index: 2
3131
3232# Static image export
3333
34- The supported image types are:
35- - jpg via ` Chart.SaveJPG `
36- - png via ` Chart.SavePNG `
37- - svg via ` Chart.SaveSVG `
34+ ### Table of contents
35+
36+ - [ Saving static images] ( #Saving-static-images )
37+ - [ Generating URIs for static chart images] ( #Generating-URIs-for-static-chart-images )
38+ - [ Including static images in dotnet interactive notebooks] ( #Including-static-images-in-dotnet-interactive-notebooks )
39+
40+ 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
41+ than you might expect. The underlying renderer needs to execute javascript, leading to the usage of headless browsers.
42+
43+ The package ` Plotly.NET.ImageExport ` contains extensions for Plotly.NET to render static images. It is designed with extensibility in mind and
44+ it is very easy to add a new rendering engine. The current engines are provided:
45+
46+ | Rendering engine | Type | Prerequisites |
47+ | -| -| -|
48+ | [ PuppeteerSharp] ( https://github.com/hardkoded/puppeteer-sharp ) | headless browser | [ read more here] ( https://github.com/hardkoded/puppeteer-sharp#prerequisites ) |
49+
50+ ## Saving static images
51+
52+ By referencing the ` Plotly.NET.ImageExport ` package, you get access to:
53+
54+ - jpg via ` Chart.SaveJPG `
55+ - png via ` Chart.SavePNG `
56+ - svg via ` Chart.SaveSVG `
57+
58+ (and Extensions for C# style fluent interfaces by opening the ` GenericChartExtensions ` namespace)
3859
3960The parameters for all three functions are exactly the same.
4061*)
@@ -56,51 +77,67 @@ exampleChart
5677 Height= 300
5778)
5879
80+ (* ** condition: ipynb ***)
5981#if IPYNB
60-
6182let imgString = $""" <img
6283 src= "{exampleChart|> Chart.toBase64JPGString(Width=300,Height=300)}"
6384/>"""
64-
6585DisplayExtensions.DisplayAs( imgString, " text/html" )
66-
6786#endif // IPYNB
6887
6988(* **hide***)
7089$""" <img
7190 src= "{exampleChart|> Chart.toBase64JPGString(Width=300,Height=300)}"
7291/>"""
73-
7492(* **include-it-raw***)
7593
7694(**
95+ ## Generating URIs for static chart images
96+
97+ By referencing the ` Plotly.NET.ImageExport ` package, you get access to:
98+
99+ - jpg via ` Chart.toBase64JPGString `
100+ - png via ` Chart.toBase64PNGString `
101+ - svg via ` Chart.toSVGString `
102+
103+ (and Extensions for C# style fluent interfaces by opening the ` GenericChartExtensions ` namespace)
77104
78105*)
79106
80107let base64JPG =
81108 exampleChart
82- |> Chart.toBase64PNGString (
109+ |> Chart.toBase64JPGString (
83110 Width= 300 ,
84111 Height= 300
85112 )
86113
87- #if IPYNB
114+ (**
115+ It is very easy to construct a html tag that includes this image via a base64 uri. For SVGs,
116+ not even that is necessary and just the SVG string can be used.
117+ *)
88118
89- let imgString = $""" <img
119+ (* **do-not-eval***)
120+ $""" <img
90121 src= "{base64JPG}"
91122/>"""
92123
124+ (* ** condition: ipynb ***)
125+ #if IPYNB
126+ let imgString = $""" <img
127+ src= "{base64JPG}"
128+ />"""
93129DisplayExtensions.DisplayAs( imgString, " text/html" )
94-
95130#endif // IPYNB
96131
132+ (* **hide***)
97133$""" <img
98134 src= "{base64JPG}"
99135/>"""
100- (* **include-it-raw***)
101136
137+ (* **include-it-raw***)
102138
103- (**
139+ (**
140+ SVGs can be included without the image tag:
104141*)
105142
106143let svgString =
@@ -110,9 +147,39 @@ let svgString =
110147 Height= 300
111148 )
112149
150+ svgString.Substring( 0 , 300 )
151+ |> printfn " %s "
152+
153+ (* **include-output***)
154+
155+ (**
156+ In fact, the images shown on this site are included just the same way.
157+
158+ ## Including static images in dotnet interactive notebooks
159+
160+ To include the images in dotnet interactive, convert them to html tags as above and include them via
161+ dotnet interactive's ` DisplayAs ` function. The content type for PNG/JPB is "text/html", and "image/svg+xml" for SVG.
162+ *)
163+
164+ let base64PNG =
165+ exampleChart
166+ |> Chart.toBase64PNGString(
167+ Width= 300 ,
168+ Height= 300
169+ )
170+
171+ let svgString2 =
172+ exampleChart
173+ |> Chart.toSVGString(
174+ Width= 300 ,
175+ Height= 300
176+ )
177+
178+ // DisplayExtensions.DisplayAs(base64PNG,"text/html")
179+ // DisplayExtensions.DisplayAs(svgString,"image/svg+xml")
180+
181+ (* ** condition: ipynb ***)
113182#if IPYNB
183+ DisplayExtensions.DisplayAs( base64PNG, " text/html" )
114184DisplayExtensions.DisplayAs( svgString, " image/svg+xml" )
115- #endif // IPYNB
116-
117- svgString
118- (* **include-it-raw***)
185+ #endif // IPYNB
0 commit comments