User Story #10: The
data-date attribute and its corresponding bar element should align with the corresponding value on the x-axis.
My code, that is actually working has something like:
.attr('x', (d,i) => x(d))
.attr('y', d => y(d))
.attr('height', d => h - y(d))
I know this works. The data-date gets its values from the corresponding record at d. The bar are displayed at d, so how can’t they be “aligned”?
What does “aligned” means in this exercise?
Everything else is already working. Thanks.
Just because this code is correct, however, doesn’t mean the test will pass as the error could be anywhere else. The most likely causes are problems in the definition of your axis and scale, problems in the class and id attributes of the bars or other elements, problems with the format of the data used for the attributes or axes, or missing or extra elements.
You’ll need to post a link to your codepen (or similar) of the project (preferable) or post all the code.
This is the codepen: https://codepen.io/luisferfranco/pen/eYeBZWg?editors=1111
I know I can work better my x-axis, and that the tooltip is just a div that hides and unhides, but as they are, they cover the requirements. I may do them better, but right now my problem is with that “alignment”
The only “important” difference I see against the official answer is that they use a
scaleLinear() and I use a
scaleBand(). In fact,
scaleBand() is a better answer since its the “official” way to construct bar charts, since you don’t have to calculate the X’s coordinates or the bar width since D3 makes the calcs for you.
The scale type doesn’t matter as long as the tests pass. The tests expect the left side of the bars to start at the correct position relative to the axis, so the GDP bar for 1985-07-01 needs its left side above the axis position for 1985-07-01. D3’s band scale centers the bands on ticks, since it’s supposed to be used for discrete or categorical data and that would be appropriate. But the tests are treating time as a continuous variable and expecting the bar over the whole time period (quarter) not centered on the start.
You can see this in the chart if you zoom in far enough; the bars overlap. The first bar also overlaps the y-axis. Since the code otherwise appears correct, I believe this is the problem. You should be able to verify this is the problem by either translating the bars to the right or by using a linear or time scale for the x-axis.
Yeah, that’s how I understood the instructions, and it’s how it works. The first data is in the first bar, second in the second and so on…
I realized that the scale doesn’t matter, I also tried scaleLinear.
I haven’t tried converting the data to date… I’m pretty lazy, thought a scaleLinear() would suffice… but that’s the only thing left to try… Thanks!
That may be necessary, but I would do it last. If the bars are overlapping each other and centered on the time position on the axis and not starting (with their left edge) on the time position, the tests will always fail.
Well… overlapping is never a problem if you use
scaleBand() instead of
scaleLinear() (as I did)
It can be a problem if you set your bandwidth wider than the ratio of the axis length divided by the number of bands (shouldn’t be a problem as long as
bandwidth() is calculating like you think it is). Regardless, I just rechecked you posted codepen above and the bars still overlap. I zoomed to about 400% and looked at the first bar which overlaps the y-axis and other individual bars (look for neighbors with different heights) overlap but it’s hard to see unless you look at the tops and you can see the colors zig-zag.
All you can do is try something. Shrink the bar width, change scales, or muck with the dates, whatever it takes to get the tests passing.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.