i am working on the d3 sections first challenge and having trouble with the y scaling. i think it has something to do with the scaling, the padding, and i also think how that affects the height of the ‘rect’. Im not sure what the height is doing for the ‘rect’ shapes so im having trouble understanding if its affecting this or not.
i went through the course a third time to try and figure out what i am doing but still stumped.
if anyone can take a peek below and let me know what you think.
If your question is about positioning the bars vertically, there’s a lot to consider. First, remember that D3 measures from an origin of (0, 0) in the top-left corner, so the y-axis is inverted compared to the normal Cartesian axis. The D3 y coordinate is for the top of the bar and the D3 height attribute is for the height (down) of the bar from the y coordinate.
So to line a sequence of bars up vertically on your x-axis (which looks like you are using padding for its vertical location), you need to get the height of the bar (use a scale function similar to what you have) and then calculate the y-coordinate you would need for the top of the bar to have so that its bottom (height units down) is on the x-axis (padding).
If you follow this description, then for small GDPs you should have small bars, so your scaling function will need to be linear and not inversely linear like now.
i was able to implement your suggestions, i am working on the axis, but the y-axis keeps coming up reverse the values. i can make the y-axis correct but then it inverses the graph.
I dont know if it is the scale, or something i am doing in the y-axis ‘g’ attributes. or even if it goes back to the ‘rect’ height again
Not only are different scales normal, they are often different types of scales. For instance, you could use a linear scale for something like the GDP in dollars and a time scale for the quarter in which the GDP was produced.
im trying to figure out tool tips. I am grabbing the ‘boddy’ and adding a div for the tool tip and then using mouseover to write text but i cant get anything to appear.
I am getting so close. i need this tooltip and then my data is not inline with the scales. It appears in line but im assuming that i need to somehow join them so that the program knows they are exactly inline with eachother.
You need to create a dummy tooltip div before you modify it in the mouse event functions. The call signature for the on('event', ...) functions is (event, datum) in D3v7; you’ll get weird results otherwise. It’s also probably best to leave the .append('title') commented because it may grab your mouse event before your code does, and therefore, not trigger your tooltip.
This is the problem. You’re already looping over the data (implicitly) with the select/enter/data for the rects. You just have to get the data-date attribute correct.
The call signature for event functions for on() in D3v7 is (event, datum) => {...} so whatever you call that second argument, it’s actually the datum for the current rect. Right now you’re setting the data-date to the list of all the processed years. You just need to set it to the same thing you set the data-date attribute to for the rect and the test will pass.
I think that your code may be confusing you right now since you’re using the accessor function signature (which is (datum, index) => {...}) for the on() event function.
Thank you so much. I wasnt understanding what you meant about the different syntax of the functions but i got it now.
for
attr('data-date', ()=>{return d[0]}
i figured out that this would pull from the ‘rect’. I was inserting a parameter ‘d’ that was making the ‘d[0]’ pull from its own instance instead of every different ‘rect’