Skip to content

Commit 03a8847

Browse files
committed
Fix
2 parents 3acf8a0 + 5b3847b commit 03a8847

File tree

12 files changed

+365
-757
lines changed

12 files changed

+365
-757
lines changed

FSharp.Plotly.sln

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
33
VisualStudioVersion = 15.0.26430.16
4+
VisualStudioVersion = 15.0.27130.2036
45
MinimumVisualStudioVersion = 10.0.40219.1
56
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".paket", ".paket", "{63297B98-5CED-492C-A5B7-A5B4F73CF142}"
67
ProjectSection(SolutionItems) = preProject
@@ -64,6 +65,11 @@ EndProject
6465
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ED8079DD-2B06-4030-9F0F-DC548F98E1C4}"
6566
EndProject
6667
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FSharp.Plotly.Tests", "tests\FSharp.Plotly.Tests\FSharp.Plotly.Tests.fsproj", "{12CFF99C-5660-42FE-9370-9085A6558F16}"
68+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Plotly.Tests", "tests\FSharp.Plotly.Tests\FSharp.Plotly.Tests.fsproj", "{B7F06453-847A-4894-9705-7BFB793E5AFC}"
69+
EndProject
70+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Plotly", "src\FSharp.Plotly\FSharp.Plotly.fsproj", "{AF92DAC1-46E7-4C6A-B784-772A284DD3BD}"
71+
EndProject
72+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Plotly.WPF", "src\FSharp.Plotly.WPF\FSharp.Plotly.WPF.fsproj", "{2869F1FF-BA08-420C-AB41-CFF4835DD954}"
6773
EndProject
6874
Global
6975
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -79,6 +85,18 @@ Global
7985
{12CFF99C-5660-42FE-9370-9085A6558F16}.Debug|Any CPU.Build.0 = Debug|Any CPU
8086
{12CFF99C-5660-42FE-9370-9085A6558F16}.Release|Any CPU.ActiveCfg = Release|Any CPU
8187
{12CFF99C-5660-42FE-9370-9085A6558F16}.Release|Any CPU.Build.0 = Release|Any CPU
88+
{B7F06453-847A-4894-9705-7BFB793E5AFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
89+
{B7F06453-847A-4894-9705-7BFB793E5AFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
90+
{B7F06453-847A-4894-9705-7BFB793E5AFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
91+
{B7F06453-847A-4894-9705-7BFB793E5AFC}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{AF92DAC1-46E7-4C6A-B784-772A284DD3BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{AF92DAC1-46E7-4C6A-B784-772A284DD3BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{AF92DAC1-46E7-4C6A-B784-772A284DD3BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{AF92DAC1-46E7-4C6A-B784-772A284DD3BD}.Release|Any CPU.Build.0 = Release|Any CPU
96+
{2869F1FF-BA08-420C-AB41-CFF4835DD954}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
97+
{2869F1FF-BA08-420C-AB41-CFF4835DD954}.Debug|Any CPU.Build.0 = Debug|Any CPU
98+
{2869F1FF-BA08-420C-AB41-CFF4835DD954}.Release|Any CPU.ActiveCfg = Release|Any CPU
99+
{2869F1FF-BA08-420C-AB41-CFF4835DD954}.Release|Any CPU.Build.0 = Release|Any CPU
82100
EndGlobalSection
83101
GlobalSection(SolutionProperties) = preSolution
84102
HideSolutionNode = FALSE

src/FSharp.Plotly/ChartExtensions.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,13 @@ module ChartExtensions =
392392
File.WriteAllText(path, html)
393393
System.Diagnostics.Process.Start(path) |> ignore
394394

395+
/// Show chart in browser
396+
static member ShowAsImage (format:StyleParam.ImageFormat) (ch:GenericChart) =
397+
let guid = Guid.NewGuid().ToString()
398+
let html = GenericChart.toEmbeddedImage format ch
399+
let tempPath = Path.GetTempPath()
400+
let file = sprintf "%s.html" guid
401+
let path = Path.Combine(tempPath, file)
402+
File.WriteAllText(path, html)
403+
System.Diagnostics.Process.Start(path) |> ignore
395404

src/FSharp.Plotly/FSharp.Plotly.fsproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
99
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
1010
</PropertyGroup>
11+
<Project Sdk="Microsoft.NET.Sdk">
12+
13+
<PropertyGroup>
14+
<TargetFramework>netstandard2.0</TargetFramework>
15+
</PropertyGroup>
16+
1117
<ItemGroup>
1218
<Compile Include="StyleParams.fs" />
1319
<Compile Include="DynamicObj.fs" />
@@ -31,9 +37,18 @@
3137
<Compile Include="GenericChart.fs" />
3238
<Compile Include="Chart.fs" />
3339
<Compile Include="ChartExtensions.fs" />
40+
<None Include="Script.fsx" />
3441
</ItemGroup>
42+
43+
<ItemGroup>
44+
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
45+
</ItemGroup>
46+
3547
<ItemGroup>
3648
<Content Include="App.config" />
3749
</ItemGroup>
3850
<Import Project="..\..\.paket\Paket.Restore.targets" />
51+
<PackageReference Update="FSharp.Core" Version="4.3.4" />
52+
</ItemGroup>
53+
3954
</Project>

src/FSharp.Plotly/GenericChart.fs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,32 @@ module HTML =
2828
Plotly.newPlot('[ID]', data, layout);
2929
</script>"""
3030

31+
let staticChart =
32+
"""<div id="[ID]" style="width: [WIDTH]px; height: [HEIGHT]px;display: none;"><!-- Plotly chart will be drawn inside this DIV --></div>
33+
34+
<img id="jpg-export"></img>
35+
36+
<script>
37+
var d3 = Plotly.d3;
38+
var img_jpg= d3.select('#jpg-export');
39+
var data = [DATA];
40+
var layout = [LAYOUT];
41+
Plotly.newPlot('[ID]', data, layout)
42+
// static image in jpg format
43+
44+
.then(
45+
function(gd)
46+
{
47+
Plotly.toImage(gd,{format:'[IMAGEFORMAT]',height: [HEIGHT],width: [WIDTH]})
48+
.then(
49+
function(url)
50+
{
51+
img_jpg.attr("src", url);
52+
53+
}
54+
)
55+
});
56+
</script>"""
3157

3258
/// Module to represent a GenericChart
3359
module GenericChart =
@@ -117,8 +143,10 @@ module GenericChart =
117143
|> JsonConvert.SerializeObject
118144

119145
let html =
120-
HTML.chart
121-
.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
146+
HTML.staticChart
147+
//.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
148+
.Replace("[WIDTH]", string 600 )
149+
.Replace("[HEIGHT]", string 600)
122150
.Replace("[ID]", guid)
123151
.Replace("[DATA]", tracesJson)
124152
.Replace("[LAYOUT]", layoutJson)
@@ -152,6 +180,35 @@ module GenericChart =
152180
html
153181

154182

183+
/// Converts a GenericChart to its Image representation
184+
let toChartImage (format:StyleParam.ImageFormat) gChart =
185+
let guid = Guid.NewGuid().ToString()
186+
let tracesJson =
187+
getTraces gChart
188+
|> JsonConvert.SerializeObject
189+
let layoutJson =
190+
getLayout gChart
191+
|> JsonConvert.SerializeObject
192+
193+
let html =
194+
HTML.staticChart
195+
//.Replace("style=\"width: [WIDTH]px; height: [HEIGHT]px;\"","style=\"width: 600px; height: 600px;\"")
196+
.Replace("[WIDTH]", string 600 )
197+
.Replace("[HEIGHT]", string 600)
198+
.Replace("[ID]", guid)
199+
.Replace("[DATA]", tracesJson)
200+
.Replace("[LAYOUT]", layoutJson)
201+
.Replace("[IMAGEFORMAT]",format.ToString().ToLower())
202+
html
203+
204+
/// Converts a GenericChart to an image and embeds it into a html page
205+
let toEmbeddedImage (format:StyleParam.ImageFormat) gChart =
206+
let html =
207+
let chartMarkup =
208+
toChartImage format gChart
209+
HTML.doc.Replace("[CHART]", chartMarkup)
210+
html
211+
155212

156213
/// Creates a new GenericChart whose traces are the results of applying the given function to each of the trace of the GenericChart.
157214
let mapTrace f gChart =

0 commit comments

Comments
 (0)