plotly: exposing the “config” object in the stored JSON string, for easier use with Plotly.JS functions

大城市里の小女人 提交于 2021-02-11 12:33:33

问题


I don't know whether this is a plotly question or an htmlwidgets question. Maybe it's both.

I am using R to make plotly widgets, which are -- when made from R -- also htmlwidgets. When making plotly widgets in this way, the resulting HTML contains a stringified JSON object that includes the data and some settings for the widget. But only some parts of this JSON object are easily accessed from a Javascript console. For example, the "data" and "layout" objects are easily accessed, but the "config" object is not. Why is that -- and is it possible to easily access the "config" object in some other way?

Here is an example, starting with R code that creates a Plotly widget:

  library(plotly)
  data(iris)
  myPlot <- plot_ly(
      x     = ~Petal.Length, y = ~Petal.Width, 
      color = ~Species,
      data  = iris)
  myPlot$elementId <- "myExample"  
  myPlot

This code produces an HTML page that displays the plotly widget. The page contains a <script type="application/json" data-for="myExample"> container, which in turn contains a stringified JSON object. Given the code above, the JSON object has this structure:

{
VALID (RFC 8259)
Formatted JSON Data
{
  "x":{
    "attrs":{},
    "layout":{},
    "config":{
      "showSendToCloud":false
    },
    "data":[],
    "highlight":{},
  },
  "evals":[],
  "jsHooks":[]
}

(For brevity, I've omitted a few objects in x. I'm also hiding the contents of most objects; for example, attrs and layout aren't really empty.)

From a Javascript console (e.g., in Chrome), it is then easy to access the "data" array and the "layout" object:

el = document.getElementById("myExample");
typeof el.data;    # "object"
el.data.length;    # 3
typeof el.layout;  # "object"
typeof el.config;  # "undefined"

I can get access to the data array just by typing el.data, and I can get access to the layout object in the same way. Why can't I get access to the config object just by typing el.config? And is there a way to make "config" as easily accessed as "data" and "layout"?

I ask because some Plotly.js functions, like Plotly.react(), take a "config" object. Functions like that would be easier to use if there were easy access to the "config" object.

I know that the "config" object can be accessed in other ways, e.g., with JSON.parse(). But the approach used above -- just call el.data or el.layout -- is much easier, which is why I would like to use it for the config data, too.


回答1:


If you really want to access the "config" object, you may need to do so the hard way, i.e., with JSON.parse(). But if you instead want only to update the config object, and then to update your Plotly figure, there are at least two alternatives:

  1. Pass an unnamed object to Plotly.react(). Example: Plotly.react(el.data, el.layout, {"showSendToCloud" : true} ).

  2. Use Plotly.setPlotConfig() followed by Plotly.react(). Example:

    Plotly.setPlotConfig( { "showLink" : true } );
    Plotly.react(el, el.data, el.layout, {} );

(Curiously, Plotly.setPlotConfig() doesn't seem to have its own entry in the Plotly documentation, though it is mentioned in passing in other parts of the documentation.)



来源:https://stackoverflow.com/questions/61740985/plotly-exposing-the-config-object-in-the-stored-json-string-for-easier-use-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!