Skip to content

Commit d183457

Browse files
committed
plotlyjs v2.10.0: Add support to use version 3 of MathJax and add typesetMath attribute to config (plotly/plotly.js#6073)
1 parent 99fcf65 commit d183457

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

src/Plotly.NET/CSharpLayer/GenericChartExtensions.fs

+5-2
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,14 @@ module GenericChartExtensions =
958958
/// Adds the necessary script tags to render tex strings to the chart's DisplayOptions
959959
[<CompiledName("WithMathTex")>]
960960
[<Extension>]
961-
member this.WithMathTex([<Optional; DefaultParameterValue(true)>] ?AppendTags: bool) =
961+
member this.WithMathTex(
962+
[<Optional; DefaultParameterValue(true)>] ?AppendTags: bool,
963+
[<Optional; DefaultParameterValue(3)>] ?MathJaxVersion: int
964+
) =
962965
let append =
963966
Option.defaultValue true AppendTags
964967

965-
this |> Chart.withMathTex (append)
968+
this |> Chart.withMathTex (AppendTags = append)
966969

967970

968971
/// Save chart as html single page

src/Plotly.NET/ChartAPI/Chart.fs

+21-6
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,7 @@ type Chart =
27692769
static member withConfigStyle
27702770
(
27712771
[<Optional; DefaultParameterValue(null)>] ?StaticPlot: bool,
2772+
[<Optional; DefaultParameterValue(null)>] ?TypesetMath: bool,
27722773
[<Optional; DefaultParameterValue(null)>] ?PlotlyServerUrl: string,
27732774
[<Optional; DefaultParameterValue(null)>] ?Autosizable: bool,
27742775
[<Optional; DefaultParameterValue(null)>] ?Editable: bool,
@@ -2784,6 +2785,7 @@ type Chart =
27842785
let config =
27852786
Config.init (
27862787
?StaticPlot = StaticPlot,
2788+
?TypesetMath = TypesetMath,
27872789
?PlotlyServerUrl = PlotlyServerUrl,
27882790
?Autosizable = Autosizable,
27892791
?Responsive = Responsive,
@@ -3171,17 +3173,30 @@ type Chart =
31713173

31723174
/// Adds the necessary script tags to render tex strings to the chart's DisplayOptions
31733175
[<CompiledName("WithMathTex")>]
3174-
static member withMathTex([<Optional; DefaultParameterValue(true)>] ?AppendTags: bool) =
3176+
static member withMathTex(
3177+
[<Optional; DefaultParameterValue(true)>] ?AppendTags: bool,
3178+
[<Optional; DefaultParameterValue(3)>] ?MathJaxVersion: int
3179+
) =
3180+
let version = MathJaxVersion |> Option.defaultValue 3
3181+
31753182
let tags =
3176-
[
3177-
"""<script type="text/x-mathjax-config;executed=true">MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true}});</script>"""
3178-
"""<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>"""
3179-
]
3183+
if version = 2 then
3184+
[
3185+
"""<script type="text/x-mathjax-config;executed=true">MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']], processEscapes: true}});</script>"""
3186+
"""<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>"""
3187+
]
3188+
else
3189+
[
3190+
"""<script>MathJax = {tex: {inlineMath: [['$', '$'], ['\\(', '\\)']]}};</script>"""
3191+
"""<script src="https://cdn.jsdelivr.net/npm/[email protected]/es5/tex-svg.js"></script>"""
3192+
]
31803193

31813194
(fun (ch: GenericChart) ->
31823195

31833196
if (AppendTags |> Option.defaultValue true) then
3184-
ch |> Chart.withAdditionalHeadTags tags
3197+
ch
3198+
|> Chart.withAdditionalHeadTags tags
3199+
|> Chart.withConfigStyle()
31853200
else
31863201
ch |> Chart.withHeadTags tags)
31873202

src/Plotly.NET/Config/Config.fs

+4
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ type Config() =
354354
static member init
355355
(
356356
[<Optional; DefaultParameterValue(null)>] ?StaticPlot: bool,
357+
[<Optional; DefaultParameterValue(null)>] ?TypesetMath: bool,
357358
[<Optional; DefaultParameterValue(null)>] ?PlotlyServerUrl: string,
358359
[<Optional; DefaultParameterValue(null)>] ?Autosizable: bool,
359360
[<Optional; DefaultParameterValue(null)>] ?Editable: bool,
@@ -367,6 +368,7 @@ type Config() =
367368
Config()
368369
|> Config.style (
369370
?StaticPlot = StaticPlot,
371+
?TypesetMath = TypesetMath,
370372
?PlotlyServerUrl = PlotlyServerUrl,
371373
?Autosizable = Autosizable,
372374
?Responsive = Responsive,
@@ -395,6 +397,7 @@ type Config() =
395397
static member style
396398
(
397399
[<Optional; DefaultParameterValue(null)>] ?StaticPlot: bool,
400+
[<Optional; DefaultParameterValue(null)>] ?TypesetMath: bool,
398401
[<Optional; DefaultParameterValue(null)>] ?PlotlyServerUrl: string,
399402
[<Optional; DefaultParameterValue(null)>] ?Autosizable: bool,
400403
[<Optional; DefaultParameterValue(null)>] ?Editable: bool,
@@ -407,6 +410,7 @@ type Config() =
407410
) =
408411
fun (config: Config) ->
409412
StaticPlot |> DynObj.setValueOpt config "staticPlot"
413+
TypesetMath |> DynObj.setValueOpt config "typesetMath"
410414
PlotlyServerUrl |> DynObj.setValueOpt config "plotlyServerURL"
411415
Autosizable |> DynObj.setValueOpt config "autosizable"
412416
Editable |> DynObj.setValueOpt config "editable"

tests/Plotly.NET.Tests/HtmlCodegen/ChartLayout.fs

+29-7
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ let additionalHeadTagsChart =
336336
// Add reference to the bulma css framework
337337
|> Chart.withAdditionalHeadTags ["""<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">"""]
338338

339-
let mathtexChart =
339+
let mathtexv3Chart =
340340
[
341341
Chart.Point([(1.,2.)],@"$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$", UseDefaults = false)
342342
Chart.Point([(2.,4.)],@"$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$", UseDefaults = false)
@@ -346,6 +346,16 @@ let mathtexChart =
346346
// include mathtex tags in <head>. pass true to append these scripts, false to ONLY include MathTeX.
347347
|> Chart.withMathTex(true)
348348

349+
let mathtexv2Chart =
350+
[
351+
Chart.Point([(1.,2.)],@"$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$", UseDefaults = false)
352+
Chart.Point([(2.,4.)],@"$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$", UseDefaults = false)
353+
]
354+
|> Chart.combine
355+
|> Chart.withTitle @"$\beta_{1c} = 25 \pm 11 \text{ km s}^{-1}$"
356+
// include mathtex tags in <head>. pass true to append these scripts, false to ONLY include MathTeX.
357+
|> Chart.withMathTex(true, MathJaxVersion = 2)
358+
349359
[<Tests>]
350360
let ``Display options`` =
351361
testList "ChartLayout.Display options" [
@@ -376,16 +386,28 @@ let ``Display options`` =
376386
]
377387
|> substringListIsInChart additionalHeadTagsChart toEmbeddedHTML
378388
);
379-
testCase "MathTex data" ( fun () ->
389+
testCase "MathTex v2 data" ( fun () ->
380390
"""var data = [{"type":"scatter","name":"$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$","mode":"markers","x":[1.0],"y":[2.0],"marker":{},"line":{}},{"type":"scatter","name":"$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$","mode":"markers","x":[2.0],"y":[4.0],"marker":{},"line":{}}];"""
381-
|> chartGeneratedContains mathtexChart
391+
|> chartGeneratedContains mathtexv2Chart
382392
);
383-
testCase "MathTex layout" ( fun () ->
393+
testCase "MathTex v2 layout" ( fun () ->
384394
"var layout = {\"title\":{\"text\":\"$\\\\beta_{1c} = 25 \\\\pm 11 \\\\text{ km s}^{-1}$\"}};"
385-
|> chartGeneratedContains mathtexChart
395+
|> chartGeneratedContains mathtexv2Chart
386396
);
387-
testCase "MathTex include mathjax" ( fun () ->
397+
testCase "MathTex v2 include mathjax" ( fun () ->
388398
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/"
389-
|> substringIsInChart mathtexChart toEmbeddedHTML
399+
|> substringIsInChart mathtexv2Chart toEmbeddedHTML
400+
)
401+
testCase "MathTex v3 data" ( fun () ->
402+
"""var data = [{"type":"scatter","name":"$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$","mode":"markers","x":[1.0],"y":[2.0],"marker":{},"line":{}},{"type":"scatter","name":"$\\beta_{1c} = 25 \\pm 11 \\text{ km s}^{-1}$","mode":"markers","x":[2.0],"y":[4.0],"marker":{},"line":{}}];"""
403+
|> chartGeneratedContains mathtexv3Chart
404+
);
405+
testCase "MathTex v3 layout" ( fun () ->
406+
"var layout = {\"title\":{\"text\":\"$\\\\beta_{1c} = 25 \\\\pm 11 \\\\text{ km s}^{-1}$\"}};"
407+
|> chartGeneratedContains mathtexv3Chart
408+
);
409+
testCase "MathTex v3 include mathjax" ( fun () ->
410+
"https://cdn.jsdelivr.net/npm/mathjax@3"
411+
|> substringIsInChart mathtexv3Chart toEmbeddedHTML
390412
)
391413
]

0 commit comments

Comments
 (0)