Skip to content

Commit

Permalink
fix #374 :
Browse files Browse the repository at this point in the history
- Expand DisplayOptions object with `DocumentTitle`, `DocumentCharset`, `DocumentDescription`, and `DocumentFavicon` fields
- these are overwritten with the fields of the second object on combine
- this makes `AdditionalHeadTags` clearer, as now nothing will accumulate on the head tag on combine that is on any doc by default
- add tests
  • Loading branch information
kMutagene committed Jan 5, 2024
1 parent c26f0c2 commit 488568c
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 66 deletions.
14 changes: 9 additions & 5 deletions src/Plotly.NET/ChartAPI/Chart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3532,25 +3532,29 @@ type Chart =
static member withDisplayOptionsStyle
(
[<Optional; DefaultParameterValue(null)>] ?AdditionalHeadTags: XmlNode list,
[<Optional; DefaultParameterValue(null)>] ?Description: XmlNode list,
[<Optional; DefaultParameterValue(null)>] ?ChartDescription: XmlNode list,
[<Optional; DefaultParameterValue(null)>] ?PlotlyJSReference: PlotlyJSReference
) =
(fun (ch: GenericChart) ->

let displayOpts' =
DisplayOptions.init (
?AdditionalHeadTags = AdditionalHeadTags,
?Description = Description,
?ChartDescription = ChartDescription,
?PlotlyJSReference = PlotlyJSReference
)

GenericChart.addDisplayOptions displayOpts' ch)


/// Show chart in browser
/// <summary>
/// Adds the given chart deycription the to chart's DisplayOptions. They will be included in the document's head
/// </summary>
/// <param name="chartDescription">The chart description to add to the given chart's DisplayOptions object</param>
/// <param name="ch">The chart to add a description to</param>
[<CompiledName("WithDescription")>]
static member withDescription (description: XmlNode list) (ch: GenericChart) =
ch |> GenericChart.mapDisplayOptions (DisplayOptions.addDescription description)
static member withDescription (chartDescription: XmlNode list) (ch: GenericChart) =
ch |> GenericChart.mapDisplayOptions (DisplayOptions.addChartDescription chartDescription)

/// Adds the given additional html tags on the chart's DisplayOptions. They will be included in the document's head
[<CompiledName("WithAdditionalHeadTags")>]
Expand Down
28 changes: 22 additions & 6 deletions src/Plotly.NET/ChartAPI/GenericChart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ type GenericChart =

let displayOpts = GenericChart.getDisplayOptions gChart

let description =
displayOpts |> DisplayOptions.getDescription
let chartDescription =
displayOpts |> DisplayOptions.getChartDescription

let plotlyReference =
displayOpts |> DisplayOptions.getPlotlyReference
Expand All @@ -312,7 +312,7 @@ type GenericChart =
config = configJson,
plotlyReference = plotlyReference
)
yield! description
yield! chartDescription
]

/// <summary>
Expand Down Expand Up @@ -349,16 +349,32 @@ type GenericChart =

let displayOpts = GenericChart.getDisplayOptions gChart

let documentTitle =
(displayOpts |> DisplayOptions.getDocumentTitle)

let documentDescription =
(displayOpts |> DisplayOptions.getDocumentDescription)

let documentCharset =
(displayOpts |> DisplayOptions.getDocumentCharset)

let documentFavicon =
(displayOpts |> DisplayOptions.getDocumentFavicon)

let additionalHeadTags =
(displayOpts |> DisplayOptions.getAdditionalHeadTags)

let description =
(displayOpts |> DisplayOptions.getDescription)
let chartDescription =
(displayOpts |> DisplayOptions.getChartDescription)

let plotlyReference =
displayOpts |> DisplayOptions.getPlotlyReference

HTML.Doc(
documentTitle = documentTitle,
documentDescription = documentDescription,
documentCharset = documentCharset,
documentFavicon = documentFavicon,
chart =
HTML.CreateChartHTML(
data = tracesJson,
Expand All @@ -368,7 +384,7 @@ type GenericChart =
),
plotlyReference = plotlyReference,
AdditionalHeadTags = additionalHeadTags,
Description = description
ChartDescription = chartDescription
)
|> RenderView.AsString.htmlDocument

Expand Down
23 changes: 20 additions & 3 deletions src/Plotly.NET/ChartAPI/HTML.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ type HTML() =
]


static member Doc(chart, plotlyReference: PlotlyJSReference, ?AdditionalHeadTags, ?Description) =
static member Doc(
chart,
documentTitle: string,
documentDescription: string,
documentCharset: string,
documentFavicon: XmlNode,
plotlyReference: PlotlyJSReference,
?AdditionalHeadTags,
?ChartDescription
) =
let additionalHeadTags =
defaultArg AdditionalHeadTags []

let description = defaultArg Description []
let chartDescription = defaultArg ChartDescription []

let plotlyScriptRef =
match plotlyReference with
Expand All @@ -68,9 +77,17 @@ type HTML() =
[]
[
plotlyScriptRef
title [] [ str documentTitle ]
meta [ _charset documentCharset ]
meta
[
_name "description"
_content documentDescription
]
documentFavicon
yield! additionalHeadTags
]
body [] [ yield! chart; yield! description ]
body [] [ yield! chart; yield! chartDescription ]
]

static member CreateChartHTML(data: string, layout: string, config: string, plotlyReference: PlotlyJSReference) =
Expand Down
29 changes: 11 additions & 18 deletions src/Plotly.NET/Defaults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,17 @@ module Defaults =

let mutable DefaultDisplayOptions =
DisplayOptions.init (
PlotlyJSReference = CDN $"https://cdn.plot.ly/plotly-{Globals.PLOTLYJS_VERSION}.min.js",
AdditionalHeadTags =
[
title [] [ str "Plotly.NET Datavisualization" ]
meta [ _charset "UTF-8" ]
meta
[
_name "description"
_content "A plotly.js graph generated with Plotly.NET"
]
link
[
_id "favicon"
_rel "shortcut icon"
_type "image/png"
_href $"data:image/png;base64,{Globals.LOGO_BASE64}"
]
]
DocumentTitle = "Plotly.NET Datavisualization",
DocumentDescription = "A plotly.js graph generated with Plotly.NET",
DocumentCharset = "UTF-8",
DocumentFavicon =
(link [
_id "favicon"
_rel "shortcut icon"
_type "image/png"
_href $"data:image/png;base64,{Globals.LOGO_BASE64}"
]),
PlotlyJSReference = CDN $"https://cdn.plot.ly/plotly-{Globals.PLOTLYJS_VERSION}.min.js"
)

/// The default chart template. Default: ChartTemplates.plotly
Expand Down
Loading

0 comments on commit 488568c

Please sign in to comment.