Skip to content

Instantly share code, notes, and snippets.

@lisantwi
Created August 29, 2019 16:27
Show Gist options
  • Save lisantwi/3b0541c348ad0a44cdca5dd4e62937f1 to your computer and use it in GitHub Desktop.
Save lisantwi/3b0541c348ad0a44cdca5dd4e62937f1 to your computer and use it in GitHub Desktop.
document.addEventListener('DOMContentLoaded', () => {
console.log("loaded")
fetchCountryData()
})
function fetchCountryData () {
fetch('http://api.worldbank.org/v2/country/US/indicator/NY.GDP.MKTP.CD?format=json')
.then(resp => resp.json())
.then(data => {
let years = data[1].map(year => year.date).reverse()
let gdps = data[1].map(year => year.value).reverse()
createChart(years,gdps)
})
}
function createChart(years,gdps){
new Chart(document.getElementById("line-chart"), {
type: 'line',
data: {
labels: years,
datasets: [{
data: gdps,
label: "USA GDP",
fill: false,
backgroundColor: 'blue',
borderColor: 'blue',
pointBorderColor: 'blue',
pointRadius: 1,
}
]
},
options: {
title: {
display: true,
text: 'USA GDP Data 1969 - 2019',
},
animation: false,
legend: {display: true},
maintainAspectRatio: false,
responsive: true,
responsiveAnimationDuration: 0,
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
if(parseInt(value) >= 1000){
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return '$' + value;
}
}
}
}]
}
}
});
}
@edwardtilley
Copy link

How is this changed to chart two or three countries?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment