Skip to content

Commit 5b3847b

Browse files
committed
Add StaticChart extension
1 parent 7c2e311 commit 5b3847b

File tree

7 files changed

+104
-11
lines changed

7 files changed

+104
-11
lines changed

src/FSharp.Plotly.WPF/ChartWPF.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ module ChartWPF =
2121
let html = GenericChart.toEmbeddedHTML ch
2222
ViewContainer.showHTML html
2323

24+
25+
/// Save chart as image
26+
static member SaveImageAs (format:StyleParam.ImageFormat) pathName (ch:GenericChart,?Verbose) =
27+
let html = GenericChart.toEmbeddedImage format ch
28+
let wnd,browser = ViewContainer.createContainerWithBrowser()
29+
browser.NavigateToString (html)
30+
let web = new HtmlAgilityPack.HtmlDocument()
31+
web.LoadHtml(browser.Document.ToString())
32+
let url = web.GetElementbyId("jpg-export")//.GetAttributeValue("src")
33+
url.Attributes
34+
//let wc = new System.Net.WebClient()
35+
//wc.DownloadFile()
36+
37+
38+

src/FSharp.Plotly.WPF/FSharp.Plotly.WPF.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="HtmlAgilityPack" Version="1.8.4" />
1415
<PackageReference Include="Microsoft.Xaml" Version="4.0.0.1" />
1516
</ItemGroup>
1617

src/FSharp.Plotly.WPF/WpfTest.fsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//#I "./bin/Debug"
2-
#r "./bin/Debug/Newtonsoft.Json.dll"
3-
#r "./bin/Debug/FSharp.Plotly.dll"
4-
#r "./bin/Debug/FSharp.Plotly.WPF.dll"
2+
//#r "./bin/Debug/netstandard2.0/Newtonsoft.Json.dll"
3+
//#r @"D:\Source\FSharp.Plotly\packages\htmlagilitypack\1.8.4\HtmlAgilityPack.dll"
4+
#r "./bin/Debug/netstandard2.0/FSharp.Plotly.dll"
5+
#r "./bin/Debug/netstandard2.0/FSharp.Plotly.WPF.dll"
6+
#r "netstandard"
57

68
open FSharp.Plotly
79
open FSharp.Plotly.WPF
@@ -20,7 +22,7 @@ let yValues' = seq [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
2022
|> Chart.withLineStyle(Width=1);
2123
]
2224
|> Chart.Combine
23-
|> Chart.ShowWPF
25+
//|> Chart.SaveImageAs StyleParam.ImageFormat.SVG ""
2426

2527

2628

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,13 @@ module ChartExtensions =
392392
File.WriteAllText(path, html)
393393
System.Diagnostics.Process.Start(path) |> ignore
394394

395+
/// Show chart in browser
396+
static member ShowAsImage (format:StyleParam.ImageFormat) (ch:GenericChart) =
397+
let guid = Guid.NewGuid().ToString()
398+
let html = GenericChart.toEmbeddedImage format ch
399+
let tempPath = Path.GetTempPath()
400+
let file = sprintf "%s.html" guid
401+
let path = Path.Combine(tempPath, file)
402+
File.WriteAllText(path, html)
403+
System.Diagnostics.Process.Start(path) |> ignore
395404

