Accepted answer

You need to change a lot of things in your code.


  • change value to population in the array
  • scales are used to convert values to proportional pixel values
  • height is the vertical size of an element. you should use yScale(
  • width is the horizontal size of an element. you should use xScale(d.population)
  • y is the vertical position of an element. you should use yScale.bandwidth()
  • x is the vertical position of an element. you should use 0
  • use selectAll("rect") on a new appended g or the svg element not the same g element that has the axises on it
  • add fill attribute so that your rects have color

You have the population field labelled value but you're calling population through out the code to use it. So replace value with population in your data objects.

Next you need to change the way you're setting up the rects. use selectAll on the svg element directly or append another g to the svg element and add the rects on that element. Right now your code attempts to add the rects to the same g element as the x axis.

Make sure you are setting the attributes of the rects correctly. Height is the size in pixels of the rect element not the position. y is the position of the rects vertically from the top downwards. this means the height attribute should use yScale( and width should use xScale(d.population) because they are the width and length of the rectangles, or rect elements. x and y are the coordinate positions of the element from the top left corner of the svg element. Use them to determine the starting position of the top left pixel of your rects. You should have y as yScale.bandwidth() and x as 0.

Related Query