2014-10-29 165 views
5

我使用NVD3显示此线图:http://jsbin.com/xodaxafiti/2/edit?js,outputNVD3折线图X轴蜱缺少

但好像NVD3自动隐藏于x轴的一些tickLabels,但只有靠近边缘的蜱,即2- 3Oct和27-28Oct(除了第一个和最后一个勾号)。我知道这是一个自动缩小,因为当我增加图表的宽度时,滴答开始显示。但是我发现这种减少行为很奇怪,而lineChart没有像multiBarChart这样的reduceXTicks选项。

我希望能够控制减少自己的行为像this

var chart = nv.models.lineChart()  
    .useInteractiveGuideline(true) 
    .margin({left: 80,top: 20,bottom: 120,right: 20}); 

chart.xAxis.ticks(function() { 
    return data[0].map(chart.x()).filter(function(d,i) { 
     i % Math.ceil(data[0].values.length/(availableWidth/100)) === 0; 
    }) 
}) 

但没有奏效。任何人有任何想法如何控制这个?

missing tickLabels

+0

使用'.tickValues()'而不是'.ticks()'。 – 2014-10-29 09:53:42

+0

我已经尝试过使用'.tickFormat()',但它给了我上面缺少tickLabels的过滤器。另外,使用这个技巧时,隐藏的标签甚至不会显示在工具提示中。我相信'tickValues()'会有相同的结果。 – yonasstephen 2014-10-29 13:24:32

+0

嗯,我会使用x轴的时间尺度 - 这应该可以让您更好地控制所显示的内容。 [这个问题](http://stackoverflow.com/questions/14058876/how-do-i-display-dates-on-the-x-axis-for-nvd3-d3-js)应该是有帮助的。 – 2014-10-29 13:41:15

回答

13

还原行为有效,因为showMaxMin设置为默认值为TRUE。添加.showMaxMin(false)解决了这个问题:

chart.xAxis.axisLabel("XAxisLabel") 
    .showMaxMin(false)  
    .tickValues(tickvalues)   
    .tickFormat(function (d) { 
     return tickformat[d]; 
     }) 
; 

enter image description here

+0

谢谢,男人! – yonasstephen 2014-10-30 12:41:04

+0

minMax救了我! – bravokeyl 2015-11-17 13:54:37

+0

注意你没有超出范围的tickvalues,或者他们会出现在yaxis的左边。你也可以使用'xDomain'来设置xAxis的范围 – 2016-12-22 08:15:25

0

如果你想有两个靠近边界的边界蜱和蜱(最大最小)值,你可以修改源。

在nv.models.axis(),有当showMaxMin是真正的底/顶部取向给出一个缓冲:

if (showMaxMin && (axis.orient() === 'top' || axis.orient() === 'bottom')) { 
      var maxMinRange = []; 
      wrap.selectAll('g.nv-axisMaxMin') 
       .each(function(d,i) { 
        try { 
         if (i) // i== 1, max position 
          maxMinRange.push(scale(d) - this.getBoundingClientRect().width - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case) 
         else // i==0, min position 
          maxMinRange.push(scale(d) + this.getBoundingClientRect().width + 4) 
        }catch (err) { 
         if (i) // i== 1, max position 
          maxMinRange.push(scale(d) - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case) 
         else // i==0, min position 
          maxMinRange.push(scale(d) + 4); 
        } 
       }); 
      // the g's wrapping each tick 
      g.selectAll('g').each(function(d, i) { 
       if (scale(d) < maxMinRange[0] || scale(d) > maxMinRange[1]) { 
        if (d > 1e-10 || d < -1e-10) // accounts for minor floating point errors... though could be problematic if the scale is EXTREMELY SMALL 
         d3.select(this).remove(); 
        else 
         d3.select(this).select('text').remove(); // Don't remove the ZERO line!! 
       } 
      }); 
} 

我只是删除这些缓冲区:

try { 
     if (i) // i== 1, max position 
      maxMinRange.push(scale(d)); 
     else // i==0, min position 
      maxMinRange.push(scale(d)) 
}catch (err) { 
     if (i) // i== 1, max position 
       maxMinRange.push(scale(d)); 
     else // i==0, min position 
       maxMinRange.push(scale(d)); 
}