将Plotly.NET中的图表序列化为JSON字符串

huangapple go评论64阅读模式
英文:

Serialize Chart into JSON string in Plotly.NET

问题

我想将一个在F#中创建的Plotly图表序列化为JSON字符串,以便通过HTTP请求传递。

理论上,这应该很简单,因为Plotly.NET文档中指出,"Plotly.NET是用于创建plotly图表的.NET封装器。这意味着在幕后,所有功能都创建可以由plotly呈现的JSON对象"。

从我在Python中使用Plotly的经验来看,可以这样做:

json.dumps(obj=Figure(), cls=utils.PlotlyJSONEncoder)

是否有F#的等效方法?

英文:

I want to serialize a Plotly chart in F# into JSON string so it can be passed as a payload through a HTTP request.

In theory, this should be trivial as the Plotly.NET documentation states, "Plotly.NET is a .NET wrapper for creation of plotly charts. This means that, under the hood, all functionality creates JSON objects that can be rendered by plotly".

From having used Plotly in Python, the the way to do this is:

json.dumps(obj=Figure(), cls=utils.PlotlyJSONEncoder)

Is there an equivalent method for F#?

答案1

得分: 0

在一般情况下,请注意 Plotly.NET 中的 JSON 创建是单向的,您不能轻松地将 JSON 对象反序列化为 Plotly.NET 类型。

然而,由于您只要求进行序列化,一种方法是将 GenericChart(它是一个 Union,因此不能直接序列化)转换为 Figure,然后对其进行序列化(请注意,我使用 UseDefaults=false 来减小此示例中的 JSON 大小,否则它将包含大量的模板内容):

{ "data": [{"type":"scatter","mode":"markers","x":[1],"y":[2],"marker":{},"line":{}}], "layout":{}, "frames":[] }

但这不包括配置对象。如果您想要包含配置对象,目前没有内置的简单方法,因为内部将相应的图表分解为 datalayoutconfig 对象,然后进行序列化并注入到 HTML 中。

因此,如果您需要序列化配置对象,可以这样做:

{ "data": [{"type":"scatter","mode":"markers","x":[1],"y":[2],"marker":{},"line":{}}], "layout":{}, "config":{} }

我同意这比应该更加困难,因此我提出了 #399 来跟踪此问题。理想情况下,应该有 GenericChart.toFigureJsonGenericChart.toJson,并且内部的序列化设置应该是公开的,而不是私有的。

英文:

In general, note that json creation in Plotly.NET is a one-way road, you can not deserialize the json objects into Plotly.NET types easily.

However, since you only asked for serialization only, one way is converting the GenericChart (which is a Union, therefore you cannot serialize it directly) to a Figure, and then serializing it (note that i use UseDefaults=false to reduce json size for this example, otherwise it would include large amounts of template stuff):

open Plotly.NET
open Newtonsoft.Json

let settings = JsonSerializerSettings(ReferenceLoopHandling = ReferenceLoopHandling.Serialize)

Chart.Point([1,2], UseDefaults = false) 
|> GenericChart.toFigure
|> fun c -> JsonConvert.SerializeObject(c, settings)

result:

{"data":[{"type":"scatter","mode":"markers","x":[1],"y":[2],"marker":{},"line":{}}],"layout":{},"frames":[]}

this does not include the config object though. if you want to include that, there is currently no easy built-in for that, as internally the respective charts are deconstructed into data, layout, and config objects and then serialized and injected into html.

So you could do this if you need the config object serialized:

type ChartDTO = {
    data: Trace list
    layout: Layout
    config: Config
}

Chart.Point([1,2], UseDefaults = false)
|> fun c ->
    {
        data = c |> GenericChart.getTraces
        layout = c |> GenericChart.getLayout
        config = c |> GenericChart.getConfig
    }

|> fun c -> JsonConvert.SerializeObject(c, settings)

result:

{"data":[{"type":"scatter","mode":"markers","x":[1],"y":[2],"marker":{},"line":{}}],"layout":{},"config":{}}

I agree that this is harder than it should be though, so I opened #399 to track this. Ideally, there should be GenericChart.toFigureJson and GenericChart.toJson, and the internal serializer settings should be exposed instead of being private.

huangapple
  • 本文由 发表于 2023年7月10日 22:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定