我正在尝试创建汇率图表。在数组中,我正在写入有关汇率的数据,并且希望将此数据移动到图表中。而且我不知道该怎么做。
这是数组["25.09", "25.03", "24.88"]
0: "25.09"
1: "25.03"
2: "24.88"
Array
var dataArr = [];
async function currency(e) {
chartData.series[0].data = [];
var sdate=startdate.value.split("-").join("");
var edate=enddate.value.split("-").join("");
for (var i = Number(sdate); i <= Number(edate); i++){
await delay(2000)
let url=`https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?valcode=${currenc.value}&date=${i}&json`;
let response= await fetch(url);
let data =await response.json();
var itemObj={
rate:data[0].rate,
exchangedate:data[0].exchangedate
};
dataArr.push(itemObj.rate.toFixed(2)); //here push elements to array
}
console.log(dataArr);
}
var chartData = {
title: {
text: 'NBU Currency'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: 'Exchange rate'
}
},
legend: {
enabled: false
},
plotOptions: {
series: {
label: {
connectorAllowed: false
},
}
},
series: [{
name: '',
data:[] // OVER HERE!!
}],
responsive: {
rules: [{
condition: {
maxWidth: 500
}
}]
}
}
Highcharts.chart('container', chartData);
2件事:
let pushItem = dataArr[0];
dataArr2.push(pushItem);
您的第二个数组(dataArr2)现在将包含该项目。
您还可以通过这种方式存储对象或其他数组。希望有帮助