动态图表 - 寓言

时间:2017-12-28 03:47:18

标签: f# f#-data fsharpchart fable-f#

我有一个使用fable-elmish的模型更新视图架构项目。我必须每分钟下载文件并阅读这些文件。如何在更新功能中下载以及如何阅读和解析Json?

我也需要使用Fable创建动态图表。有人知道怎么做?

我的部分代码在这里:

let update (msg : Msg) (model : Model) =
  match msg with
  | GetData -> 
    model, 
    Cmd.ofPromise 
      (fun () -> 
        promise {
          let wc = new WebClient()
          wc.DownloadData("https://www.quandl.com/api/v1/datasets/LBMA/SILVER.json", "SILVER.json")
          wc.DownloadData("https://www.quandl.com/api/v1/datasets/LBMA/GOLD.json", "GOLD.json")
          // Read 2 files
          // Return 2 Json.Object
        })
      ()
      (fun silver gold -> GotData silver gold)
      (fun e -> GotError e.Message)
  | GotData silver gold -> 
    (Model.SilverData silver, Model.GoldData gold), // I think this doesn't work
    Cmd.ofPromise 
      (fun () -> Promise.sleep 60000)
      ()
      (fun () -> GetData)
      (fun e -> GetData)

1 个答案:

答案 0 :(得分:1)

如果您有一个定期事件,该事件应在Elmish应用程序中引起某些操作,我将使用subscription。以下代码段显示了一个函数,该函数设置一个间隔,该间隔导致每10分钟发送一次命令。

let timer initial =
    let sub dispatch =
        window.setInterval(fun _ -> dispatch LoadDataSet; console.log("Timer triggered")
            , 1000 * 60 * 10) |> ignore

    Cmd.ofSub sub

您将使用Program.withSubscription函数将订阅添加到主调度循环中。

我将使用Fable PowerPack包来为其fetchpromise提供支持,以获取数据集。以下代码将从指定的端点获取文档,将它们解析为DataSet类型的值,并在promise成功路径上将它们作为SilverAndGold模型类型的值返回。

type DataSet =
    { column_names : string list
      data : (string * float * float * float) list }

type SilverAndGold =
    { Silver : DataSet
      Gold : DataSet }

...

let fetchDataSets () = promise {
    let! silverData = Fetch.fetchAs<DataSet> "https://www.quandl.com/api/v1/datasets/LBMA/SILVER.json" []
    let! goldData = Fetch.fetchAs<DataSet> "https://www.quandl.com/api/v1/datasets/LBMA/GOLD.json" []

    return { Silver = silverData; Gold = goldData }
}

在Elmish应用程序的更新功能中,您可以看到如何触发承诺执行。在我们的订阅分发的每条LoadDataSet消息中,我们都会创建一个promise命令,该命令将导致DataSetLoaded消息中包含数据集或错误。

let update (msg:Msg) (model:Model) =
    match msg with
    | LoadDataSet ->
        model, Cmd.ofPromise fetchDataSets () DataSetLoaded Error
    | DataSetLoaded silverGold ->
        // here you could process you silver and gold datasets
        console.log silverGold
        Some silverGold, Cmd.none
    | Error e -> model, Cmd.none

我们可以对Fable bindings库使用Recharts来绘制数据集。以下代码显示了我们如何变换和修剪数据集(在浏览器中渲染所有数据点会很麻烦)并在view函数中将它们显示为折线图。

type ChartDataPoint =
    { Date : string
      Usd : float
      Gbp : float
      Euro : float }

let toChartData (dataSet : DataSet) =
    dataSet.data
    |> List.map (fun (dt, usd, gbp, eur) ->
        { Date = dt; Usd = usd; Gbp = gbp; Euro = eur } )
    |> Array.ofList
    |> Array.take 1000
    |> Array.rev

let priceChart (chartData : ChartDataPoint[]) =
    lineChart
        [ Chart.Data chartData
          Chart.Width 600.
          Chart.Height 500. ] [
        xaxis [ Cartesian.DataKey "Date" ] []
        yaxis [] []
        tooltip [] []
        legend [] []
        line [ Cartesian.Type "monotone"; Cartesian.DataKey "Gbp" ] []
        line [ Cartesian.Type "monotone"; Cartesian.DataKey "Euro" ] []
        line [ Cartesian.Type "monotone"; Cartesian.DataKey "Usd" ] []
    ]

let view (model : SilverAndGold option ) dispatch =
  div [ ] [
        match model with
        | Some sets ->
            yield h2 [] [ str "Silver" ]
            yield priceChart (toChartData sets.Silver)
            yield h2 [] [ str "Gold" ]
            yield priceChart (toChartData sets.Gold)
        | None ->
            yield h2 [] [ str "No data :("]
    ]

我制作了一个很小的Elmish应用程序,其中包含所有这些主题。您可以在here找到它,并根据需要对其进行调整。