FSharp.Charting icon indicating copy to clipboard operation
FSharp.Charting copied to clipboard

SaveChartAs does not work without first calling ShowChart

Open palpha opened this issue 12 years ago • 4 comments

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 avatar Sep 10 '13 09:09 palpha

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

palpha avatar Sep 10 '13 12:09 palpha

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"

stmax82 avatar Jan 12 '14 13:01 stmax82

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.

wk-j avatar May 14 '15 05:05 wk-j

@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.

simra avatar Jul 19 '15 06:07 simra