Accepted answer

Updated code:

Question 1: The width of the bars is determined by the width of the svg, as well as your given values for the scaleBand functions paddingInner, paddingOuter, and a few others.

Take a look at the image describing these parameters. The point is to not hardcode the width of the bars like you did with .attr("width", 20);, and instead manipulate the scaleBand to find your desired look.

Question 2:

You did this correctly with the code

  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")

But then on update, you remove the axis and didn't apply the transform again. Instead of deleting the axis and recreating it, simply update the axis scale with the new domain, and do".x.axis").call(xAxis). This falls in line with the d3 way of thinking in a data driven fashion. Change your data, and let D3 handle the DOM.

Question 3:

The function name is forEach, not foreach. Also, you say you want to make sure the value is a string, but putting the + operator before a variable converts it to a number, not a string.

Related Query

More Query from same tag