#r "nuget: Plotly.NET, 2.0.0-preview.7"
#r "nuget: Plotly.NET.Interactive, 2.0.0-preview.7"
open Plotly.NET
#r "nuget: FSharp.Data, 4.2.2"
open FSharp.Data
type remoteData = JsonProvider<"https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json">
let data = remoteData.Load("https://raw.githubusercontent.com/plotly/plotly.js/master/test/image/mocks/sankey_energy.json")
let sankey =
let vals = Seq.head data.Data
let nodes = Seq.zip vals.Node.Label vals.Node.Color |>
Seq.map(fun (label, color) -> Node.Create(label,
color = color,
lineColor = "black",
lineWidth = 0.5)) |>
Seq.toArray
let linksSrcTgt = Seq.zip vals.Link.Source vals.Link.Target
let linksValColorLabel = Seq.zip3 vals.Link.Value vals.Link.Color vals.Link.Label
let links = Seq.zip linksSrcTgt linksValColorLabel |>
Seq.map(fun ((source, target), (value, color, label)) -> Link.Create(nodes.[source],
nodes.[target],
value = float value,
label = label,
color = color)
)
let sankey = Chart.Sankey(nodes = nodes,
links = links,
nodePadding = 15.,
nodeThickness = 15.,
nodeLineColor = "black",
nodeLineWidth = 0.5)
|> GenericChart.mapTrace(fun x -> x.SetValue("valueformat", ".0f")
x.SetValue("valuesuffix", ".TWh")
x)
sankey
let trace =
let vals = Seq.head data.Data
let tmp = Trace("sankey")
tmp?valueFormat <- ".0f"
tmp?valuesuffix <- "TWh"
tmp?node <- {|pad = 15.; thickness = 15.; line = {| color = "black"; width = 0.5 |}; label = vals.Node.Label; color = vals.Node.Color |}
tmp?link <- {| source = vals.Link.Source; target = vals.Link.Target; value = vals.Link.Value; color = vals.Link.Color; label = vals.Link.Label |}
tmp
[
GenericChart.ofTraceObject(trace)
|> Chart.withLayout(Layout.init(Width = 1000.,
Font = Font.init(Size = 10.),
Title = Title.init(Text = "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>")))
sankey
|> Chart.withLayout(Layout.init(Width = 1000.,
Font = Font.init(Size = 10.),
Title = Title.init(Text = "Energy forecast for 2050<br>Source: Department of Energy & Climate Change, Tom Counsell via <a href='https://bost.ocks.org/mike/sankey/'>Mike Bostock</a>")))
]
Below there are 2 implementations of the example https://plotly.com/python/sankey-diagram/#more-complex-sankey-diagram-with-colored-links, one with the member Chart.Sankey, and the other with Trace("sankey") and dynamic assignments. Despite the same data is used, the Chart.Sankey implementation only rendered the 6 first nodes.