How do I plot a data series in F#?

2019-03-19 18:18发布

Over on FSHUB, LethalLavaLand said,

Let me plot my values!

So the question is, how can I plot a data series in F# using built-in .NET 4.0 controls?

3条回答
淡お忘
2楼-- · 2019-03-19 18:31

Don't forget, you don't have to do everything in F#.

You can roll up all your F# calculations into a library or class, and then use that in whatever "Front End" language you want:

E.g you could easily marry F# back end with WPF or Silverlight or C# WinForms.

查看更多
不美不萌又怎样
3楼-- · 2019-03-19 18:41

Since I've been working with the built-in Microsoft Charting Controls in .NET 4.0 lately (and loving every minute of it!), I thought I'd take a crack at answering my own question...

#r "System.Windows.Forms.DataVisualization"

open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting

type LineChartForm( title, xs : float seq ) =
    inherit Form( Text=title )

    let chart = new Chart(Dock=DockStyle.Fill)
    let area = new ChartArea(Name="Area1")
    let series = new Series()
    do series.ChartType <- SeriesChartType.Line
    do xs |> Seq.iter (series.Points.Add >> ignore)
    do series.ChartArea <- "Area1"
    do chart.Series.Add( series )
    do chart.ChartAreas.Add(area)
    do base.Controls.Add( chart )

let main() =
    let data = seq { for i in 1..1000 do yield sin(float i / 100.0) }
    let f = new LineChartForm( "Sine", data )
    f.Show()

main()
查看更多
冷血范
4楼-- · 2019-03-19 18:41

You may appreciate my parametric plot in F# that visualizes predator-prey dynamics using the new charting functionality in .NET 4. The original 5-line Mathematica solution becomes 19 lines of F# code. Not bad for a general-purpose language!

However, this is a one-shot solution and, consequently, is not very useful in F# interactive. To reuse the same functionality interactively you need to handle threading issues (marshalling data to and from a UI thread that handles a persistent GUI) as our F# for Visualization product does. The technique is also described in my Visual F# 2010 for Technical Computing book.

查看更多
登录 后发表回答