Skip to content

Commit a0efeda

Browse files
committed
use Giraffe.ViewEngine for rendering html
1 parent 9f7edb8 commit a0efeda

File tree

10 files changed

+201
-380
lines changed

10 files changed

+201
-380
lines changed

src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs

+4-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open Plotly.NET.LayoutObjects
44
open Plotly.NET.TraceObjects
55
open System
66
open System.IO
7+
open Giraffe.ViewEngine
78

89
open DynamicObj
910
open GenericChart
@@ -941,19 +942,19 @@ module GenericChartExtensions =
941942
/// Show chart in browser
942943
[<CompiledName("WithDescription")>]
943944
[<Extension>]
944-
member this.WithDescription(description: ChartDescription) =
945+
member this.WithDescription(description: XmlNode list) =
945946
this |> Chart.withDescription description
946947

947948
/// Adds the given additional script tags on the chart's DisplayOptions. They will be included in the document's <head>
948949
[<CompiledName("WithAdditionalHeadTags")>]
949950
[<Extension>]
950-
member this.WithAdditionalHeadTags(additionalHeadTags: seq<string>) =
951+
member this.WithAdditionalHeadTags(additionalHeadTags: XmlNode list) =
951952
this |> Chart.withAdditionalHeadTags additionalHeadTags
952953

953954
/// Sets the given additional script tags on the chart's DisplayOptions. They will be included in the document's <head>
954955
[<CompiledName("WithHeadTags")>]
955956
[<Extension>]
956-
member this.WithHeadTags(headTags: seq<string>) = this |> Chart.withHeadTags headTags
957+
member this.WithHeadTags(headTags: XmlNode list) = this |> Chart.withHeadTags headTags
957958

958959
/// Adds the necessary script tags to render tex strings to the chart's DisplayOptions
959960
[<CompiledName("WithMathTex")>]
@@ -979,11 +980,6 @@ module GenericChartExtensions =
979980
[<Extension>]
980981
member this.Show() = this |> Chart.show
981982

982-
/// Show chart in browser
983-
[<CompiledName("ShowAsImage")>]
984-
[<Extension>]
985-
member this.ShowAsImage(format: StyleParam.ImageFormat) = this |> Chart.showAsImage format
986-
987983
/// Sets the polar object with the given id on the chart layout
988984
[<CompiledName("WithPolar")>]
989985
member this.WithPolar(polar: Polar, [<Optional; DefaultParameterValue(null)>] ?Id) =

src/Plotly.NET/ChartAPI/Chart.fs

+16-58
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open Plotly.NET.ConfigObjects
77
open DynamicObj
88
open System
99
open System.IO
10-
10+
open Giraffe.ViewEngine
1111
open GenericChart
1212
open System.Runtime.InteropServices
1313

@@ -52,33 +52,6 @@ type Chart =
5252
let path = Path.Combine(tempPath, file)
5353
ch |> Chart.saveHtml (path, true)
5454

55-
/// <summary>
56-
/// Saves the given chart as a temporary html file containing a static image of the chart and opens it in the browser.
57-
///
58-
/// IMPORTANT: this is not the same as static image generation. The file still needs to be opened in the browser to generate the image, as it is done via a js script in the html.
59-
///
60-
/// For real programmatic static image export use Plotly.NET.ImageExport (https://www.nuget.org/packages/Plotly.NET.ImageExport/)
61-
///
62-
/// This yields basically the same results as using `StaticPlot = true` for the chart's config.
63-
/// </summary>
64-
/// <param name="format">The image format for the static chart</param>
65-
/// <param name="ch">The chart to show in the browser</param>
66-
[<Obsolete("This function will be dropped in the 2.0 release. It is recommended to either create static plots or use Plotly.NET.ImageExport instead.")>]
67-
[<CompiledName("ShowAsImage")>]
68-
static member showAsImage (format: StyleParam.ImageFormat) (ch: GenericChart) =
69-
let guid = Guid.NewGuid().ToString()
70-
let tempPath = Path.GetTempPath()
71-
let file = sprintf "%s.html" guid
72-
let path = Path.Combine(tempPath, file)
73-
74-
let html =
75-
ch
76-
|> Chart.withAdditionalHeadTags [ """<script src="https://unpkg.com/@plotly/[email protected]/d3.js"></script>""" ]
77-
|> GenericChart.toEmbeddedImage format
78-
79-
File.WriteAllText(path, html)
80-
path |> openOsSpecificFile
81-
8255
// #######################
8356
/// Create a combined chart with the given charts merged
8457
[<CompiledName("Combine")>]
@@ -3214,33 +3187,20 @@ type Chart =
32143187

