D3 - using strings for axis ticks

后端 未结 2 1265
滥情空心
滥情空心 2020-12-30 02:04

I want to crate a bar chart using strings as the labels for the ticks on the x-axis (e.g., Year 1, Year 2, etc instead of 0,1,2, etc).

I started by using the numeric

2条回答
  •  北海茫月
    2020-12-30 02:28

    Update

    Just sort the svg.pointsNames before you apply them as tickValues. Make sure you sort them in exactly the same way that you sort your data. This way, a one-one mapping is always maintained between your labels and tick values.

    Also if I may, check out the tickFormat` function here. This seems a better option to me.

    //Tick format example
    chart,xAxis.tickFormat(function(d, i){
        return "Year" + d //"Year1 Year2, etc depending on the tick value - 0,1,2,3,4"
    })
    

提交回复
热议问题