I want to draw a candlestick chart by using Google Chart , i have a multi-array array call chartdata
and i failed to pass the multi-array array into google.visualization.arrayToDataTable([chartdata],true)
and give errorLast domain does not have enough data columns (missing 3)
it should look like this 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
JavaScript
x
35
35
1
<div >
2
<div>
3
<button>OK</button>
4
<table id="haha">
5
<thead>
6
<tr>
7
<td>date</td>
8
<td>open</td>
9
<td>close</td>
10
<td>high</td>
11
<td>low</td>
12
<td>change</td>
13
</tr>
14
</thead>
15
<tr class="data">
16
<td class="date_">2022-05-25 12:00</td>
17
<td class="open">12</td>
18
<td class="close">864</td>
19
<td class="high">889</td>
20
<td class="low">76</td>
21
<td class="change">66</td>
22
</tr>
23
<tr class="data">
24
<td class="date_">2022-05-25 13:00</td>
25
<td class="open">765</td>
26
<td class="close">45</td>
27
<td class="high">97</td>
28
<td class="low">82</td>
29
<td class="change">613</td>
30
</tr>
31
</table>
32
</div>
33
</div>
34
<div id="chart" style="width: 900px; height: 500px;"></div>
35
js
JavaScript
1
51
51
1
$(document).ready(function(){
2
$(document).on("click", "button", function(){
3
4
var parenthis = this.parentElement;
5
var tr = parenthis.querySelectorAll('.data');
6
7
var chartdata = [];
8
9
for(var x=0 ; x < tr.length ; x++){
10
11
var subdata = [];
12
var array=['date_','open','close','high','low'];
13
14
for(var y=0 ; y < array.length ; y++){
15
16
17
var class_table = parenthis.querySelectorAll("[class=" + CSS.escape(array[y]) + "]");
18
19
if(y==0){
20
subdata.push(class_table[x].innerHTML);
21
}
22
else if(y==4){
23
subdata.push(parseFloat(class_table[x].innerHTML));
24
chartdata.push(subdata);
25
}
26
else{
27
subdata.push(parseFloat(class_table[x].innerHTML))
28
}
29
30
};
31
};
32
33
// candlestick chart
34
google.charts.load('current', {'packages':['corechart']});
35
google.charts.setOnLoadCallback(drawChart);
36
37
function drawChart(){
38
39
// [[str,num/float,num/float,num/float,num/float],]
40
var data = google.visualization.arrayToDataTable([chartdata],true);
41
42
var options = {
43
legend:'none',
44
};
45
46
var chart = new google.visualization.CandlestickChart(document.getElementById('chart'));
47
chart.draw(data, options);
48
};
49
})
50
});
51
Advertisement
Answer
In your script, how about the following modification?
From:
JavaScript
1
2
1
var data = google.visualization.arrayToDataTable([chartdata],true);
2
To:
JavaScript
1
2
1
var data = google.visualization.arrayToDataTable(chartdata,true);
2
- In your script,
chartdata
is the 2-dimensional array. So, I thought that this might be able to be directly used.