11

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?

Igor Kulman
  • 16,211
  • 10
  • 57
  • 118

1 Answers1

7

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)
Gene Belitski
  • 10,270
  • 1
  • 34
  • 54
  • 1
    FSharpChart.fsx is not part of the Nuget package but I found it on the authorw web. Does it mean that this package with is not for F# but fo C#? – Igor Kulman Aug 03 '12 at 16:04
  • 1
    It is for building F# WinForm/WPF applications with FSharpChart library. – Gene Belitski Aug 03 '12 at 16:09
  • I remember #loading the fsx file takes a long time, and provides the same as just referencing the nuget's dll – nicolas Aug 22 '12 at 18:06
  • @nicolas: `FSharpChart.dll` cannot visualize anything, it just yields in this case an instance of `ChartTypes.LineChart` subtype of `GenericChart`. Making the latter visible would require certain plumbing: firing a `Form`, making a `ChartControl` instance off `ChartTypes.LineChart`, etc. On the contrary, `FSharpChart.fsx` script provides all required chart visualization plumbing via [`fsi.AddPrinter`](http://msdn.microsoft.com/en-us/library/ee842576) machinery. – Gene Belitski Aug 23 '12 at 04:23
  • you still need the fsi.AddPrinter wiring, but it is 23 lines, not x thousands. although it is interesting to study, it is dog slow to load. – nicolas Aug 23 '12 at 13:12