I somehow passed all the tests so far by looking at the code written by FCC .
However, I’m having trouble passing the final test, that is
My tooltip should have a “data-date” property that corresponds to the “data-date” of the active area.
Here’s my code so far,(Please note that, I’ve copied only the relevant code below)
Link to this project, https://codepen.io/salmanthasleem/pen/eYWzLbb?editors=0111
const arr = data.data;
const years = arr.map((elem) => new Date(elem[0]));
const yearsString = arr.map((elem) =>elem[0].substring(0,4));
var gdps = arr.map((elem) => elem[1]);
var tooltip = d3.select("#chart").append("div").attr("id", "tooltip").style('opacity', 0);
svg
.selectAll("rect")
.data(arr)
.enter()
.append("rect")
.attr("x", (d, i) => xScale(years[i]))
.attr("y", (d, i) => yScale(gdps[i]))
.attr("width", w / data.data.length)
.attr("height", (d, i) => h - padding - yScale(gdps[i]))
.attr("class", "bar")
.attr("data-date", (d, i) => d[0])
.attr("data-gdp", (d, i) => d[1])
.attr("fill", "navy")
.on('mouseover', function (d,i) {
tooltip.style('opacity', 1);
tooltip
.attr("data-date",()=>d[0])
.html(()=>
d[0] +
'<br>' +
'$' +
d[1] +
' Billion'
)
.style('left',()=> i * barsize + 30 + 'px')
.style('top', h - 100 + 'px')
.style('transform', 'translateX(60px)')
})
.on('mouseout', function () {
tooltip.style('opacity', 0);
});
CSS for #tooltip
#tooltip {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
text-align: center;
width: 150px;
height: 50px;
padding: 2px;
font: 12px;
background: lightsteelblue;
box-shadow: 1px 1px 10px;
border-radius: 2px;
pointer-events: none;
}
I hope I get this fixed anytime soon, been at it since yesterday and it’s really frustrating to get stuck at something like this.
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36
Challenge: Visualize Data with a Bar Chart
Link to the challenge: