Skip to content

Commit e956065

Browse files
committed
Change toChartHTML for jupyter notebook interoperability. (FeatureRequest #36)
1 parent 483df1c commit e956065

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

Diff for: docsrc/content/violin-plots.fsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ violin1 |> Chart.Show
3232
By swapping x and y plus using `StyleParam.Orientation.Horizontal` we can flip the chart horizontaly.
3333
*)
3434
let violin2 =
35-
Chart.Violin (y,x,Jitter=0.1,Points=StyleParam.Jitterpoints.All,Orientation=StyleParam.Orientation.Horizontal)
35+
Chart.Violin (y,x,Jitter=0.1,Points=StyleParam.Jitterpoints.All,Orientation=StyleParam.Orientation.Horizontal,Meanline=Meanline.init(Visible=true))
3636

3737
(***do-not-eval***)
3838
violin2 |> Chart.Show

Diff for: src/FSharp.Plotly/GenericChart.fs

+36-7
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,43 @@ module HTML =
4545
</html>"""
4646

4747

48+
// let chart =
49+
// """<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;"><!-- Plotly chart will be drawn inside this DIV --></div>
50+
//<script>
51+
// var data = [DATA];
52+
// var layout = [LAYOUT];
53+
// var config = [CONFIG];
54+
// Plotly.newPlot('[ID]', data, layout, config);
55+
//</script>"""
4856
let chart =
49-
"""<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;"><!-- Plotly chart will be drawn inside this DIV --></div>
50-
<script>
51-
var data = [DATA];
52-
var layout = [LAYOUT];
53-
var config = [CONFIG];
54-
Plotly.newPlot('[ID]', data, layout, config);
55-
</script>"""
57+
let newScript = new System.Text.StringBuilder()
58+
newScript.AppendLine("""<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;"><!-- Plotly chart will be drawn inside this DIV --></div>""") |> ignore
59+
newScript.AppendLine("<script type=\"text/javascript\">") |> ignore
60+
newScript.AppendLine(@"
61+
var renderPlotly = function() {
62+
var fsharpPlotlyRequire = requirejs.config({context:'fsharp-plotly',paths:{plotly:'https://cdn.plot.ly/plotly-latest.min'}});
63+
fsharpPlotlyRequire(['plotly'], function(Plotly) {") |> ignore
64+
newScript.AppendLine(@"
65+
var data = [DATA];
66+
var layout = [LAYOUT];
67+
var config = [CONFIG];
68+
Plotly.newPlot('[ID]', data, layout, config);") |> ignore
69+
newScript.AppendLine("""});
70+
};
71+
if ((typeof(requirejs) !== typeof(Function)) || (typeof(requirejs.config) !== typeof(Function))) {
72+
var script = document.createElement("script");
73+
script.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js");
74+
script.onload = function(){
75+
renderPlotly();
76+
};
77+
document.getElementsByTagName("head")[0].appendChild(script);
78+
}
79+
else {
80+
renderPlotly();
81+
}""") |> ignore
82+
newScript.AppendLine("</script>") |> ignore
83+
newScript.ToString()
84+
5685

5786
let description ="""<div class=container>
5887
<h3>[DESCRIPTIONHEADING]</h3>

Diff for: src/FSharp.Plotly/Trace.fs

+6-6
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ module Trace =
468468
Fillcolor |> DynObj.setValueOpt boxPlot "fillcolor"
469469
Marker |> DynObj.setValueOpt boxPlot "marker"
470470
Line |> DynObj.setValueOpt boxPlot "line"
471-
Alignmentgroup |> DynObj.setValueOpt boxPlot "Alignmentgroup"
472-
Offsetgroup |> DynObj.setValueOpt boxPlot "Offsetgroup"
471+
Alignmentgroup |> DynObj.setValueOpt boxPlot "alignmentgroup"
472+
Offsetgroup |> DynObj.setValueOpt boxPlot "offsetgroup"
473473
Notched |> DynObj.setValueOpt boxPlot "notched"
474474
NotchWidth |> DynObj.setValueOpt boxPlot "notchwidth"
475475
QuartileMethod |> DynObj.setValueOptBy boxPlot "quartilemethod" StyleParam.QuartileMethod.convert
@@ -500,7 +500,7 @@ module Trace =
500500

501501
?Box:Box,
502502
?Bandwidth,
503-
?Meanline,
503+
?Meanline:Meanline,
504504
?Scalegroup,
505505
?Scalemode,
506506
?Side,
@@ -535,10 +535,10 @@ module Trace =
535535
Width |> DynObj.setValueOpt boxPlot "width"
536536
Marker |> DynObj.setValueOpt boxPlot "marker"
537537
Line |> DynObj.setValueOpt boxPlot "line"
538-
Alignmentgroup |> DynObj.setValueOpt boxPlot "Alignmentgroup"
539-
Offsetgroup |> DynObj.setValueOpt boxPlot "Offsetgroup"
538+
Alignmentgroup |> DynObj.setValueOpt boxPlot "alignmentgroup"
539+
Offsetgroup |> DynObj.setValueOpt boxPlot "offsetgroup"
540540

541-
Box |> DynObj.setValueOpt boxPlot "Box"
541+
Box |> DynObj.setValueOpt boxPlot "box"
542542
Bandwidth |> DynObj.setValueOpt boxPlot "bandwidth"
543543
Meanline |> DynObj.setValueOpt boxPlot "meanline"
544544
Scalegroup |> DynObj.setValueOpt boxPlot "scalegroup"

0 commit comments

Comments
 (0)