作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我有如下所示的图表和表格,并且我想将图表数据传递到表格单元格中。我该如何通过呢?
Highcharts.chart('container', {
chart: {
type: 'bar'
},
series: {
data: [100,200, 300],
},
});
<div id="container"></div>
<table id="dataTable">
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
最佳答案
以下是使用 Highcharts 和 jQuery 用图表中的数据填充 HTML 表格单元格的基本方法。
首先,我们要在表中设置一个占位符行,我们将在其中填充数据。我已为该行指定了 RowToFill
ID,以便我们可以在 Javascript 中引用它。
HTML:
<div id="container" style="height: 400px"></div>
<table id="dataTable">
<thead>
<tr>
<th>First Column</th>
<th>Second Column</th>
<th>Third Column</th>
</tr>
</thead>
<tbody>
<tr id="RowToFill">
</tr>
</tbody>
</table>
接下来,我们将浏览图表的数据,并使用 jQuery append()
函数将表格单元格添加到占位符行。
Javascript:
/* set the chart to a variable so you can get to the data later */
var thisChart = Highcharts.chart('container', {
chart: {
type: 'bar'
},
xAxis: {
categories: ['A', 'B', 'C']
},
series: [{
data: [100,200,300]
}]
});
/* go through each item in the chart series */
$(thisChart.series[0].data).each(function( index ) {
/* add a table cell to the row where it should go */
$('#RowToFill').append('<td>' + this.y + '</td>');
});
这是此解决方案的有效 fiddle :http://jsfiddle.net/brightmatrix/5ad8fgzp/
为了使其更加灵活,您可以修改此函数以首先运行 x 轴类别列表来创建表标题行。这将使表格主体行中的单元格数量与图表中的数据点数量相匹配。
希望这对您有帮助。
关于javascript - 我如何从 Highcharts 数据填充表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51135583/
我是一名优秀的程序员,十分优秀!