如何在现有表单中显示FSharp.Charting图?

时间:2014-01-15 05:04:00

标签: winforms f# fsharpchart f#-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) 

谢谢!

1 个答案:

答案 0 :(得分:15)

为了实现这一目标,您应该将图表包装到FSharp.Charting.ChartTypes.ChartControl并注意正确对接。另外,您不应将FSharp.Charting中的ChartChart中的System.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