• <bdo id='WLT3S'></bdo><ul id='WLT3S'></ul>

    <small id='WLT3S'></small><noframes id='WLT3S'>

      <legend id='WLT3S'><style id='WLT3S'><dir id='WLT3S'><q id='WLT3S'></q></dir></style></legend>

    1. <i id='WLT3S'><tr id='WLT3S'><dt id='WLT3S'><q id='WLT3S'><span id='WLT3S'><b id='WLT3S'><form id='WLT3S'><ins id='WLT3S'></ins><ul id='WLT3S'></ul><sub id='WLT3S'></sub></form><legend id='WLT3S'></legend><bdo id='WLT3S'><pre id='WLT3S'><center id='WLT3S'></center></pre></bdo></b><th id='WLT3S'></th></span></q></dt></tr></i><div id='WLT3S'><tfoot id='WLT3S'></tfoot><dl id='WLT3S'><fieldset id='WLT3S'></fieldset></dl></div>
      1. <tfoot id='WLT3S'></tfoot>

        可以将动态 html 表用作源数据吗?

        Can plotly use a dynamic html table as source data?(可以将动态 html 表用作源数据吗?)

            <tbody id='MfLzD'></tbody>
          <tfoot id='MfLzD'></tfoot>

              • <small id='MfLzD'></small><noframes id='MfLzD'>

              • <legend id='MfLzD'><style id='MfLzD'><dir id='MfLzD'><q id='MfLzD'></q></dir></style></legend>
                <i id='MfLzD'><tr id='MfLzD'><dt id='MfLzD'><q id='MfLzD'><span id='MfLzD'><b id='MfLzD'><form id='MfLzD'><ins id='MfLzD'></ins><ul id='MfLzD'></ul><sub id='MfLzD'></sub></form><legend id='MfLzD'></legend><bdo id='MfLzD'><pre id='MfLzD'><center id='MfLzD'></center></pre></bdo></b><th id='MfLzD'></th></span></q></dt></tr></i><div id='MfLzD'><tfoot id='MfLzD'></tfoot><dl id='MfLzD'><fieldset id='MfLzD'></fieldset></dl></div>
                  <bdo id='MfLzD'></bdo><ul id='MfLzD'></ul>
                  本文介绍了可以将动态 html 表用作源数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个 html 表,其中包含根据文件中的过滤器计算的值,我可以根据这些值读取并生成绘图吗?

                  If I have an html table that contains values that are calculated based on filters within my file, can I get plotly to read and produce a plot based on those values?

                  我不确定回答这个问题是否重要,但我主要使用 R,并使用 r 代码块从我使用 crosstalk 包创建的 sharedData 对象名称 shared_ert 计算总和为 R.

                  I'm not sure that it matters to answering this question, but I use R primarily, and use the r code chunks calculate sums from a sharedData object names shared_ert that I created with the crosstalk package for R.

                  <table id="example" class="cell-border" style="width:100%">
                  <tr>
                  <th>Enrolled</th>
                  <th>Not Enrolled</th>
                  </tr>
                  <tr>
                  <td>
                  ```{r, echo=FALSE, collapse=TRUE, warning=FALSE, message=FALSE}
                  summarywidget::summarywidget(shared_ert,statistic = 'sum',column = 'Enrolled')
                  ```
                  </td>
                  <td>
                  
                  ```{r, echo=FALSE, collapse=TRUE, warning=FALSE, message=FALSE}
                  summarywidget::summarywidget(shared_ert,statistic = 'sum',column = 'Not Enrolled')
                  
                  ```
                  
                  </td>
                  </tr>
                  </table>
                  

                  请注意,摘要小部件最终会在每个 td 中生成一个 span 标签.跨度看起来像

                  所以表格最终看起来像:

                  So the table ends up looking like:

                  <table id="example" class="cell-border" style="width:100%">
                  <tr>
                  <th>Enrolled</th>
                  <th>Not Enrolled</th>
                  </tr>
                  <tr>
                  <td>
                  <span id ="waffle" class="summarywidget html-widget html-widge-static-bound">1293</span>
                  <script type="application/json" data-for="waffle">
                  ### a bunch of data appears here, from which the 1293 value is derived
                  </script>
                  
                  </td>
                  <td>
                  <span id ="iron" class="summarywidget html-widget html-widge-static-bound">948</span>
                  <script type="application/json" data-for="iron">
                  ### a bunch of data appears here, from which the 948 value is derived
                  </script>
                  
                  
                  </td>
                  </tr>
                  </table>
                  

                  根据我对 javascript 世界的有限理解,我需要我的数据看起来像

                  From my limited understanding of the world of javascript, I need my data to look something like

                  var data = [
                  {
                  x: ['giraffes', 'orangutans', 'monkeys'],
                  y: [20, 14, 23],
                  type: 'bar'
                    }
                  ];
                  

                  这样我就可以得到一个类似这样的情节:

                  So that I can get a plot produced with something like:

                  Plotly.newPlot('myDiv', data);
                  

                  (直接来自 https://plotly.com/javascript/bar-charts/)

                  如果我正确理解了问题,我需要阅读 html 表 example 并创建一个保存该表的 var.经过大量搜索 SO 和一般网络后,我的猜测是这里的解决方案:HTML Table to JSON 将表格拉入正确的格式.我在努力

                  If I understand the problem correctly, I need to read the html table example and create a var that holds the table. After much searching around SO and the web in general, my guess is that the solution here: HTML Table to JSON pulls the table into the correct format. I'm trying

                  ```{js}
                  function tableToJson(table) {
                  var data = [];
                  
                  // first row needs to be headers
                  var headers = [];
                  for (var i=0; i<table.rows[0].cells.length; i++) {
                      headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
                  }
                  
                  // go through cells
                  for (var i=1; i<table.rows.length; i++) {
                  
                      var tableRow = table.rows[i];
                      var rowData = {};
                  
                      for (var j=0; j<tableRow.cells.length; j++) {
                  
                          rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
                  
                      }
                  
                      data.push(rowData);
                  }       
                  
                  return data;
                  }
                  
                  var tabdata = $document.getElementById('#example').tableToJSON();            
                  
                  ```
                  

                  我想从这里开始,我需要在当前状态下从表中读取数据,所以我使用按钮和 onclick 生成绘图,如下所示:

                  I think from here, I need plotly to read the data from the table in it's current state, so I produce the plot using a button and onclick, as follows:

                  <button type="button" onclick="Plotly.newPlot('myDiv',tabdata);">Make Plot</button>
                  

                  点击后,会出现绘图,但在任何地方都没有数据点.

                  Upon clicking, the plotly plot appears, but doesn't have a data point anywhere.

                  我的方法论可能偏离了轨道,所以我顺从最初的问题:我可以根据动态 html 表读取并生成绘图吗?

                  I might be way off track in my methodology, so I defer to the original question: can I get plotly to read and produce a plot based on a dynamic html table?

                  非常感谢任何帮助建立为此目的的方法.

                  Any help establishing a means to this end would be very much appreciated.

                  推荐答案

                  您需要生成带有键 x & 的 jsony .所以,这里 x 值将是您的标题,即: th 标签和 y 值将是 td值.现在,如果您的表中只有一行,您可以简单地创建 JSON 对象,然后使用键将值推入其中:data[x"]、data[y";]..等等.

                  You need generate your json with keys x & y .So , here x value will be your header i.e : th tags and y values will be tdvalues . Now , if you have only one row in your table you can simply create JSON Object and then push value inside this using key i.e : data["x"] , data["y"]..etc .

                  演示代码:

                  function tableToJSON(table) {
                    var data = {}; //create obj
                    data["x"] = [] //for x & y
                    data["y"] = []
                    data["type"] = "bar"
                    for (var i = 0; i < table.rows[0].cells.length; i++) {
                      data["x"].push(table.rows[0].cells[i].innerHTML.toLowerCase().trim()); //push x values
                    }
                    for (var i = 1; i < table.rows.length; i++) {
                      var tableRow = table.rows[i];
                      for (var j = 0; j < tableRow.cells.length; j++) {
                        data["y"].push(parseInt(tableRow.cells[j].querySelector(".summarywidget").textContent.trim()));
                        //push y values
                        console.log(tableRow.cells[j].querySelector(".summarywidget").textContent.trim())
                      }
                    }
                  
                    return data;
                  }
                  
                  function draw() {
                    var tabdata = tableToJSON(document.getElementById('example'));
                    tester = document.getElementById('tester');
                    Plotly.newPlot(tester, [tabdata])
                  }

                  <script src="https://cdn.plot.ly/plotly-2.1.0.min.js"></script>
                  <table id="example" class="cell-border" style="width:100%">
                    <tr>
                      <th>Enrolled</th>
                      <th>Not Enrolled</th>
                    </tr>
                    <tr>
                      <td>
                        <span id="waffle" class="summarywidget html-widget html-widge-static-bound">1293</span>
                        <script type="application/json" data-for="waffle">
                          ###
                          a bunch of data appears here, from which the 1293 value is derived
                        </script>
                  
                      </td>
                      <td>
                        <span id="iron" class="summarywidget html-widget html-widge-static-bound">948</span>
                        <script type="application/json" data-for="iron">
                          ###
                          a bunch of data appears here, from which the 948 value is derived
                        </script>
                  
                  
                      </td>
                    </tr>
                  </table>
                  
                  <button type="button" onclick="draw()">Make Plot</button>
                  <div id="tester" style="width:600px;height:250px;"></div>

                  现在,如果您的表中有多行,则需要生成该值的 JSON 数组.为此,您需要保留 main_array 然后将值推送到其中每次迭代的 main_array.

                  Now , if you have mutliple rows in your table you need to generate JSON Array of that values .For that you need to keep main_array and then push values inside this main_array on each iterations.

                  演示代码:

                  function tableToJSON(table) {
                    var main_array = [] //for main array
                    var for_x = [] //for x values
                    for (var i = 0; i < table.rows[0].cells.length; i++) {
                      for_x.push(table.rows[0].cells[i].innerHTML.toLowerCase().trim()); //push value 
                    }
                    for (var i = 1; i < table.rows.length; i++) {
                      var tableRow = table.rows[i];
                      var data = {}; //create obj..
                      data["y"] = [] //for y values
                      for (var j = 0; j < tableRow.cells.length; j++) {
                        data["y"].push(parseInt(tableRow.cells[j].innerHTML.trim())); //push y values
                      }
                      //save other values..
                      data["x"] = for_x
                      data["type"] = "bar"
                      data["name"] = "Rows" + i
                      main_array.push(data) //push values in main array
                    }
                    //console..[{},{}..]
                    return main_array;
                  
                  }
                  
                  function draw() {
                    var tabdata = tableToJSON(document.getElementById('example'));
                    tester = document.getElementById('tester');
                    //pass it here
                    Plotly.newPlot(tester, tabdata, {
                      barmode: 'stack'
                    })
                  }

                  <script src="https://cdn.plot.ly/plotly-2.1.0.min.js"></script>
                  <table id="example" class="cell-border" style="width:100%">
                    <tr>
                      <th>Enrolled</th>
                      <th>Not Enrolled</th>
                    </tr>
                    <tr>
                      <td>
                        123
                      </td>
                      <td>
                        125
                      </td>
                    </tr>
                    <tr>
                      <td>
                        121
                      </td>
                      <td>
                        127
                      </td>
                    </tr>
                  </table>
                  
                  <button type="button" onclick="draw()">Make Plot</button>
                  <div id="tester" style="width:600px;height:250px;"></div>

                  这篇关于可以将动态 html 表用作源数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                    <tfoot id='QsXFd'></tfoot>
                    <i id='QsXFd'><tr id='QsXFd'><dt id='QsXFd'><q id='QsXFd'><span id='QsXFd'><b id='QsXFd'><form id='QsXFd'><ins id='QsXFd'></ins><ul id='QsXFd'></ul><sub id='QsXFd'></sub></form><legend id='QsXFd'></legend><bdo id='QsXFd'><pre id='QsXFd'><center id='QsXFd'></center></pre></bdo></b><th id='QsXFd'></th></span></q></dt></tr></i><div id='QsXFd'><tfoot id='QsXFd'></tfoot><dl id='QsXFd'><fieldset id='QsXFd'></fieldset></dl></div>

                      <tbody id='QsXFd'></tbody>

                          • <bdo id='QsXFd'></bdo><ul id='QsXFd'></ul>

                            <small id='QsXFd'></small><noframes id='QsXFd'>

                          • <legend id='QsXFd'><style id='QsXFd'><dir id='QsXFd'><q id='QsXFd'></q></dir></style></legend>