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

More Query from same tag