1

I have seq<Nullable<int>> and need to create nice plot, but without null values. Here is my code:

open System
#r """..\packages\FSharp.Charting.0.90.14\lib\net40\FSharp.Charting.dll"""
#load """..\packages\FSharp.Charting.0.90.14\FSharp.Charting.fsx"""
open FSharp.Charting

//in a real world replaced by .csv with empty values
let seqWithNullInt = seq[Nullable 10 ; Nullable 20  ; Nullable (); Nullable 40; Nullable 50] 
//let seqWithNullInt = seq[ 10 ; 20  ;  30;  40; 50]  //works fine

let bothSeq = seqWithNullInt |> Seq.zip {1..5}

Chart.Line bothSeq // Error because of nullable int 

And here is my vision:

Expected chart

How to skip null values? I don't want to replace them by nearest or something, I need to skip them from chart.. Is there any solution?

Alamakanambra
  • 5,845
  • 3
  • 36
  • 43

1 Answers1

2

Something like this might work (note that I've used Option values instead of nullables, since that's more idiomatic in F#):

let neitherPairHasNoneInValue (pair1, pair2) =
    pair1 |> snd |> Option.isSome && pair2 |> snd |> Option.isSome
let seqWithNone = Seq.ofList [Some 10; Some 20; None; Some 40; Some 50]
let pairsWithoutNone = seqWithNone
                       |> Seq.zip {1..5}
                       |> Seq.pairwise
                       |> Seq.filter neitherPairHasNoneInValue
printfn "%A" pairsWithoutNone

This will output [(1,10),(2,20) ; (4,40),(5,50)]. I don't know the FSharp.Charting API offhand so I can't tell you which function will take that list of X,Y pairs and draw the graph you want, but it should be relatively straightforward to get from there to your graph.

rmunn
  • 34,942
  • 10
  • 74
  • 105