Skip to content

Instantly share code, notes, and snippets.

@Sisekelo
Created July 9, 2018 11:22
Show Gist options
  • Select an option

  • Save Sisekelo/c653f8c6384d74525d45fffe93bda408 to your computer and use it in GitHub Desktop.

Select an option

Save Sisekelo/c653f8c6384d74525d45fffe93bda408 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Graph JS</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>
</head>
<body>
<div class="container">
<canvas id="myChart">
</canvas>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
{
"population":[
{
"city": "Mbabane",
"people": 100000
},
{
"city": "Manzini",
"people": 80000
},
{
"city": "Siteki",
"people": 30000
},
{
"city": "Logoba",
"people": 50000
}
]
}
var response2;
var cities = [];
var people = [];
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(xhttp.responseText);
console.log(response.population);
response2 = response.population;
for(e in response2){
cities.push(response2[e].city);
people.push(response2[e].people);
}
}
};
xhttp.open("GET", "population.json", false);
xhttp.send();
var myChart = document.getElementById("myChart").getContext('2d');
var finalVisual = new Chart(myChart, {
type: 'bar',
data: {
labels: cities,
datasets: [{
label: 'Population',
data: people,
backgroundColor: [
'rgba(126, 127, 154, 0.7)',
'rgba(235, 148, 134, 0.7)',
'rgba(179, 151, 167, 0.7)',
'rgba(243, 222, 138, 0.7)',
],
borderColor: [
'rgba(126, 127, 154, 1)',
'rgba(235, 148, 134, 1)',
'rgba(179, 151, 167, 1)',
'rgba(243, 222, 138, 1)',
],
borderWidth: 3,
hoverBackgroundColor: 'red',
}]
},
options: {
legend: {
display: true,
position: 'right',
labels: {
fontColor: 'blue'
}
},
title: {
display: true,
text: 'Largest cities in Swaziland',
fontSize: 25,
},
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
@tambetabot
Copy link

Very good man.

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