Search code examples
winformsf#f#-interactivefsifsharpchart

FSharpChart with Windows.Forms very slow for many points


I use code like the example below to do basic plotting of a list of values from F# Interactive. When plotting more points, the time taken to display increases dramatically. In the examples below, 10^4 points display in 4 seconds whereas 4.10^4 points take a patience-testing 53 seconds to display. Overall it's roughly as if the time to plot N points is in N^2.

The result is that I'll probably add an interpolation layer in front of this code, but

1) I wonder if someone who knows the workings of FSharpChart and Windows.Forms could explain what is causing this behaviour? (The data is bounded so one thing that seems to rule out is the display needing to adjust scale.)

2)Is there a simple remedy other than interpolating the data myself?

let plotl (f:float list) =
    let chart = FSharpChart.Line(f, Name = "")
            |> FSharpChart.WithSeries.Style(Color = System.Drawing.Color.Red, BorderWidth = 2)
    let form = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
    let ctl = new ChartControl(chart, Dock = DockStyle.Fill)
    form.Controls.Add(ctl)

let z1 = [for i in 1 .. 10000 do yield sin(float(i * i))]
let z2 = [for i in 1 .. 20000 do yield sin(float(i * i))]
plotl z1
plotl z2

Solution

  • First of all, FSharpChart is a name used in an older version of the library. The latest version is called F# Charting, comes with a new documentation and uses just Chart.

    To answer your question, Chart.Line and Chart.Points are quite slow for large number of points. The library also has Chart.FastLine and Chart.FastPoints (which do not support as many features, but are faster). So, try getting the latest version of F# Charting and using the "Fast" version of the method.