How to display an FSharp.Charting graph in an existing form?

白昼怎懂夜的黑 提交于 2020-01-21 03:40:27

问题


I don't understand how to create a chart control and place the chart in an existing form. All the examples I found on the web show the chart in a new form but I would like to add the chart to one of my existing forms.

I'm thinking of something like this:

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) 

Thanks!


回答1:


In order to achieve this you should wrap your chart into FSharp.Charting.ChartTypes.ChartControl and take care of correct docking. Also you should not mix Chart from FSharp.Charting with Chart from System.Windows.Forms.DataVisualization.Charting.

A good staring point may be the following fully functional sample that works with the current FSharp.Charting v0.90.5; also references are required to System.Drawing and System.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


来源:https://stackoverflow.com/questions/21129486/how-to-display-an-fsharp-charting-graph-in-an-existing-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!