我有一个从Json API提取数据的图表。我只想显示从月初到当前日期的日期。
开始日期是该月的第一天,结束日期是当前日期
我尝试了以下方法。但它返回的是不确定的。我正在寻求与此代码的协助。
谢谢
let dates = [];
let confirmed = [];
let recovered = [];
let deaths = [];
var today = new Date();
var TodayDate = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var startDate = '01-04-2020'; //YYYY-MM-DD
var endDate = TodayDate; //YYYY-MM-DD
var getDateArray = function(start, end) {
var arr = new Array();
var dt = new Date(start);
while (dt <= end) {
arr.push(new Date(dt));
dt.setDate(dt.getDate() + 1);
}
return arr;
}
var dateArr = getDateArray(startDate, endDate);
Below is my code.
fetch("https://pomber.github.io/covid19/timeseries.json")
.then(response => response.json())
.then(data=> {
data["South Africa"].forEach(o => {
dates.filter(function() {
o.date==dateArr;
}).push(o.date);
confirmed.push(o.confirmed);
recovered.push(o.recovered);
deaths.push(o.deaths);
})
new Chart(document.getElementById('myChart'), {
type: 'line',
data: {
labels: dates,
datasets: [{
label: 'Confirmed',
borderColor: 'orange',
backgroundColor: 'orange',
fill: 'false',
data: confirmed
},
{
label: 'Recovered',
borderColor: 'green',
backgroundColor: 'green',
fill: 'false',
data: recovered
},
{
label: 'Deaths',
borderColor: 'red',
backgroundColor: 'red',
fill: 'false',
data: deaths
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'COVID-19 / Eswatini Time Series'
}
}
});
});
</script>
问题在于,结束日期未格式化为日期,并且循环(而)未正确执行。解决方法是新日期(结束)
var getDateArray = function(start, end) {
var arr = new Array();
var dt = new Date(start);
while (dt <= new Date(end)) {
arr.push(new Date(dt));
dt.setDate(dt.getDate() + 1);
}
return arr;
}
完整的代码将是
let dates = [];
let confirmed = [];
let recovered = [];
let deaths = [];
var today = new Date();
var TodayDate = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var startDate = '01-04-2020'; //YYYY-MM-DD
var endDate = TodayDate; //YYYY-MM-DD
var getDateArray = function(start, end) {
var arr = new Array();
var dt = new Date(start);
while (dt <= new Date(end)) {
arr.push(new Date(dt));
dt.setDate(dt.getDate() + 1);
}
return arr;
}
var dateArr = getDateArray(startDate, endDate);
fetch("https://pomber.github.io/covid19/timeseries.json")
.then(response => response.json())
.then(data=> {
data["South Africa"].forEach(o => {
dates.filter(function() {
o.date==dateArr;
}).push(o.date);
confirmed.push(o.confirmed);
recovered.push(o.recovered);
deaths.push(o.deaths);
})
new Chart(document.getElementById('myChart'), {
type: 'line',
data: {
labels: dates,
datasets: [{
label: 'Confirmed',
borderColor: 'orange',
backgroundColor: 'orange',
fill: 'false',
data: confirmed
},
{
label: 'Recovered',
borderColor: 'green',
backgroundColor: 'green',
fill: 'false',
data: recovered
},
{
label: 'Deaths',
borderColor: 'red',
backgroundColor: 'red',
fill: 'false',
data: deaths
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'COVID-19 / Eswatini Time Series'
}
}
});
});
console.log(dateArr)
这会给您提供您在预期日期之间的api列表数据
var today = new Date();
var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
var getDateArray = function(start, end) {
var arr = new Array();
var dt = start;
while (dt <= end) {
arr.push(dt.getFullYear()+'-'+(dt.getMonth()+1)+'-'+dt.getDate());
dt.setDate(dt.getDate() + 1);
}
return arr;
}
var dateArr = getDateArray(firstDay, today);
getDatas("South Africa");
function getDatas(counryName){
fetch("https://pomber.github.io/covid19/timeseries.json")
.then(response => response.json())
.then(data=> {
var result = data[counryName].filter(function(item) {
return dateArr.includes(item.date);
})
console.log(result);
});
}