是否可以使用或扩展FSharpChart库来创建滑动窗口的线形图? [英] Is it possible to use or extend the FSharpChart library to create a line chart of a sliding window?

查看:225
本文介绍了是否可以使用或扩展FSharpChart库来创建滑动窗口的线形图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出如何在F#中创建一个图表,使用FSharpCharting库,它显示一个滑动窗口的数据。例如,我想能够显示过去3分钟的所有值。值

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.

根据我的实验,我不认为底层的Microsoft图表控件支持这种情况。有谁知道是否可以创建这种类型的图表?还有其他替代品有这种能力吗?

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:枚举操作可能无法执行。

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


推荐答案

通过避免DataBindY调用,并显式地添加和删除点来工作。

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

这篇关于是否可以使用或扩展FSharpChart库来创建滑动窗口的线形图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