javascript-d3How do I set the left y-axis in d3.js?
Setting the left y-axis in d3.js can be done by using the .axisLeft() method. This method takes in an optional argument that specifies the scale for the axis. The following example code creates a linear scale and uses it to set the left y-axis:
//Create linear scale
var yScale = d3.scaleLinear()
.domain([0, 100])
.range([0, 100]);
//Set left y-axis
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale));
This code will create a left y-axis with a linear scale, ranging from 0 to 100.
The parts of the code are as follows:
d3.scaleLinear(): creates a linear scale.domain([0, 100]): sets the lower and upper bounds of the scale.range([0, 100]): sets the range of the scalesvg.append("g"): appends a group element to the SVG.attr("class", "y axis"): assigns the group element the class "y axis".call(d3.axisLeft(yScale)): calls the axisLeft method and passes in the linear scale
Helpful links
More of Javascript D3
- How do I use d3.js to zoom to a selected area?
- How do I use D3.js to zoom on the x-axis?
- How can I use d3.js to create a zoom scale?
- How do I create a zoomable line chart using d3.js?
- How do I use the z-index property with d3.js?
- How can I use d3.js to make an XMLHttpRequest?
- How do I decide between using d3.js and plotly for creating interactive charts?
- How can I create a knowledge graph using d3.js?
- How do I create a US map using D3.js?
- How do I add a tooltip to my d3.js visualization?
See more codes...