D3

D3 Axes

To render axes in D3, we need to first generate them, and then plot them.
There are four axis generators called axis components:

Axis components control the orientation of the axis, but not the location. All axes are rendered at the origin.

There are two ways to pass a scale to an axis:

To plot the axis, call it on a selection, then some SVG elements presenting the axis will be created as a child of that selection. Also, there are two ways:

Since all axes are rendered at the origin, we need to translate them. For example:

const yAxis = d3.axisLeft().scale(yScale);
const svg = d3.select("svg")

svg.append("g")
   .attr("class", "yAxis")
   .attr("transform",
     `translate(${margin.left}, ${margin.top})`)
   .call(yAxis);

Ticks

To label the ticks, use labels when creating the D3 Scale. For example, for data

const bardata = [{month: "Jan", value: 300},
                 {month: "Feb", value: 100},
                 {month: "Mar", value: 150},
                 {month: "Apr", value: 220},
                 {month: "May", value: 70 },
                 {month: "Jun", value: 270}]

we can create the x ordinal scale using month,

const xScale = d3.scaleBand()
  .domain(bardata.map(d => d.month))
  .range([0, innerWidth])
  .paddingInner(.1);

and then create y continuous scale using value. Then the axes will automatically comply with the scale.

Or, we can add the tick values explicitly

const yAxis = d3.axisLeft(xScale).tickValues([1, 2, 3, 5, 8, 13, 21]);
Creative Commons License by zcysxy