Search code examples
f#mschart

MS Chart Control Range Bar


I am trying to somehow replicate the range bar chart here.

enter image description here

I've found this reference but I don't fully grasp the code.

What I have is a series of task (sometimes accomplished in different periods).

let d = [("task1", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("12/01/2014 10:30")); 
         ("task2", DateTime.Parse("15/01/2014 09:30"), DateTime.Parse("16/01/2014 10:30"));
         ("task3", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("16/01/2014 10:30"))]
let chart = d |> FSharp.Charting.Chart.RangeBar
chart.ShowChart()

I am struggling to understand the logic of the API.


I have also tried:

let chart = new Windows.Forms.DataVisualization.Charting.Chart(Dock = DockStyle.Fill)
let area = new ChartArea("Main")
chart.ChartAreas.Add(area)
let mainForm = new Form(Visible = true, TopMost = true, Width = 700, Height = 500)
mainForm.Controls.Add(chart)
let seriesColumns = new Series("NameOfTheSerie")
seriesColumns.ChartType <- SeriesChartType.RangeBar

type SupportToChart(serieVals: Series) = 
    member this.addPointXY(lbl, [<ParamArray>] yVals: Object[]) = 
        serieVals.Points.AddXY(lbl, yVals) |> ignore

let supporter = SupportToChart(seriesColumns)
supporter.addPointXY("AAA", DateTime.Parse("11/01/2014 08:30"), DateTime.Parse("12/01/2014 10:30") )

which results in

System.ArgumentOutOfRangeException: You can only set 1 Y values for this data point.

Has something changed in the API since then?


Solution

  • The trick is initializing the Series with

    let serie = new Series("Range", yValues)
    

    where yValues defines the max number of "Y-values".