Tell us what’s happening:
Hi everyone,
I’m trying to add the “x” and “y” axis to a graph but it doesn’t work and I can’t figure out the motivation.
Someone is so kind to give some advice?
If it can be usefull I leave the link to the CodePen Project
Your code so far
HTML
<div class="main">
<div class="container">
<div id="title">Gross Domestic Product USA</div>
<div class="visHolder"></div>
</div>
</div>
JavaScript
const width = 1000;
const height = 500;
const padding = 0;
//Creating SVG
const svg = d3
.select(".visHolder")
.append("svg")
.attr("width", width)
.attr("height", height);
//Adding data
d3.json(
"https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json",
function (e, dataset) {
//Returning an array of year+quarter values of each date
let years = dataset.data.map(function(item) {
let quarter;
let month = item[0].substring(5, 7);
if (month === "01") {
quarter = "Q1";
} else if (month === "04") {
quarter = "Q2";
} else if (month === "07") {
quarter = "Q3";
} else {
quarter = "Q4";
}
return item[0].substring(0,4) + " " + quarter;
});
//Returning an array of GDP values
let GDP = dataset.date.map(function(item) {
return item[1];
})
//Defining scale
let dateObject = dataset.data.map(function(item) {
return new Date(item[0]);
});
const xScale = d3
.scaleTime()
.domain([d3.min(dateObject), d3.max(dateObject)])
.range([padding, width-padding]);
const yScale() = d3
.scaleLinear()
.domanin([0, d3.max(GDP)])
.range([height-padding, padding]);
//Appending axis
const xAxis = d3.axisBottom().scale(xScale);
svg
.append("g")
.call(xAxis)
.attr("id", "x-axis");
const yAxis = d3.axisLeft().scale(yScale);
svg
.append("g")
.call(yAxis)
.attr("id", "y-axis");
}
);
Your browser information:
User Agent is: Mozilla/5.0 (Android 9; Mobile; rv:86.0) Gecko/86.0 Firefox/86.0
Challenge: Visualize Data with a Bar Chart
Link to the challenge: