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
I was able to make something like this work by avoiding the DataBindY call, and explicitly adding and removing the points.