Skip to content

Commit b727966

Browse files
committed
Rename Chart.saveHtmlAs -> Chart.saveHtml. Refactor and temporaily fix Chart.ShowAsImage
1 parent 319128f commit b727966

File tree

3 files changed

+52
-51
lines changed

3 files changed

+52
-51
lines changed

src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -966,10 +966,10 @@ module GenericChartExtensions =
966966

967967

968968
/// Save chart as html single page
969-
[<CompiledName("SaveHtmlAs")>]
969+
[<CompiledName("SaveHtml")>]
970970
[<Extension>]
971-
member this.SaveHtmlAs(pathName: string, [<Optional; DefaultParameterValue(null)>] ?Verbose) =
972-
Chart.saveHtmlAs pathName this
971+
member this.SaveHtml(path: string, [<Optional; DefaultParameterValue(null)>] ?OpenInBrowser: bool) =
972+
this |> Chart.saveHtml (path, ?OpenInBrowser = OpenInBrowser)
973973

974974
/// Show chart in browser
975975
[<CompiledName("Show")>]

src/Plotly.NET/ChartAPI/Chart.fs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,44 @@ type Chart =
1818
//==============================================================================================================
1919

2020
/// Save chart as html single page
21-
[<CompiledName("SaveHtmlAs")>]
22-
static member saveHtmlAs(pathName: string, [<Optional; DefaultParameterValue(null)>] ?Verbose) =
21+
[<CompiledName("SaveHtml")>]
22+
static member saveHtml(path: string, [<Optional; DefaultParameterValue(null)>] ?OpenInBrowser: bool) =
2323
fun (ch: GenericChart) ->
24+
let show = defaultArg OpenInBrowser false
25+
2426
let html = GenericChart.toEmbeddedHTML ch
25-
let file = sprintf "%s.html" pathName // remove file extension
26-
File.WriteAllText(file, html)
2727

28-
let verbose = defaultArg Verbose false
28+
let file =
29+
if path.EndsWith(".html") then
30+
path
31+
else
32+
$"{path}.html"
2933

30-
if verbose then
31-
file |> openOsSpecificFile
34+
File.WriteAllText(file, html)
35+
if show then file |> openOsSpecificFile
3236

3337
/// Show chart in browser
3438
[<CompiledName("Show")>]
3539
static member show(ch: GenericChart) =
3640
let guid = Guid.NewGuid().ToString()
37-
let html = GenericChart.toEmbeddedHTML ch
3841
let tempPath = Path.GetTempPath()
3942
let file = sprintf "%s.html" guid
4043
let path = Path.Combine(tempPath, file)
41-
File.WriteAllText(path, html)
42-
path |> openOsSpecificFile
44+
ch |> Chart.saveHtml (path, true)
4345

4446
/// Show chart in browser
4547
[<CompiledName("ShowAsImage")>]
4648
static member showAsImage (format: StyleParam.ImageFormat) (ch: GenericChart) =
4749
let guid = Guid.NewGuid().ToString()
48-
let html = GenericChart.toEmbeddedImage format ch
4950
let tempPath = Path.GetTempPath()
5051
let file = sprintf "%s.html" guid
5152
let path = Path.Combine(tempPath, file)
53+
54+
let html =
55+
ch
56+
|> Chart.withAdditionalHeadTags [ """<script src="https://unpkg.com/@plotly/[email protected]/d3.js"></script>""" ]
57+
|> GenericChart.toEmbeddedImage format
58+
5259
File.WriteAllText(path, html)
5360
path |> openOsSpecificFile
5461

src/Plotly.NET/ChartAPI/GenericChart.fs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ module HTML =
4848
</html>"""
4949

5050

51-
// let chart =
52-
// """<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;"><!-- Plotly chart will be drawn inside this DIV --></div>
53-
//<script>
54-
// var data = [DATA];
55-
// var layout = [LAYOUT];
56-
// var config = [CONFIG];
57-
// Plotly.newPlot('[ID]', data, layout, config);
58-
//</script>"""
5951
let chart =
6052
let newScript = new System.Text.StringBuilder()
6153
newScript.AppendLine("""<div id="[ID]"><!-- Plotly chart will be drawn inside this DIV --></div>""") |> ignore
@@ -99,14 +91,13 @@ module HTML =
9991
newScript.ToString()
10092