32153188
/// Show chart in browser
32163189
[<CompiledName("WithDescription")>]
3217-
static member withDescription (description: ChartDescription) (ch: GenericChart) =
3218-
ch |> mapDisplayOptions (DisplayOptions.style (Description = description))
3190+
static member withDescription (description: XmlNode list) (ch: GenericChart) =
3191+
ch |> mapDisplayOptions (DisplayOptions.addDescription description)
3192+
32193193

32203194

32213195
/// Adds the given additional html tags on the chart's DisplayOptions. They will be included in the document's <head>
32223196
[<CompiledName("WithAdditionalHeadTags")>]
3223-
static member withAdditionalHeadTags (additionalHeadTags: seq<string>) (ch: GenericChart) =
3224-
ch
3225-
|> mapDisplayOptions (fun d ->
3226-
let tags =
3227-
d.TryGetTypedValue<seq<string>>("AdditionalHeadTags")
3228-
3229-
let newTags =
3230-
tags
3231-
|> Option.map (fun tags ->
3232-
seq {
3233-
yield! tags
3234-
yield! additionalHeadTags
3235-
})
3236-
|> Option.defaultValue additionalHeadTags
3237-
3238-
d |> DisplayOptions.style (AdditionalHeadTags = newTags))
3197+
static member withAdditionalHeadTags (additionalHeadTags: XmlNode list) (ch: GenericChart) =
3198+
ch |> mapDisplayOptions (DisplayOptions.addAdditionalHeadTags additionalHeadTags)
32393199

32403200
/// Sets the given additional head tags on the chart's DisplayOptions. They will be included in the document's <head>
32413201
[<CompiledName("WithHeadTags")>]
3242-
static member withHeadTags (headTags: seq<string>) (ch: GenericChart) =
3243-
ch |> mapDisplayOptions (DisplayOptions.style (AdditionalHeadTags = headTags))
3202+
static member withHeadTags (headTags: XmlNode list) (ch: GenericChart) =
3203+
ch |> mapDisplayOptions (fun d -> {d with AdditionalHeadTags = headTags})
32443204

32453205

32463206
/// Adds the necessary script tags to render tex strings to the chart's DisplayOptions
@@ -3253,24 +3213,22 @@ type Chart =
32533213

32543214
let tags =
32553215
if version = 2 then
3256-
[
3257-
"""<script type="text/x-mathjax-config;executed=true">MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true}});</script>"""
3258-
"""<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML%2CSafe.js&ver=4.1"></script>"""
3259-
]
3216+
Globals.MATHJAX_V2_TAGS
32603217
else
3261-
[
3262-
"""<script>MathJax = {tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]}};</script>"""
3263-
"""<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>"""
3264-
]
3218+
Globals.MATHJAX_V3_TAGS
32653219

32663220
(fun (ch: GenericChart) ->
32673221

32683222
if (AppendTags |> Option.defaultValue true) then
32693223
ch
32703224
|> Chart.withAdditionalHeadTags tags
3271-
|> Chart.withConfigStyle()
3225+
|> Chart.withConfigStyle(TypesetMath=true)
32723226
else
3273-
ch |> Chart.withHeadTags tags)
3227+
ch
3228+
|> Chart.withHeadTags tags
3229+
|> Chart.withConfigStyle(TypesetMath=true)
3230+
)
3231+
32743232

32753233
/// Sets the color axis with the given id on the chart layout
32763234
[<CompiledName("WithColorAxis")>]

0 commit comments

Comments
 (0)