Is it possible to use or extend the FSharpChart library to create a line chart of a sliding window?

怎甘沉沦 提交于 2020-02-06 02:29:27

问题


I have been trying to figure out how to create a chart in F#, using the FSharpCharting library, which shows a sliding window of data. For example, I would like to be able to show all values for the past 3 minutes. Values older than that would fall off the chart, and be replaced as new values were observed.

Based on my experiments, I don't think that the underlying Microsoft Chart Controls support this scenario. Does anyone know if it is possible to create this type of chart? Are there other alternatives that have this capability?

Here is the 'experiment' that I tried. The task to update the values in the series produces an exception

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

#r "System.Windows.Forms.DataVisualization.dll"

open System.Drawing
open System.Collections.ObjectModel
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting

/// Add data series of the specified chart type to a chart
let addSeries typ (chart:Chart) =
    let series = new Series(ChartType = typ)
    chart.Series.Add(series)
    series

/// Create form with chart and add the first chart series
let createChart typ =
    let chart = new Chart(Dock = DockStyle.Fill, 
                          Palette = ChartColorPalette.Pastel)
    let mainForm = new Form(Visible = true, Width = 700, Height = 500)
    let area = new ChartArea()
    area.AxisX.MajorGrid.LineColor <- Color.LightGray
    area.AxisY.MajorGrid.LineColor <- Color.LightGray
    mainForm.Controls.Add(chart)
    chart.ChartAreas.Add(area)
    chart, addSeries typ chart

let numbers = 
    seq { while true do for i in 0.0 .. 0.1 .. 45.0 do yield i }
    |> Seq.map sin

let dataWindow = new ObservableCollection<double>()

numbers
|> Seq.take 100
|> Seq.iter dataWindow.Add

let chart, series = createChart SeriesChartType.Line
series.BorderWidth <- 3
series.Points.DataBindY(dataWindow)

async{
    for number in numbers do
        series.Points.RemoveAt(0)
        series.Points.Add(number) |> ignore
}
|> Async.Start

回答1:


I was able to make something like this work by avoiding the DataBindY call, and explicitly adding and removing the points.

#r "System.Windows.Forms.DataVisualization.dll"

open System
open System.Drawing
open System.Threading
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting

/// Add data series of the specified chart type to a chart
let createSeries typ (chart:Chart) =
    let series = new Series(ChartType = typ)
    chart.Series.Add(series)    
    series

/// Create form with chart and add the first chart series
let createChart() =
    let chart = new Chart(Dock = DockStyle.Fill, 
                          Palette = ChartColorPalette.Pastel)
    let mainForm = new Form(Visible = true, Width = 700, Height = 500)
    let area = new ChartArea()
    area.AxisX.MajorGrid.LineColor <- Color.LightGray
    area.AxisY.MajorGrid.LineColor <- Color.LightGray
    mainForm.Controls.Add(chart)
    chart.ChartAreas.Add(area)
    chart

let numbers = 
    seq { while true do for i in 0.0 .. 0.1 .. Double.MaxValue do yield i }
    |> Seq.map sin

let chart = createChart()
let series = createSeries SeriesChartType.FastLine chart

let firstWindow = numbers |> Seq.take 100

for number in firstWindow do
    series.Points.AddY(number) |> ignore

let context = SynchronizationContext.Current
let numbersEnumerator = numbers.GetEnumerator()
async{
    do! Async.SwitchToContext context
    while(numbersEnumerator.MoveNext() && not chart.IsDisposed) do        
        let number = numbersEnumerator.Current
        series.Points.SuspendUpdates()
        series.Points.RemoveAt(0)
        series.Points.AddY( number ) |> ignore
        series.Points.ResumeUpdates()
        do! Async.SwitchToThreadPool()
        do! Async.Sleep(100)
        do! Async.SwitchToContext context
}
|> Async.Start


来源:https://stackoverflow.com/questions/26472339/is-it-possible-to-use-or-extend-the-fsharpchart-library-to-create-a-line-chart-o

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