FSharpChart.SaveAs () saves a blank image if called before the chart is completed

When launched in F # Interactive, I expect the following code to create a simple pie chart and save it to disk:

let pie = FSharpChart.Pie([("Apples",1);("Oranges",2);("Bananas",3)]) FSharpChart.SaveAs "test.png" ChartImageFormat.Png pie 

However, what is actually stored in "test.png" is an empty image. The same thing happens if I connect the chart to the FShartChart.SaveAs function. But if I first execute only the chart creation code and give the timeline for rendering before manual SaveAs, the image will be saved as expected.

Is there a way to block the FSharpChart.Pie call until rendering is complete? I am using FSharpChart.fsx version 0.60 in Visual Studio 2013.

+6
source share
2 answers

The problem is that you must first display the underlying chart control before it can save the chart to a file (this is pretty dumb, but unfortunately the F # chart is just a lightweight shell from the .NET base libraries).

I think you can either run two lines separately in F # interactive, or you need to explicitly call some method that displays the chart (I believe there is FSharpChart.Show or something like that)

I tested this using F # Charting , which is a newer version of the library (with some API changes, but very similar ideas), and the following works (even when executing all in one command):

 #load @"packages\FSharp.Charting.0.87\FSharp.Charting.fsx" open FSharp.Charting let pie = Chart.Pie([("Apples",1);("Oranges",2);("Bananas",3)]) pie.ShowChart() pie.SaveChartAs("D:\\temp\\test.png", ChartTypes.ChartImageFormat.Png) 
+8
source

I found the following to work well without requiring a display. (FSharp.Charting, v 0.90.14, .net 4.5)

 open FSharp.Charting // etc let prices = getPriceData () let chart = Chart.Candlestick(prices) Chart.Save @"C:\Charts\prices.png" chart 

Update

 let renderChart ch = let frm = new Form(Visible = false, TopMost = true, Width = 700, Height = 500) let ctl = new ChartControl(ch, Dock = DockStyle.Fill) frm.Controls.Add(ctl) 

calling this on your chart will allow you to display the chart without pop-ups.

+1
source

Source: https://habr.com/ru/post/956917/


All Articles