Created
October 20, 2020 21:19
-
-
Save waseemnawaz/793a8a8004f55878a9aa2d8953532795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var belgdata = d3.csv( | |
"https://raw.githubusercontent.com/eschnou/covid19-be/master/covid19-belgium.csv" | |
).then(function(data){ | |
//console.log(data) | |
generate(data) | |
}) | |
function generate(dataset){ | |
//parse the time data | |
var time_parse = d3.timeParse("%d/%m/%Y") | |
dataset.forEach(function(e,i){ | |
dataset[i].date = time_parse(e.date) | |
dataset[i].daily_cases = +dataset[i].daily_cases | |
}) | |
console.log(dataset) | |
var newData = dataset.forEach(function(d){ | |
d.daily_cases = +d.daily_cases | |
}) | |
//create the svg | |
var chart_width = 800 | |
var chart_height = 400 | |
var margin = 50 | |
var svg = d3.select('#chart') | |
.append('svg') | |
.attr('width', chart_width) | |
.attr('height', chart_height) | |
//create the scales | |
var x_scale = d3.scaleTime() | |
.domain([d3.min(dataset, function(d){ | |
return d.date | |
}), d3.max(dataset, function(d){ | |
return d.date | |
})]) | |
.rangeRound([margin, chart_width - margin]) | |
var y_scale = d3.scaleLinear() | |
.domain([d3.min(dataset, function(d){ | |
return d.daily_cases | |
}), d3.max(dataset, function(d){ | |
return d.daily_cases | |
})]) | |
.rangeRound([chart_height - margin, margin]) | |
//create the axes | |
var x_axis = d3.axisBottom(x_scale) | |
svg.append('g') | |
.attr('class', 'x-axis') | |
.attr('transform', 'translate(0,' + (chart_height - margin) + ')') | |
.call(x_axis) | |
var y_axis = d3.axisLeft(y_scale) | |
svg.append('g') | |
.attr('class', 'y-axis') | |
.attr('transform', 'translate(' + margin + ',0)') | |
.call(y_axis) | |
//clippath | |
svg.append('clipPath') | |
.attr('id', 'theChartClipPathIsARectangle') | |
.append('rect') | |
.attr('x', margin) | |
.attr('x', margin) | |
.attr('height', chart_height - margin) | |
.attr('width', chart_width - margin) | |
.attr('fill', 'yellow') | |
//create the bars | |
svg.append('g') | |
.attr('id', 'circleId') | |
.attr('clip-path', 'url(#theChartClipPathIsARectangle)') | |
.selectAll('circle') | |
.data(dataset) | |
.enter() | |
.append('circle') | |
.attr('cx', function(d){ | |
return x_scale(d.date) | |
}) | |
.attr('cy', function(d){ | |
return y_scale(d.daily_cases) | |
}) | |
.attr('r', 3) | |
.attr('fill', 'teal') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>D3 Bar Chart</title> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
</head> | |
<body> | |
<h1 class="title">Daily cases of Covid-19 in Belgium between March and May 2020</h1> | |
<div id="chart"></div> | |
<!--hr> | |
<button type="button">Update</button--> | |
<script src="https://d3js.org/d3.v5.js"></script> | |
<script src="app2.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#chart{ | |
width: 800px; | |
height: 400px; | |
background-color: #f7f7f7; | |
margin: 25px auto; | |
} | |
.bar{ | |
display: inline-block; | |
width: 25px; | |
height: 300px; | |
background-color: #7ED26D; | |
margin-right: 5px; | |
} | |
.title{ | |
display: block; | |
margin: 0 auto; | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment