Add Bar Chart Certification Project
This commit is contained in:
parent
364266b2e7
commit
fe0f8d08ad
5 changed files with 148 additions and 0 deletions
32
4-data-visualization/1-bar-chart/index.css
Normal file
32
4-data-visualization/1-bar-chart/index.css
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
body {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
margin: 0;
|
||||||
|
height: 100vh;
|
||||||
|
width: 100vw;
|
||||||
|
background-color: grey;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tooltip {
|
||||||
|
background-color: tomato;
|
||||||
|
display: none;
|
||||||
|
height: fit-content;
|
||||||
|
width: fit-content;
|
||||||
|
opacity: 0.8;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 20px;
|
||||||
|
position: fixed;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chart {
|
||||||
|
width: 900px;
|
||||||
|
height: 500px;
|
||||||
|
text-align: center;
|
||||||
|
background-color: navajowhite;
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: rgba(0, 0, 0, 0.25) 0 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*# sourceMappingURL=index.css.map */
|
1
4-data-visualization/1-bar-chart/index.css.map
Normal file
1
4-data-visualization/1-bar-chart/index.css.map
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"version":3,"sourceRoot":"","sources":["index.scss"],"names":[],"mappings":"AAAA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA","file":"index.css"}
|
16
4-data-visualization/1-bar-chart/index.html
Normal file
16
4-data-visualization/1-bar-chart/index.html
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>FreeCodeCamp Barchart</title>
|
||||||
|
<link rel="stylesheet" href="index.css">
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="chart">
|
||||||
|
<h1 id="title">United States GDP</h1>
|
||||||
|
</div>
|
||||||
|
<div id="tooltip"></div>
|
||||||
|
<script src="index.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
69
4-data-visualization/1-bar-chart/index.js
Normal file
69
4-data-visualization/1-bar-chart/index.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
const w = 800
|
||||||
|
const h = 400
|
||||||
|
|
||||||
|
d3.json('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json')
|
||||||
|
.then(data => {
|
||||||
|
const dataset = data.data
|
||||||
|
|
||||||
|
d3.select('#chart')
|
||||||
|
.append('svg')
|
||||||
|
.attr('height', h)
|
||||||
|
.attr('width', w)
|
||||||
|
|
||||||
|
function getDate(d) {
|
||||||
|
console.log(d, new Date(d[0]))
|
||||||
|
return new Date(d[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const xScale = d3.scaleTime()
|
||||||
|
.domain([getDate(dataset[0]), getDate(dataset[dataset.length - 1])])
|
||||||
|
.range([0, w])
|
||||||
|
|
||||||
|
const yScale = d3.scaleLinear()
|
||||||
|
.domain([0, d3.max(dataset, (d) => d[1])])
|
||||||
|
.range([h, 0])
|
||||||
|
|
||||||
|
const xAxis = d3.axisBottom().scale(xScale);
|
||||||
|
const yAxis = d3.axisLeft().scale(yScale);
|
||||||
|
|
||||||
|
d3.select('svg')
|
||||||
|
.append('g')
|
||||||
|
.call(xAxis)
|
||||||
|
.attr('id', 'x-axis')
|
||||||
|
.attr('transform', 'translate(40, 360)')
|
||||||
|
|
||||||
|
d3.select('svg')
|
||||||
|
.append('g')
|
||||||
|
.call(yAxis)
|
||||||
|
.attr('id', 'y-axis')
|
||||||
|
.attr('transform', 'translate(40, -40)')
|
||||||
|
|
||||||
|
|
||||||
|
d3.select('svg')
|
||||||
|
.selectAll('rect')
|
||||||
|
.data(dataset)
|
||||||
|
.enter()
|
||||||
|
.append('rect')
|
||||||
|
.attr('class', 'bar')
|
||||||
|
.attr('x', (d) => xScale(getDate(d)) + 40)
|
||||||
|
.attr("y", (d) => yScale(d[1]) - 40)
|
||||||
|
.attr('width', () => w / 275)
|
||||||
|
.attr("height", (d) => h - yScale(d[1]))
|
||||||
|
.attr('data-date', (d) => d[0])
|
||||||
|
.attr('data-gdp', (d) => d[1])
|
||||||
|
.style('fill', '#74dbf1')
|
||||||
|
.on('mouseover', (e) => {
|
||||||
|
const date = e.target.__data__[0]
|
||||||
|
const value = e.target.__data__[1]
|
||||||
|
d3.select('#tooltip')
|
||||||
|
.attr('data-date', date)
|
||||||
|
.style('display', 'initial')
|
||||||
|
.text(() => 'Date: ' + date + " GDP: " + value)
|
||||||
|
})
|
||||||
|
.on('mouseout', () => {
|
||||||
|
d3.select('#tooltip')
|
||||||
|
.style('display', 'none')
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
30
4-data-visualization/1-bar-chart/index.scss
Normal file
30
4-data-visualization/1-bar-chart/index.scss
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
body {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
margin: 0;
|
||||||
|
height: 100vh;
|
||||||
|
width: 100vw;
|
||||||
|
background-color: grey;
|
||||||
|
}
|
||||||
|
|
||||||
|
#tooltip{
|
||||||
|
background-color: tomato;
|
||||||
|
display: none;
|
||||||
|
height: fit-content;
|
||||||
|
width: fit-content;
|
||||||
|
opacity: 0.8;
|
||||||
|
border-radius: 5px;
|
||||||
|
padding: 20px;
|
||||||
|
position: fixed;
|
||||||
|
z-index: 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chart {
|
||||||
|
width: 900px;
|
||||||
|
height: 500px;
|
||||||
|
text-align: center;
|
||||||
|
background-color: navajowhite;
|
||||||
|
border-radius: 5px;
|
||||||
|
box-shadow: rgba(0, 0, 0, 0.25) 0 54px 55px, rgba(0, 0, 0, 0.12) 0px -12px 30px, rgba(0, 0, 0, 0.12) 0px 4px 6px, rgba(0, 0, 0, 0.17) 0px 12px 13px, rgba(0, 0, 0, 0.09) 0px -3px 5px;
|
||||||
|
}
|
Reference in a new issue