Luke Xu
Luke Xu

Reputation: 2440

My F# Chart Library doesn't work in a project

open FSharp.Charting
open System

[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    let x = Chart.Line [ for x in -100 .. 100 -> x, pown x 3]
    let y = Console.ReadKey();
    0 //return an integer exit code

Hello, I'm trying to play with the F# in PROJECT mode NOT a script. So far I have the code above as well as it told me to add a reference to System.Drawing which I also did.

I have 0 errors or warnings when I compile and it runs perfectly fine. The console screen appears but nothing else happens. The graph just doesn't show up or anything.

I HAVE tried this in the interactive window and it works perfectly fine there. Any ideas?

Upvotes: 9

Views: 627

Answers (2)

yuyoyuppe
yuyoyuppe

Reputation: 1602

As @s952163 mentioned, you could wrap it to Winforms. However, ShowChart() already returns System.Windows.Form value. So, as long as you're pumping events with Application.Run(f), this code works fine too:

[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    let f = (Chart.Line [ for x in -100 .. 100 -> x, pown x 3]).ShowChart()
    System.Windows.Forms.Application.Run(f)
    0

Upvotes: 9

s952163
s952163

Reputation: 6324

You should just wrap it in Winforms. Please see: How to display an FSharp.Charting graph in an existing form?

A very barebones example:

open FSharp.Charting
open System
open System.Windows.Forms

[<STAThread>]
[<EntryPoint>]
let main argv = 
    printfn "%A" argv
    let chart = Chart.Line [ for x in -100 .. 100 -> x, pown x 3]
    let f1 = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
    f1.Controls.Add(new ChartTypes.ChartControl(chart, Dock = DockStyle.Fill))
    Application.Run(f1)
   0 // return an integer exit code

Please reference System.Drawing, System.Windows.Forms, System.Windows.Forms.DataVisualization

Upvotes: 4

Related Questions