Hi all,
I am creating a bar chart following the course on FreeCodeCamp and for some reason my bar chart is not displaying on the Web. Although it was appearing with an x-axis issue. When I tried to fix it both are not displaying anymore. I need help to understand what’s wrong with my code? If you look at line chart I created , it works fine.
*Please, be aware that there is a solution provided to pass this project , but I do not want to copy, then it is not in my style. *
Here is the code:
<body>
<div class="canvas">
<!-- Bar -->
<div>
<div>GDP by Year in Bar chart</div>
<svg id="bar_title" width="500" height="400" style="background-color: darkkhaki;">
<g id="title" style="transform:translate(50px, 50px)"></g>
<g id="x-axis"></g>
<g id="y-axis"></g>
</svg>
</div>
<div>
<div>GDP by Year in Line chart</div>
<!-- Linear -->
<svg id="line_title" width="500" height="400" style="background-color: darkkhaki;">
<g id="linear" style="transform:translate(50px, 50px)"></g>
</svg>
</div>
</div>
<!-- D3 -->
<script type="javascript">
// Loading data from URL
d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json").then(ShowData);
const bar_chart = d3.select("#title");
const bar_title = d3.select("#bar_title");
function BarDraw(data) {
let w = 400;
let h = 300;
data = data.data.map(fn => fn);
data = data.map(dn => ({
Date: new Date(dn.DATE),
GDP: +dn.VALUE
}));
let max = d3.max(data, fn => +fn.GDP);
let iter = data.map(fn => fn.Date);
let x = d3.scaleBand().domain(iter).range([0, w]).padding(1.3);
// bar_chart.append("g").attr("id", "x-axis").attr("transform", `translate(0,${h})`).call(d3.axisBottom(x).ticks(10).tickFormat(d3.timeFormat("%Y"))).selectAll("text").attr("transform", "translate(-10,0)rotate(-45)").style("text-anchor", "end");
let y = d3.scaleLinear().domain([0, max]).range([h, 0]);
// bar_chart.append("g").attr("id", "y-axis").call(d3.axisLeft(y))
bar_chart.selectAll("rect")
.data(data).enter()
.append("rect")
.attr("class", "bar")
.attr("fill", "navy")
.attr('stroke', "green")
.attr("x", fn => x(fn.Date))
.attr("y", fn => y(+fn.GDP))
.attr("width", x.bandwidth())
.attr("height", fn => y(+fn.GDP))
let xAxis = d3.axisBottom(x).tickFormat(d3.timeFormat("%Y"))
d3.select("#x-axis").attr("transform", `translate(0, ${h})`).call(xAxis)
let yAxis = d3.axisLeft(y)
d3.select("#y-axis").call(yAxis)
};
// Line chart the same trouble
const line_chart = d3.select("#linear");
const title = d3.select("linear_title");
function LineDraw(data) {
let width = 400;
let height = 300;
data = data.data.map(fn => fn);
let maxVal = d3.max(data, fn => +fn[1]);
// Alternative
data = data.map(dn => ({
Date: new Date(dn[0]),
GDP: +dn[1]
}))
let x = d3.scaleTime().domain(d3.extent(data, fn => fn.Date)).range([0, width]);
line_chart.append("g").attr("transform", `translate(0, ${height})`).call(d3.axisBottom(x).tickFormat(d3.timeFormat("%Y")));
let y = d3.scaleLinear().domain([0, maxVal]).range([height, 0])
line_chart.append("g").call(d3.axisLeft(y));
line_chart.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "green")
.attr("d", d3.line()
.x(d => x(d.Date))
.y(d => y(+d.GDP)))
}
function ShowData(data) {
LineDraw(data);
BarDraw(data);
}
</script>
</body>
I appreciate your contribution.
Kind reagrds,
Kanatbek