fslaborg / FSharp.Charting

Charting library suitable for interactive F# scripting
http://fslab.org/FSharp.Charting/
Other
216 stars 67 forks source link

SaveChartAs does not work without first calling ShowChart #26

Open palpha opened 11 years ago

palpha commented 11 years ago

The output is a 300px² blank image. I can make it work by implementing SaveChartAs as an extension like ShowChart, and having them work together, but what I've done is a WorksForMySpecificUseCase™ hack. Someone who knows the code better could probably come up with a more robust solution.

This is true for the various Copy methods as well, but they can't be moved easily.

palpha commented 11 years ago

Oh snap. I didn't realize ChartControl was public. Don't prioritize this issue, if it's even an issue.

stmax82 commented 10 years ago

I ran into this problem while trying to save (and display) charts of intermediate results while unit testing algorithms.

I created the following ugly workaround... maybe it can be of use to someone:

module Chart =
    open System.Windows.Forms
    open FSharp.Charting.ChartTypes

    let Show (c:GenericChart) =
        use cc = new ChartControl(c)
        cc.Dock <- DockStyle.Fill
        use f = new Form()
        f.Size <- System.Drawing.Size(800, 600)
        f.Controls.Add cc
        f.ShowDialog() |> ignore

    let Save filename (c:GenericChart) =
        use cc = new ChartControl(c)
        cc.Dock <- DockStyle.Fill
        use f = new Form()
        f.Size <- System.Drawing.Size(800, 600)
        f.Controls.Add cc
        f.Load |> Event.add (fun _ -> c.SaveChartAs(filename, ChartImageFormat.Png); f.Close()) // yay
        Application.Run f

... at least that makes the following two examples work as expected (i.e. saves/displays charts instead of white 300x300 images):

open FsUnit
open NUnit.Framework

open FSharp.Charting

[<Test>]
let testShowChart() =
    [for x in 0.0..0.1..10.0 -> x, sin x]
    |> Chart.Line
    |> Chart.Show

[<Test>]
let testSaveChart() =
    [for x in 0.0..0.1..10.0 -> x, sin x]
    |> Chart.Line
    |> Chart.Save @"c:\temp\chart.png"
wk-j commented 9 years ago

Thanks @stmax82
Still here 2015 I also ran into this problem. Your solution is ok at least I can save image, But I think it not make sense to run gui every time.

simra commented 9 years ago

@stmax82 is there a reason why you chose to use Application.Run in the Save method? This leads to an exception in fsi.exe since there is already a running event loop: I've tested using f.ShowDialog() and it seems to work fine in both fsi and in compiled .exes, but before I submit a PR I'm interested to hear whether there will be any gotchas. See also the discussion at Issue #38.