d3 axis labeling

后端 未结 6 1206
天涯浪人
天涯浪人 2020-12-12 15:55

How do I add text labels to axes in d3?

For instance, I have a simple line graph with an x and y axis.

On my x-axis, I have ticks from 1 to 10. I want the wo

6条回答
  •  青春惊慌失措
    2020-12-12 16:27

    In the new D3js version (version 3 onwards), when you create a chart axis via d3.svg.axis() function you have access to two methods called tickValues and tickFormat which are built-in inside the function so that you can specifies which values you need the ticks for and in what format you want the text to appear:

    var formatAxis = d3.format("  0");
    var axis = d3.svg.axis()
            .scale(xScale)
            .tickFormat(formatAxis)
            .ticks(3)
            .tickValues([100, 200, 300]) //specify an array here for values
            .orient("bottom");
    

提交回复
热议问题