Hi all,
Thanks for having a look into this one.
I’m trying to solve first challenge in Data Visualisation module. I’m done with everything apart from the damned tooltip which just won’t display the data I want it to.
My current understanding of creating object attributes with d3 is that in, let’s say :
.attr("height", (d,i)=>d)
second variable can be a function and within that function d would be the data/value and i would be the index of the given value, taken from dataset provided in .data().
And this should work the same for everything in:
svg.selectAll('rect')
.data(scaledValues)
.enter()
.append('rect')
.attr("y", (d,i)=>{console.log(d+', '+i);return h-d})
.attr("x", (d,i)=>scaleX(realDates[i]))
.attr("height", (d,i)=>d)
.attr("width", barW)
.attr("data-date", (d,i)=>stringDates[i])
.attr("data-gdp", (d,i)=>values[i])
.attr("transform", "translate(50,0)")
.attr("class", "bar")
.on("mouseover", (d,i)=>{
console.log(d + ', ' + i);
tooltip.transition().duration(300).style("opacity", 0.9);
tooltip
.style("left", 200 + "px")
.style("top", h-100 + "px")
.html(prettyDates[i] + '<br>' + values[i] + ' Billions')
})
.on('mouseout', (d,i)=>{
tooltip.transition().duration(300).style('opacity',0)
});
In the code above, I have inserted 2 console.log() commands. One in:
.attr("y", (d,i)=>{console.log(d+', '+i);return h-d})
to see values and indexes of my dataset. Console log shows me this:
And this works just as expected.
However, console.log() from the:
.on("mouseover", (d,i)=>{
console.log(d + ', ' + i);
tooltip.transition().duration(300).style("opacity", 0.9);
shows me that d has become the [object MouseEvent] and i is the value from the dataset now.
I did try to add the 3rd variable to the second argument of .on() function hoping that it would be the index from the dataset but it didn’t work.
When I look at the example solution under:
The issue I encounter is non existent.
Here’s my own code:
I fail most of the tests at the moment, as I’m not bothered by the user stories yet
Would someone be able to explain why is this happening?
Thanks