Data Visualization - US GDP

Developer
Size
3,131 Kb
Views
2,024

How do I make an data visualization - us gdp?

Fulfill the below user stories. User Story: I can see US Gross Domestic Product by quarter, over time.. What is a data visualization - us gdp? How do you make a data visualization - us gdp? This script and codes were developed by Sam Koshy on 14 January 2023, Saturday.

Data Visualization - US GDP Previews

Data Visualization - US GDP - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Data Visualization - US GDP</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="chartTitle"><h1>US Gross Domestic Product</h1></div> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.1/d3.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Data Visualization - US GDP - Script Codes CSS Codes

.chartTitle{ text-align:center
}
svg{ display: block; margin:-40px auto;
}
.bar:hover{ fill:#AA3939;
}
.tooltip{ position:absolute; width: auto; line-height: 0.01; padding-left:5px; padding-right:5px; border-radius:10px; font-family:Helvetica; background: #BCD9D9; white-space: nowrap; /*nowrap avoids a line break when approaching the window margin on right*/
}

Data Visualization - US GDP - Script Codes JS Codes

const w = 1000;
const h = 600;
const padding = 60;
const svg = d3.select("body").append("svg").attr("width", w).attr("height", h);
var month=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
d3.json( "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json", function(gdpData) { const barW = w / gdpData.data.length; const startDate = parseFloat(gdpData.from_date.substring(0, 4)) + parseFloat(gdpData.from_date.substring(5, 7)) / 12; const endDate = parseFloat(gdpData.to_date.substring(0, 4)) + parseFloat(gdpData.to_date.substring(5, 7)) / 12; const xScale = d3 .scaleLinear() .domain([startDate, endDate]) .range([padding, w - padding]); const yScale = d3 .scaleLinear() .domain([0, d3.max(gdpData.data, (d) => d[1])]) .range([h - padding, padding]); const xAxis = d3.axisBottom(xScale).tickFormat(d3.format("")); //d3.format("") or ("s") produces a number without comma delimiters const yAxis = d3.axisLeft(yScale); const tooltip = d3 .select("body") .append("div") .attr("class", "tooltip") .style("visibility", "hidden"); svg .selectAll("rect") .data(gdpData.data) .enter() .append("rect") .attr("x", (d) => xScale( parseFloat(d[0].substring(0, 4)) + parseFloat(d[0].substring(5, 7)) / 12 ) ) .attr("y", (d) => yScale(d[1])) .attr("width", barW) .attr("height", (d) => h - padding - yScale(d[1])) .style("fill", "#226666") .attr("class", "bar") //for hover effect .on("mouseover", (d, i) => { tooltip .style("visibility", "visible") .html( "<h4> $" + d[1].toLocaleString() + " Billion</h4> <p style='font-size:15px;'>" + month[parseInt(d[0].substring(5, 7))-1]+ ", " + d[0].substring(0, 4) + "</p>" );//toLocaleString() adds comma delimiters to d[1] numbers $("body").mousemove(function(e) { tooltip .style("top", e.pageY - 30 + "px") .style("left", 10 + e.pageX + "px"); });//e.pageX and e.pageY provide the position of pointer }) .on("mouseout", (d, i) => { tooltip.style("visibility", "hidden"); }); svg .append("g") .attr("transform", "translate(0, " + (h - padding) + ")") .style("font-size", "15px") .call(xAxis); svg .append("g") .attr("transform", "translate(" + (padding + 0) + ", 0)") .style("font-size", "15px") .call(yAxis); svg .append("text") .attr("x", w / 2) .attr("text-anchor", "middle") .attr("transform", "translate(0, " + (h - padding / 4) + ")") .style("font-size", "20px") .text("Year"); svg .append("text") .attr("x", -padding - 1) .attr("y", padding + 20) .attr("text-anchor", "end") .attr("transform", "translate(" + 0 + ", 0)rotate(-90)") .style("font-size", "20px") .text("Units: Billions of Dollars"); }
);
Data Visualization - US GDP - Script Codes
Data Visualization - US GDP - Script Codes
Home Page Home
Developer Sam Koshy
Username codinger
Uploaded January 14, 2023
Rating 3
Size 3,131 Kb
Views 2,024
Do you need developer help for Data Visualization - US GDP?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Sam Koshy (codinger) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!