将多维数组传入 Google.visualization.arrayToDataTable
Pass multi-dimension array into Google.visualization.arrayToDataTable
我想使用 Google Chart 绘制烛台图,我有一个多数组数组调用 chartdata
但我未能将多数组数组传递到 google.visualization.arrayToDataTable([chartdata],true)
并给出错误Last domain does not have enough data columns (missing 3)
应该是这样的var data = google.visualization.arrayToDataTable([ ['2022-05-25 12:00',12,864,889,76], ['2022-05-25 13:00',765,45,97,82] ],true);
html
<div >
<div>
<button>OK</button>
<table id="haha">
<thead>
<tr>
<td>date</td>
<td>open</td>
<td>close</td>
<td>high</td>
<td>low</td>
<td>change</td>
</tr>
</thead>
<tr class="data">
<td class="date_">2022-05-25 12:00</td>
<td class="open">12</td>
<td class="close">864</td>
<td class="high">889</td>
<td class="low">76</td>
<td class="change">66</td>
</tr>
<tr class="data">
<td class="date_">2022-05-25 13:00</td>
<td class="open">765</td>
<td class="close">45</td>
<td class="high">97</td>
<td class="low">82</td>
<td class="change">613</td>
</tr>
</table>
</div>
</div>
<div id="chart" style="width: 900px; height: 500px;"></div>
js
$(document).ready(function(){
$(document).on("click", "button", function(){
var parenthis = this.parentElement;
var tr = parenthis.querySelectorAll('.data');
var chartdata = [];
for(var x=0 ; x < tr.length ; x++){
var subdata = [];
var array=['date_','open','close','high','low'];
for(var y=0 ; y < array.length ; y++){
var class_table = parenthis.querySelectorAll("[class=" + CSS.escape(array[y]) + "]");
if(y==0){
subdata.push(class_table[x].innerHTML);
}
else if(y==4){
subdata.push(parseFloat(class_table[x].innerHTML));
chartdata.push(subdata);
}
else{
subdata.push(parseFloat(class_table[x].innerHTML))
}
};
};
// candlestick chart
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(){
// [[str,num/float,num/float,num/float,num/float],]
var data = google.visualization.arrayToDataTable([chartdata],true);
var options = {
legend:'none',
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart'));
chart.draw(data, options);
};
})
});
在你的脚本中,做如下修改怎么样?
发件人:
var data = google.visualization.arrayToDataTable([chartdata],true);
收件人:
var data = google.visualization.arrayToDataTable(chartdata,true);
- 在您的脚本中,
chartdata
是二维数组。所以,我想这也许可以直接使用。
参考:
我想使用 Google Chart 绘制烛台图,我有一个多数组数组调用 chartdata
但我未能将多数组数组传递到 google.visualization.arrayToDataTable([chartdata],true)
并给出错误Last domain does not have enough data columns (missing 3)
应该是这样的var data = google.visualization.arrayToDataTable([ ['2022-05-25 12:00',12,864,889,76], ['2022-05-25 13:00',765,45,97,82] ],true);
html
<div >
<div>
<button>OK</button>
<table id="haha">
<thead>
<tr>
<td>date</td>
<td>open</td>
<td>close</td>
<td>high</td>
<td>low</td>
<td>change</td>
</tr>
</thead>
<tr class="data">
<td class="date_">2022-05-25 12:00</td>
<td class="open">12</td>
<td class="close">864</td>
<td class="high">889</td>
<td class="low">76</td>
<td class="change">66</td>
</tr>
<tr class="data">
<td class="date_">2022-05-25 13:00</td>
<td class="open">765</td>
<td class="close">45</td>
<td class="high">97</td>
<td class="low">82</td>
<td class="change">613</td>
</tr>
</table>
</div>
</div>
<div id="chart" style="width: 900px; height: 500px;"></div>
js
$(document).ready(function(){
$(document).on("click", "button", function(){
var parenthis = this.parentElement;
var tr = parenthis.querySelectorAll('.data');
var chartdata = [];
for(var x=0 ; x < tr.length ; x++){
var subdata = [];
var array=['date_','open','close','high','low'];
for(var y=0 ; y < array.length ; y++){
var class_table = parenthis.querySelectorAll("[class=" + CSS.escape(array[y]) + "]");
if(y==0){
subdata.push(class_table[x].innerHTML);
}
else if(y==4){
subdata.push(parseFloat(class_table[x].innerHTML));
chartdata.push(subdata);
}
else{
subdata.push(parseFloat(class_table[x].innerHTML))
}
};
};
// candlestick chart
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(){
// [[str,num/float,num/float,num/float,num/float],]
var data = google.visualization.arrayToDataTable([chartdata],true);
var options = {
legend:'none',
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart'));
chart.draw(data, options);
};
})
});
在你的脚本中,做如下修改怎么样?
发件人:
var data = google.visualization.arrayToDataTable([chartdata],true);
收件人:
var data = google.visualization.arrayToDataTable(chartdata,true);
- 在您的脚本中,
chartdata
是二维数组。所以,我想这也许可以直接使用。