10193

102-
let staticChart =
94+
let imageChart =
10395
"""<div id="[ID]" style="display: none;"><!-- Plotly chart will be drawn inside this DIV --></div>
10496
105-
<img id="jpg-export"></img>
97+
<img id="chart-image"></img>
10698
10799
<script>
108-
var d3 = Plotly.d3;
109-
var img_jpg= d3.select('#jpg-export');
100+
var img_jpg = d3.select('#chart-image');
110101
var data = [DATA];
111102
var layout = [LAYOUT];
112103
var config = [CONFIG];
@@ -388,12 +379,6 @@ module GenericChart =
388379
.Replace("[CONFIG]", configJson)
389380
|> DisplayOptions.replaceHtmlPlaceholders displayOpts
390381

391-
392-
393-
394-
395-
396-
397382
/// Converts a GenericChart to it HTML representation and set the size of the div
398383
let toChartHtmlWithSize (width: int) (height: int) (gChart: GenericChart) =
399384
let guid = Guid.NewGuid().ToString()
@@ -412,7 +397,6 @@ module GenericChart =
412397

413398
let displayOpts = getDisplayOptions gChart
414399

415-
416400
HTML
417401
.chart
418402
.Replace("[ID]", guid)
@@ -431,8 +415,16 @@ module GenericChart =
431415

432416
HTML.doc.Replace("[CHART]", chartMarkup) |> DisplayOptions.replaceHtmlPlaceholders displayOpts
433417

418+
[<Obsolete("This function will be dropped in the 2.0 release. Create either a static chart (e.g using Config.init(StaticPlot=true)) or use Plotly.NET.ImageExport")>]
434419
/// Converts a GenericChart to its Image representation
420+
///
421+
/// This function is obsolete and will soon be dropped.
422+
///
423+
/// Either use a static plot config (e.g. myChart |> Chart.withConfig(Config.init(StaticPlot=true)) https://plotly.net/00_3_chart-config.html#Static-plots
424+
///
425+
/// or use the Plotly.NET.ImageExport package https://www.nuget.org/packages/Plotly.NET.ImageExport/
435426
let toChartImage (format: StyleParam.ImageFormat) gChart =
427+
436428
let guid = Guid.NewGuid().ToString()
437429

438430
let tracesJson =
@@ -443,30 +435,32 @@ module GenericChart =
443435
let layout = getLayout gChart
444436
JsonConvert.SerializeObject(layout, jsonConfig)
445437

446-
let html =
447-
HTML
448-
.staticChart
449-
.Replace("[WIDTH]", string 600)
450-
.Replace("[HEIGHT]", string 600)
451-
.Replace("[ID]", guid)
452-
.Replace("[DATA]", tracesJson)
453-
.Replace("[LAYOUT]", layoutJson)
454-
.Replace("[IMAGEFORMAT]", format.ToString().ToLower())
455-
.Replace("[CONFIG]", "{}")
438+
let displayOpts = getDisplayOptions gChart
439+
440+
HTML
441+
.imageChart
442+
.Replace("[WIDTH]", string 600)
443+
.Replace("[HEIGHT]", string 600)
444+
.Replace("[ID]", guid)
445+
.Replace("[DATA]", tracesJson)
446+
.Replace("[LAYOUT]", layoutJson)
447+
.Replace("[IMAGEFORMAT]", format.ToString().ToLower())
448+
.Replace("[CONFIG]", "{}")
449+
|> DisplayOptions.replaceHtmlPlaceholders displayOpts
456450

457-
html
458451

459452
/// Converts a GenericChart to an image and embeds it into a html page
460453
let toEmbeddedImage (format: StyleParam.ImageFormat) gChart =
461-
let html =
462-
let chartMarkup = toChartImage format gChart
463454

464-
HTML
465-
.doc
466-
.Replace("[CHART]", chartMarkup)
467-
.Replace("[CONFIG]", "{}")
455+
let chartMarkup = toChartImage format gChart
456+
457+
let displayOpts = getDisplayOptions gChart
468458

469-
html
459+
HTML
460+
.doc
461+
.Replace("[CHART]", chartMarkup)
462+
.Replace("[CONFIG]", "{}")
463+
|> DisplayOptions.replaceHtmlPlaceholders displayOpts
470464

471465

472466
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.

0 commit comments

Comments
 (0)