d3 tick marks on integers only

后端 未结 5 1506
旧巷少年郎
旧巷少年郎 2020-12-03 10:04

I have a d3 bar chart whose values range from 0-3. I would like the y-axis to only show integer values, which I can do by

var yAxis = d3.svg.axis().scale(y).         


        
5条回答
  •  臣服心动
    2020-12-03 10:22

    Used this TIP to make this work on dynamic charts (1 or more charts on page)

    That did the trick. I changed to be Math.min(maxHeight+1,yAxis.ticks()) so if the graph height is less than the number of ticks, it reduces the number of ticks. – Jeff Storey Nov 28 '12 at 2:47

      // It is either 10 or Maximum Chart height + 1 
    
            var graphValues = selection.data()[0].map(function(obj) {
                return obj['count'];
            }).compact();  
    
            Array.prototype.maxValArray = function() {
              return Math.max.apply(null, this);
            };
    
            var maxValue = graphValues.maxValArray();
    
            yAxis.ticks(Math.min(maxValue + 1, 10)) 
    

提交回复
热议问题