src/FSharp.Plotly/GenericChart.fs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ module HTML =
2828
Plotly.newPlot('[ID]', data, layout);
2929
</script>"""
3030

31+
let staticChart =
32+
"""<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;display: none;"><!-- Plotly chart will be drawn inside this DIV --></div>
33+
34+
<img id="jpg-export"></img>
35+
36+
<script>
37+
var d3 = Plotly.d3;
38+
var img_jpg= d3.select('#jpg-export');
39+
var data = [DATA];
40+
var layout = [LAYOUT];
41+
Plotly.newPlot('[ID]', data, layout)
42+
// static image in jpg format
43+
44+
.then(
45+
function(gd)
46+
{
47+
Plotly.toImage(gd,{format:'[IMAGEFORMAT]',height: [HEIGHT],width: [WIDTH]})
48+
.then(
49+
function(url)
50+
{
51+
img_jpg.attr("src", url);
52+
53+
}
54+
)
55+
});
56+
</script>"""
3157

3258
/// Module to represent a GenericChart
3359
module GenericChart =
@@ -117,8 +143,10 @@ module GenericChart =
117143
|> JsonConvert.SerializeObject
118144

119145
let html =
120-
HTML.chart
121-
.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
146+
HTML.staticChart
147+
//.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
148+
.Replace("[WIDTH]", string 600 )
149+
.Replace("[HEIGHT]", string 600)
122150
.Replace("[ID]", guid)
123151
.Replace("[DATA]", tracesJson)
124152
.Replace("[LAYOUT]", layoutJson)
@@ -152,6 +180,35 @@ module GenericChart =
152180
html
153181

154182

183+
/// Converts a GenericChart to its Image representation
184+
let toChartImage (format:StyleParam.ImageFormat) gChart =
185+
let guid = Guid.NewGuid().ToString()
186+
let tracesJson =
187+
getTraces gChart
188+
|> JsonConvert.SerializeObject
189+
let layoutJson =
190+
getLayout gChart
191+
|> JsonConvert.SerializeObject
192+
193+
let html =
194+
HTML.staticChart
195+
//.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
196+
.Replace("[WIDTH]", string 600 )
197+
.Replace("[HEIGHT]", string 600)
198+
.Replace("[ID]", guid)
199+
.Replace("[DATA]", tracesJson)
200+
.Replace("[LAYOUT]", layoutJson)
201+
.Replace("[IMAGEFORMAT]",format.ToString().ToLower())
202+
html
203+
204+
/// Converts a GenericChart to an image and embeds it into a html page
205+
let toEmbeddedImage (format:StyleParam.ImageFormat) gChart =
206+
let html =
207+
let chartMarkup =
208+
toChartImage format gChart
209+
HTML.doc.Replace("[CHART]", chartMarkup)
210+
html
211+
155212

156213
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.
157214
let mapTrace f gChart =

src/FSharp.Plotly/Script.fsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//#I "./bin/Debug"
2-
#r "./bin/Debug/Newtonsoft.Json.dll"
3-
#r "./bin/Debug/FSharp.Plotly.dll"
4-
2+
//#r "./bin/Debug/netstandard2.0/Newtonsoft.Json.dll"
3+
#r "./bin/Debug/netstandard2.0/FSharp.Plotly.dll"
4+
#r "netstandard"
55

66
open FSharp.Plotly
77

@@ -11,7 +11,7 @@ let y = seq [5.; 2.5; 5.; 7.5; 5.; 2.5; 7.5; 4.5; 5.5; 5.]
1111
let y' = seq [2.; 1.5; 5.; 1.5; 3.; 2.5; 2.5; 1.5; 3.5; 1.]
1212

1313
Chart.Spline(x,y',Name="spline")
14-
|> Chart.withYError(Options.Error(Array=[1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]))
14+
//|> Chart.withYError(Options.Error(Array=[1.; 2.; 3.; 4.; 5.; 6.; 7.; 8.; 9.; 10.; ]))
1515

1616

1717
//|> Chart.withLineStyle(Width=2,Dash=StyleParam.DrawingStyle.Dot)
@@ -20,8 +20,9 @@ Chart.Spline(x,y',Name="spline")
2020
//|> Chart.withY_AxisStyle("y axis title")
2121
//|> layoutJson
2222
//|> GenericChart.toChartHtmlWithSize 500 500
23-
|> Chart.Show
2423

24+
//|> Chart.ShowAsImage StyleParam.ImageFormat.SVG
25+
|> Chart.ShowAsImage StyleParam.ImageFormat.SVG
2526

2627

2728

src/FSharp.Plotly/StyleParams.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,3 +717,11 @@ module StyleParam =
717717
//Determines which trace information appear on hover. If `none` or `skip` are set, no information is displayed upon hovering. But, if `none` is set, click and hover events are still fired.
718718

719719

720+
type ImageFormat =
721+
| SVG | PNG | JPEG
722+
static member toString = function
723+
| SVG -> "svg"
724+
| PNG -> "png"
725+
| JPEG -> "jpeg"
726+
727+
static member convert = ImageFormat.toString >> box

0 commit comments

Comments
 (0)