score:2
Accepted answer
This gives u list data from api between your expected dates
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 categories=[];
var confirms=[];
var recovers=[];
var result = data[counryName].filter(function(item) {
return dateArr.includes(item.date);
})
result.forEach(item=>{
categories.push(item.date);
confirms.push(item.confirmed);
recovers.push(item.recovered);
})
var options = {
type: 'line',
data: {
labels: categories,
datasets: [
{
label: '# of Confirmed',
data: confirms,
borderWidth: 1
},
{
label: '# of Recovered',
data:recovers,
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
console.log(result);
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
score:0
The problem is that the end date is not formatted as date and the loop (while) is not doing it correctly. The solution is new Date(end)
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;
}
and the complete code would be
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)
Source: stackoverflow.com
Related Query
- Filter dates from the first day of the month to the current date
- Laravel determine the date from database if MONDAY-SUNDAY
- How to show dynamic dates with month name of current month and previous month in momentjs?
- Passing an Array from a Flask view to the javascript code of another view
- PHP Date and Time, getting the 1st, 2nd, 3rd, and 4th week in a month
- How to take a set of Date objects and display the frequency based on time of day using ChartJS
- Change the graph from 1 Day to 15 mins when it is being loaded for the 1st time in ChartIQ
- Have the time on x axes to show the last 24 hours from current time
- Sql Server query that returns prices from each shop on each date and adds a 0 if no data is present for the shopId on a particular date
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- How do I add Date range filter in chartjs in the context of Vue.js
- Change the Y-axis values from real numbers to integers in Chart.js
- chart.js Failed to create chart: can't acquire context from the given item
- How do I prevent the scale labels from being cut off in chartjs?
- how to change the Y-axis values from float number to integer in chartjs?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- Testing Chart.js with Jest/Enzyme - Failed to create chart: can't acquire context from the given item
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- Chart.js - consolidating days to month totals along the x (time) axis?
- How can I trigger the hover mode from outside the chart with charts.js 2?
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- Chart.js remove the first vertical line
- How to change the Y-axis values from numbers to strings in Chart.js?
- increase the gap between y-axis and first x-axis value chart.js
- react testing library: can't acquire context from the given item
- Filter ChartJS using data from PHP
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- Chart.js – how to remove data entries from the tooltip?
- Lift the bar up from the bottom in bar type chart
- Charts.js - Need to remove the data value from the Tooltip
More Query from same tag
- what is wrong with my code ? java script chart position
- Redraw Chart.js Chart with Json_encoded array from ajax request
- Chartjs duration horizontal bar chart
- Chart.js line chart with correctly spaced x labels
- getBoundingClientRect gives incorrect value
- How to put dynamic value in Doughnutchart Using chartjs?
- How can I achieve something like this image with chart.js(react-chartjs-2)?
- Declare data value dynamicaly to chart js
- refs in reactjs Component empty
- ChartJS: add a clickable text in title
- How to keep bars same size even when adding new datasets
- what is `dart.js` in chartjs.dart example
- How to turn off tooltip for primeng pie chart?
- Chart.js - How to show loading animation before plotting chart
- charts labels and data with php arrays
- chart.js 3 and time axis displays wrong date data
- Chart.js with line chart and bar chart - bar chart not rendered although the max value of it is shown
- Use multi data to draw chart with chartjs
- How can i create multiple components with pie charts?
- How to update with animation a Pie Chart?
- ChartJS chart is bugged after adding new data
- Extjs with Devexpress ChartJS
- JavaScript array value to chart.js graph as stacked graph
- ChartJS, updating chart type does not update xAxis properly
- Is it possible to combine two Y axes into a single tooltip with ChartJS 2?
- Chartjs - pointColor to follow current color of gradient stroke
- Chart.js How to align x-ticks?
- How do I customize y-axis labels on a Chart.js line chart?
- how do I render PyChart.JS charts in web2py?
- Chart.js not appearing in tabs