views:

294

answers:

3

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?

+5  A: 

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()
James Hugard
The "Samples Environment for Microsoft Chart Controls" has a ton of extremely useful examples all wrapped up in a simple browseresque UI: http://code.msdn.microsoft.com/mschart
James Hugard
Dude, you're talking to yourself too much.
Massif
usually the first sign of madness..
Darknight
+2  A: 

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.

Darknight
However, if you're working with data, then exploring them interactively is one of the strengths of F# (and you cannot do that with C#). And for interactive exploration, charts are essential...
Tomas Petricek
I think you have misunderstood me, I agree for interactivity I too would work withing F#. However for production code, I would use F# as a back end, and leverage the "Front-End" using WPF/other languages in its 'native' place.
Darknight
@Darknight - Thanks for the tip. In fact, that's exactly what I've been doing: parsing, filtering, transforming, and analyzing data in F#, then using C# for chart presentation. The Visual Studio UI Designer support made creating the controlling app very easy, and helped when fine-tuning chart appearance, etc.
James Hugard
What is the advantage of using anything but F# in this context?
Jon Harrop
because it would open up the possibility to use all sorts of controls in a easier manner. Yes it could in theory be all done in F#, but the mash up of functional style along with a load of boilerplate overhead for other controls looks ugly to me. Separating the data processing and presentation layer is cleaner and much more maintainable. I'd probably use simple plotting controls for 'quick' interactive verification. But for production I'd then leverage WPF separately.
Darknight
A: 

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.

Jon Harrop