我正在尝试扩展James Hugard's帖子How do I plot a data series in F#?,并且在使用可变数量的函数参数时遇到了一个小问题。如果我在此示例中特别指出了Hugard的LineChartForm的函数,则代码在加载到F#Interactive Interpreter时按预期工作:
(* This code is based off of a Stack Overflow post by
James Hugard @
https://stackoverflow.com/questions/3276357/how-do-i-plot-a-data-series-in-f
*)
module HuggardPlot
#r "System.Windows.Forms.DataVisualization"
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting
type LineChartForm( title, minX, maxX, func1, func2) =
inherit Form( Text=title )
let chart = new Chart(Dock=DockStyle.Fill)
let area = new ChartArea(Name="Area1")
(* Add the first plot Hugard style *)
let series = new Series()
do series.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series.Points.AddXY(i, func1(i)) |> ignore
do series.ChartArea <- "Area1"
do chart.Series.Add( series )
(* Add the second plot Hugard style *)
let series2 = new Series()
do series2.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series2.Points.AddXY(i, func2(i)) |> ignore
do series2.ChartArea <- "Area1"
do chart.Series.Add( series2 )
(* Add area1 to the plot *)
do chart.ChartAreas.Add(area)
do base.Controls.Add( chart )
(* Convenience method to make it easier to plot functions *)
let plotTwoFunctions minX maxX f1 f2 =
let LCF = new LineChartForm("lines", minX, maxX, f1, f2);
LCF.Show();
另一方面,如果我尝试使用http://msdn.microsoft.com/en-us/library/dd233213.aspx的参数数组部分中的技术通过ParamArray传递可变数量的函数,并使用以下代码:
(* This code is based off of a Stack Overflow post by
James Hugard @
https://stackoverflow.com/questions/3276357/how-do-i-plot-a-data-series-in-f
*)
module HuggardPlot
#r "System.Windows.Forms.DataVisualization"
open System
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting
type LineChartForm(
title,
minX,
maxX,
[<ParamArray>] funcs: Object[]) =
inherit Form( Text=title )
let chart = new Chart(Dock=DockStyle.Fill)
let area = new ChartArea(Name="Area1")
do for func in funcs do
(* Add the first plot Hugard style *)
let series = new Series()
do series.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series.Points.AddXY(i, func(i)) |> ignore
do series.ChartArea <- "Area1"
do chart.Series.Add( series )
(* Add area1 to the plot *)
do chart.ChartAreas.Add(area)
do base.Controls.Add( chart )
(* Convenience method to make it easier to plot functions *)
let plotTwoFunctions minX maxX f1 f2 =
let LCF = new LineChartForm("lines", minX, maxX, f1, f2);
LCF.Show();
我在第27行发出错误series.Points.AddXY(i, func(i)) |> ignore
发出“此值不是一个函数,无法应用”在“func”下有一个红色波浪形。
我怀疑这与我使用[<ParamArray>] funcs: Object[]
作为Hugard先生的LineChartForm类型的参数定义这一事实有关。
我应该将[<ParamArray>] funcs: Object[]
更改为第二个代码示例的第27行将“func”识别为函数?
答案 0 :(得分:2)
如果要创建一个采用params
函数数组的方法,则需要将参数定义为[<ParamArray>] funcs: (float -> float)[]
。在原始版本中,类型为Object[]
,意味着数组的各个元素是对象 - 如果您将元素类型更改为函数,则F#将识别您可以调用它们。
但是,如果您只想绘制F#数据图表,那么WinForms DataVisualization库已经有了一个很好的包装器F# Chart and available on GitHub。还有一个全面的documentation available on MSDN(如果您使用的是最新版本的库,则FSharpChart
已重命名为Chart
)。
要创建比较指定范围内两个函数的绘图,您只需编写:
let plotTwoFunctions minX maxX f1 f2 =
Chart.Combine
[ Chart.Line [ for x in minX .. maxX -> x, f1 x ]
Chart.Line [ for x in minX .. maxX -> x, f2 x ] ]
这将创建两个单独的折线图(由两个函数生成的数据),然后使用Chart.Combine
将它们组合成一个图表。