2014-01-15 18 views
4

我不明白如何創建圖表控件並將圖表放在現有的表單中。我在網上找到的所有示例均以新形式顯示圖表,但我想將圖表添加到我現有的一種表單中。如何在現有表單中顯示FSharp.Charting圖形?

我在想是這樣的:

let form = new Form(Text="My form") 
let lbl = new Label(Text="my label") 
let chart = Chart.Area ["a", 10; "b", 20] 

form.Controls.Add lbl 
form.Controls.Add chart 
// ---> The type 'ChartTypes.GenericChart' is not compatible with the type 'Control' 
Application.Run(form) 

謝謝!

+0

'form.Controls.Add myChart'? – ildjarn

+0

我的意思是控制代替原始代碼中的容器。仍然不起作用。我想有一個像ChartControl的地方 – vidi

回答

15

爲了達到這個目的,你應該把你的圖表換成FSharp.Charting.ChartTypes.ChartControl並且照顧正確的對接。你也不應該混合來自FSharp的Chart。與ChartSystem.Windows.Forms.DataVisualization.Charting一起收起。

一個好的起始點可能是以下全功能樣本,它與當前的FSharp.Charting v0.90.5一起工作;還需要參考System.DrawingSystem.Windows.Forms

open System 
open FSharp.Charting 
open FSharp.Charting.ChartTypes 
open System.Drawing 
open System.Windows.Forms 

[<STAThread; EntryPoint>] 
let main args = 
    let myChart = [for x in 0.0 .. 0.1 .. 6.0 -> sin x + cos (2.0 * x)] 
        |> Chart.Line |> Chart.WithYAxis(Title="Test") 
    let myChartControl = new ChartControl(myChart, Dock=DockStyle.Fill) 
    let lbl = new Label(Text="my label") 
    let form = new Form(Visible = true, TopMost = true, Width = 700, Height = 500) 
    form.Controls.Add lbl 
    form.Controls.Add(myChartControl) 
    do Application.Run(form) |> ignore 
    0 
+0

非常感謝!命名空間FSharp.Charting.ChartTypes中的ChartControl根本不直觀。我找不到任何例子。 – vidi

+1

不在WinForms應用程序中過多使用'FSharp.Charting'的原因可能是庫的首要用例是腳本;那麼'WinForms'是相當過時的技術,更高級的'WPF'具有AFAIK開箱即用的例子。 –

相關問題