Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot a data series in F#?

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?

like image 536
James Hugard Avatar asked Jul 18 '10 16:07

James Hugard


2 Answers

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()
like image 59
James Hugard Avatar answered Sep 28 '22 15:09

James Hugard


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.

like image 39
Darknight Avatar answered Sep 28 '22 15:09

Darknight