Search code examples
plotlyplotly.net

Using Plotly F# or C# is it possible to create a graph/chart from Json


I'm trying to create a chart in C# (using the plotly libraries) from the json exported from plotly's chart studio.

I can see options in the code for exporting json GenericChart.toFigureJson(barChart); , but nothing for from/importing/parsing.

Is this possible?


Solution

  • Is this related to this recent Discussion thread on Plotly.NET?

    I'll port my answer from there anyways, to make it more visible:

    The thing with our strongly typed layers is that they unfortunately only work well for JSON creation. plotly.js JSON has properties that have arbitrary types, which you can do in a dynamically typed language such as JavaScript or python, but is quite hard to do in .NET.

    We circumvent this on object creation by offering a strongly typed subset of possible types and by using DynamicObj, which lets us set members dynamically. However, this basically prevents deserialization - even if you could deserialize all nested objects into DynamicObj (which does not work), there is no way of knowing whether that DynamicObj represents a Trace, a LinearAxis, or any other strong type abstraction provided by Plotly.NET without looking at the name of the member and additional knowledge about the plotly JSON schema.

    However the actual question over there is more specific than here, so i'll add a bit detail:

    You can absolutely deserialize Plotly.NET objects from JSON, but a lot of work has to be done to determine the types to use, meaning you cannot just use Newtsonsoft.JSON and use the default Deserializer and call it a day. You'd have to determine which Plotly.NET type to used based on member names, and then construct these types mapping their properties to the optional parameters of each type. Quite a hard task to do for any kind of object, which is why we do not offer this functionality.

    However, if you want to deserialize a json string that is always looking kind of the same (e.g. it is always a single bar chart), then this manual mapping might be feasible.