Add Bar Chart Certification Project

This commit is contained in:
CherryKitten 2022-10-23 16:09:09 +02:00
parent 364266b2e7
commit fe0f8d08ad
Signed by: sammy
GPG key ID: 0B696A86A853E955
5 changed files with 148 additions and 0 deletions

View 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 */

View 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"}

View 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>

View 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')
})
}
)

View 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;
}