D3 - using strings for axis ticks

后端 未结 2 1266
滥情空心
滥情空心 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条回答
  •  旧时难觅i
    2020-12-30 02:51

    Thanks for that...I used this function with an inline if-clause to handle a different x-axis series with names instead of numbers.

    The factions Array consists of all relevant names sorted by the indexes of the series who then just get matched with its corresponding index in the data.

    xAxis = d3.svg.axis().scale(xScale)
                .tickFormat(function(d) { 
    
                    if(seriesX == 'seriesValue'){ 
                        return factions[d]}
    
                    else{ 
                        return d}
                })    
             .orient("bottom");
    

提交回复
热议问题