winforms - 如何以现有形式显示 FSharp.Charting 图?

标签 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) 

谢谢!

最佳答案

为了实现这一点,您应该将图表包装成 FSharp.Charting.ChartTypes.ChartControl并注意正确对接。另外你不应该混用Chart来自 FSharp.Charting with Chart来自 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

关于winforms - 如何以现有形式显示 FSharp.Charting 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21129486/

相关文章:

f# - 在自托管的F#脚本中设置ExitCode

f# - 基于无限序列的 Observable 动画图表

c# - 使用 PostgreSql 和 ADO.NET 在 C# WinForms 中锁定记录和表

c# - ListView 和文件输出的问题

c# - 一键访问两个线程功能

c# - Control.ClientRectangle 与 Control.DisplayRectangle

memory-management - 为什么在F#中进行Seq.take抛出System.OutOfMemoryException

F# 输出参数和值类型