FSharpChart.SaveAs()保存空白图像(如果在图表渲染完成之前调用) [英] FSharpChart.SaveAs () saves blank image if called before chart rendering is complete
问题描述
在F#Interactive中运行时,我期望以下代码创建一个简单的饼图并将其保存到磁盘:
When run 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
但是,实际上保存在"test.png"中的是空白图像.如果将图表通过管道传递到FShartChart.SaveAs函数,也会发生同样的情况.但是,如果我只执行图表创建代码,并在手动执行SaveAs之前给图表时间渲染时间,则图像将按预期方式保存.
However, what actually gets saved in "test.png" is a blank image. The same happens if I pipe the chart into the FShartChart.SaveAs function. But if I first execute only the chart creation code and give the chart time to render before manually executing SaveAs, the image gets saved as expected.
有没有一种方法可以阻止对FSharpChart.Pie的调用,直到渲染完成为止?我在Visual Studio 2013中使用的是FSharpChart.fsx版本0.60.
Is there a way to block the call to FSharpChart.Pie until the rendering is complete? I'm using FSharpChart.fsx version 0.60 in Visual Studio 2013.
推荐答案
问题是底层图表控件首先需要显示,然后才能将图表保存到文件中(这很愚蠢,但可悲的是,F#图表只是基础.NET图表库的轻量级包装.
The problem is that the underlying chart control first needs to be displayed before it can save the chart to a file (this is pretty silly, but sadly, F# chart is just a lightweight wrapper over underlying .NET chart libraries).
我认为您可以在F#交互式中分别运行两行,或者您需要显式调用某种显示图表的方法(我相信有FSharpChart.Show
或类似的东西)
I think you can either run the 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)
我使用 F#图表(该库的较新版本)进行了测试(该API进行了一些更改,但想法非常相似)和以下工作原理(即使在单个命令中全部执行):
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 executed all in a single 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)
这篇关于FSharpChart.SaveAs()保存空白图像(如果在图表渲染完成之前调用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!