Search code examples
f#fsifsharpchartf#-charting

How to use FSharpChart from fsx script file


I want to use FSharpChart with a fsx script file in my project. I downloaded and referenced the MSDN.FSharpChart.dll using Nuget and my code looks like this

#r @"..\packages\MSDN.FSharpChart.dll.0.60\lib\MSDN.FSharpChart.dll"
open System.Drawing
open MSDN.FSharp.Charting

[for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)]
    |> FSharpChart.Line

The path is correct because VS 2012 offers me intellisense and knows the MSDN.FSharp namespace. The problem is that when I run this script in FSI, nothing is shown.

What is wrong?


Solution

  • In order to make your chart show up from FSI you should preload FSharpChart.fsx into your FSI session like in a snippet below:

    #load @"<your path here>\FSharpChart.fsx"
    [for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)] 
    |> MSDN.FSharp.Charting.FSharpChart.Line;;
    

    UPDATE 08/23/2012: For comparison, visualizing the same chart off FSharpChart.dll would require some WinForms plumbing:

    #r @"<your path here>\MSDN.FSharpChart.dll"
    #r "System.Windows.Forms.DataVisualization.dll"
    open System.Windows.Forms
    open MSDN.FSharp.Charting
    
    let myChart = [for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)]
                  |> FSharpChart.Line
    
    let form = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
    let ctl = new ChartControl(myChart, Dock = DockStyle.Fill)
    form.Controls.Add(ctl)