问题
I have the following piece of code:
let mychart = frame.GetAllSeries() |> Seq.iter(fun key value -> Chart.Line(value, Name=key) |> Chart.Combine
where frame.GetAllSeries()
returns a seq<KeyValuePair>
. I'd like to pipe the sequence directly in a chart.
I thought I could iterate through the sequence. The problem is that I can't find an idomatic way to access the key and value separately that could be plugged in directly in the lambda expression.
Thanks.
EDIT
This works:
let chart = frame.GetAllSeries() |> Seq.map( fun (KeyValue(k,v)) -> Chart.Line(v |> Series.observations, Name=k)) |> Chart.Combine
.. could it get simpler? I have a large dataset and I am afraid the performance gets impacted by so many transformations.
回答1:
As you already figured out, using Series.observations
gives you a sequence of key-value pairs from the series that you can then pass to Chart.Line
etc.
This is definitely something that should not be needed and you can make the code simpler using extension method that lets you automatically plot a series:
[<AutoOpen>]
module FsLabExtensions =
type FSharp.Charting.Chart with
static member Line(data:Series<'K, 'V>, ?Name, ?Title, ?Labels, ?Color, ?XTitle, ?YTitle) =
Chart.Line(Series.observations data, ?Name=Name, ?Title=Title, ?Labels=Labels, ?Color=Color, ?XTitle=XTitle, ?YTitle=YTitle)
If you include this, you can plot series directly:
let s = series [ for x in 0.0 .. 0.1 .. 1.0 -> x, sin x ]
Chart.Line(s)
You can also reference Deedle & F# Charting through our experimental package "FsLab" that includes these overloads (see here)
来源:https://stackoverflow.com/questions/20093815/plotting-deedle-frame