Skip to content

Commit 7e2e2bd

Browse files
committed
Add more styling options to optional Description text for charts
1 parent 1d2891d commit 7e2e2bd

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,5 @@ docsrc/content/release-notes.md
184184
.fake
185185
docsrc/tools/FSharp.Formatting.svclog
186186
docs
187-
temp/gh-pages
187+
temp/gh-pages
188+
/src/FSharp.Plotly/TestScript.fsx

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open System
44
open System.IO
55

66
open GenericChart
7-
7+
open ChartDescription
88

99
/// Extensions methods for Charts supporting the fluent pipeline style 'Chart.WithXYZ(...)'.
1010
[<AutoOpen>]
@@ -381,28 +381,36 @@ module ChartExtensions =
381381
if verbose then
382382
System.Diagnostics.Process.Start(file) |> ignore
383383

384+
/// Saves chart in a specified file name. The caller is responsible for full path / filename / extension.
385+
static member SaveHtmlWithDescriptionAs (pathName : string) (description : Description) (ch:GenericChart,?Verbose) =
386+
let html = GenericChart.toEmbeddedHtmlWithDescription description ch
387+
File.WriteAllText(pathName, html)
388+
let verbose = defaultArg Verbose false
389+
if verbose then
390+
System.Diagnostics.Process.Start(pathName) |> ignore
384391

385392
/// Show chart in browser
386-
static member ShowWithDescription (show : bool) (d : string) (ch:GenericChart) =
393+
static member ShowWithDescription (description : Description) (ch:GenericChart) =
387394
let guid = Guid.NewGuid().ToString()
388-
let html = GenericChart.toEmbeddedHtmlWithDescription d ch
395+
let html = GenericChart.toEmbeddedHtmlWithDescription description ch
389396
let tempPath = Path.GetTempPath()
390397
let file = sprintf "%s.html" guid
391398
let path = Path.Combine(tempPath, file)
392399
File.WriteAllText(path, html)
393-
if show then System.Diagnostics.Process.Start(path) |> ignore
400+
System.Diagnostics.Process.Start(path) |> ignore
394401

395402

396-
/// Saves chart in a specified file name and shows it in the browser. The caller is responsible for full path / filename / extension.
397-
static member ShowFileWithDescription (show : bool) (fullFileName : string) (d : string) (ch:GenericChart) =
398-
let html = GenericChart.toEmbeddedHtmlWithDescription d ch
399-
File.WriteAllText(fullFileName, html)
400-
if show then System.Diagnostics.Process.Start(fullFileName) |> ignore
401403

402404

403405
/// Show chart in browser
404-
static member Show (ch:GenericChart) = Chart.ShowWithDescription true "" ch
405-
406+
static member Show (ch:GenericChart) =
407+
let guid = Guid.NewGuid().ToString()
408+
let html = GenericChart.toEmbeddedHTML ch
409+
let tempPath = Path.GetTempPath()
410+
let file = sprintf "%s.html" guid
411+
let path = Path.Combine(tempPath, file)
412+
File.WriteAllText(path, html)
413+
System.Diagnostics.Process.Start(path) |> ignore
406414

407415
/// Show chart in browser
408416
static member ShowAsImage (format:StyleParam.ImageFormat) (ch:GenericChart) =

src/FSharp.Plotly/FSharp.Plotly.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<Compile Include="GenericChart.fs" />
3737
<Compile Include="Chart.fs" />
3838
<Compile Include="ChartExtensions.fs" />
39+
<None Include="TestScript.fsx" />
3940
<None Include="paket.references" />
4041
<None Include="paket.template" />
4142
</ItemGroup>

src/FSharp.Plotly/GenericChart.fs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@ module HTML =
1414
<!-- Plotly.js -->
1515
<meta http-equiv="X-UA-Compatible" content="IE=11" >
1616
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
17+
<style>
18+
.container {
19+
padding-right: 25px;
20+
padding-left: 25px;
21+
margin-right: 0 auto;
22+
margin-left: 0 auto;
23+
}
24+
@media (min-width: 768px) {
25+
.container {
26+
width: 750px;
27+
}
28+
}
29+
@media (min-width: 992px) {
30+
.container {
31+
width: 970px;
32+
}
33+
}
34+
@media (min-width: 1200px) {
35+
.container {
36+
width: 1170px;
37+
}
38+
}
39+
</style>
1740
</head>
1841
<body>
1942
[CHART]
@@ -30,6 +53,12 @@ module HTML =
3053
Plotly.newPlot('[ID]', data, layout);
3154
</script>"""
3255

56+
let description ="""<div class=container>
57+
<h3>[DESCRIPTIONHEADING]</h3>
58+
<p>[DESCRIPTIONTEXT]</p>
59+
</div>"""
60+
61+
3362
let staticChart =
3463
"""<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;display: none;"><!-- Plotly chart will be drawn inside this DIV --></div>
3564
@@ -57,10 +86,27 @@ module HTML =
5786
});
5887
</script>"""
5988

89+
module ChartDescription =
90+
91+
92+
93+
type Description =
94+
{
95+
Heading : string
96+
Text : string
97+
}
98+
99+
let toDescriptionHtml (d:Description) =
100+
HTML.description
101+
.Replace("[DESCRIPTIONHEADING]",d.Heading)
102+
.Replace("[DESCRIPTIONTEXT]",d.Text)
103+
104+
60105
/// Module to represent a GenericChart
61106
module GenericChart =
62107

63108
open Trace
109+
open ChartDescription
64110

65111
type GenericChart =
66112
| Chart of Trace * Layout
@@ -189,18 +235,26 @@ module GenericChart =
189235
html
190236

191237

192-
let toEmbeddedHtmlWithDescription description gChart =
238+
let toEmbeddedHtmlWithDescription (description:Description) gChart =
193239
let chartMarkup =
194240
toChartHTML gChart
195241

242+
let descriptionMarkup =
243+
toDescriptionHtml description
244+
196245
HTML.doc
197246
.Replace("[CHART]", chartMarkup)
198-
.Replace("[DESCRIPTION]", description)
247+
.Replace("[DESCRIPTION]", descriptionMarkup)
199248

200249

201250
/// Converts a GenericChart to it HTML representation and embeds it into a html page.
202-
let toEmbeddedHTML gChart = toEmbeddedHtmlWithDescription "" gChart
251+
let toEmbeddedHTML gChart =
252+
let chartMarkup =
253+
toChartHTML gChart
203254

255+
HTML.doc
256+
.Replace("[CHART]", chartMarkup)
257+
.Replace("[DESCRIPTION]", "")
204258

205259
/// Converts a GenericChart to its Image representation
206260
let toChartImage (format:StyleParam.ImageFormat) gChart =

0 commit comments

Comments
 (0)