简体   繁体   English

使用chart.js中数据表中的数组创建饼图

[英]Create a pie chart using an array from a data table in chart.js

I have a table with values, and I want to use the data within the table to dynamically create a pie chart. 我有一个带有值的表,我想使用表中的数据动态创建饼图。 I am trying to store the table data within an array, and then using that array to add the pie chart labels and data. 我试图将表数据存储在一个数组中,然后使用该数组添加饼图标签和数据。 However, the code below does not work: 但是,以下代码不起作用:

 var table = document.getElementById("myTable"); var tableArr = []; var tableLen = table.rows.length; for (var i = 1; i < tableLen; i++) { tableArr.push({ name: table.rows[i].cells[0].innerHTML, population: table.rows[i].cells[1].innerHTML, area: table.rows[i].cells[2].innerHTML }); } var canvasP = document.getElementById("pieChart"); var ctxP = canvasP.getContext('2d'); var myPieChart = new Chart(ctxP, { type: 'pie', data: { labels: tableArr["name"], datasets: [{ data: tableArr["population"], backgroundColor: ["#64B5F6", "#FFD54F", "#2196F3", "#FFC107", "#1976D2", "#FFA000", "#0D47A1"], hoverBackgroundColor: ["#B2EBF2", "#FFCCBC", "#4DD0E1", "#FF8A65", "#00BCD4", "#FF5722", "#0097A7"] }] }, options: { legend: { display: true, position: "right" } } }); 
 <script src="https://cdnjs.com/libraries/Chart.js"></script> <table id="myTable"> <tr> <thead> <th scope="col">Village</th> <th scope="col">Population</th> <th scope="col">Area</th> </thead> </tr> <tr> <tbody> <th scope="row">San Jose</th> <td>2,332</td> <td>12.46</td> </tr> <tr> <th scope="row">Santa Maria</th> <td>2,551</td> <td>4.65</td> </tr> <tr> <th scope="row">San Francisco</th> <td>544</td> <td>7.77</td> </tr> </tbody> </table> <canvas id="pieChart"></canvas> 

First of all: make sure, that your markup is valid! 首先:请确保您的标记有效! Your table is messed up with <thead> and <tbody> tags inside <tr> elements. 您的表被<tr>元素内的<tr> <thead><tbody>标记弄乱了。

Then you need to restructure your data. 然后,您需要重组数据。 I recommend to read the documentation on that. 我建议阅读有关的文档 You labels and your data need to be array of the same size, where the elements match each other. 标签和数据必须是相同大小的数组,其中元素彼此匹配。 See the working example below for clarity. 为清楚起见,请参见下面的工作示例。

For the data retrieval, it's better to take innerText for you only want to get the text content. 对于数据检索,最好采用innerText因为您只想获取文本内容。 Also you need to remove the , from your population data for Chart.js to recognize the number. 另外,您还需要从总体数据中删除,以便Chart.js识别数字。

 var table = document.getElementById("myTable") var tableLen = table.rows.length var data = {labels: [], population: [], area: [] } for (var i = 1; i < tableLen; i++) { data.labels.push(table.rows[i].cells[0].innerText) data.population.push(table.rows[i].cells[1].innerText.replace(',','')) data.area.push(table.rows[i].cells[2].innerText) } var canvasP = document.getElementById("pieChart") var ctxP = canvasP.getContext('2d') var myPieChart = new Chart(ctxP, { type: 'pie', data: { labels: data.labels, datasets: [{ data: data.population, backgroundColor: ["#64B5F6", "#FFD54F", "#2196F3", "#FFC107", "#1976D2", "#FFA000", "#0D47A1"], hoverBackgroundColor: ["#B2EBF2", "#FFCCBC", "#4DD0E1", "#FF8A65", "#00BCD4", "#FF5722", "#0097A7"] }] }, options: { legend: { display: true, position: "right" } } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script> <script src="https://cdnjs.com/libraries/Chart.js"></script> <table id="myTable"> <thead> <tr> <th scope="col">Village</th> <th scope="col">Population</th> <th scope="col">Area</th> </tr> </thead> <tbody> <tr> <th scope="row">San Jose</th> <td>2,332</td> <td>12.46</td> </tr> <tr> <th scope="row">Santa Maria</th> <td>2,551</td> <td>4.65</td> </tr> <tr> <th scope="row">San Francisco</th> <td>544</td> <td>7.77</td> </tr> </tbody> </table> <canvas id="pieChart"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM