【问题标题】:How to force the nvd3 chart timespan to stretch the width of the chart?如何强制 nvd3 图表时间跨度拉伸图表的宽度?
【发布时间】:2015-12-28 15:53:03
【问题描述】:

http://plnkr.co/edit/ymrqIOGTBqF6TVMTvC4T?p=preview

我在上面的 plnkr 中有最新的 d3 和 nvd3 库。当您第一次查看图表时,您会注意到所有时间跨度刻度 09:0008:3008:00 等都在左侧 xAxis 上相互挤压/重叠。

现在时间跨度将无法正确拉伸图表的长度,直到用户单击图表下方的时间范围查找器或调整窗口大小。

目前图表在首次加载时如下所示:

什么时候应该是这样的:

知道我哪里出错了吗?我在这里NVD3 Set Expanded as default 找到了这个问题,但是答案对我不起作用。


我的 nv.addGraph 代码

var data =[{
    "key" : "Price",
    "color" : "#4C73FF",
    "values" : [ [ 1443621600000 , 71.89],
                 [ 1443619800000 , 75.51],
                 [ 1443618000000 , 68.49],
                 [ 1443616200000 , 62.72],
                 [ 1443612600000 , 70.39],
                 [ 1443610800000 , 59.77]]
}];

nv.addGraph(function() {
      var chart = nv.models.linePlusBarChart()
          .margin({top: 20, right: 40, bottom: 50, left: 40})
          .x(function(d,i) { return i })
          .y(function(d) { return d[1] })
          .color(d3.scale.category10().range());

      chart.xAxis.tickFormat(function(d) {
          var dx = data[0].values[d] && data[0].values[d][0] || 0;
          // return time in hours:
          return d3.time.format('%I:%M')(new Date(dx))
      });

      chart.y1Axis
          .tickFormat(d3.format(',f'));

      chart.y2Axis
          .tickFormat(function(d) { return '$' + d3.format(',f')(d) });

      chart.bars.forceY([0]);
      chart.lines.interactive(false);
      chart.height(300);
      chart.noData("There is no Data to display at the moment.");

      // Remove legend:
      chart.showLegend(false);

      d3.select('#chart svg')
          .datum(data)
          .transition().duration(500)
          .call(chart);

      nv.utils.windowResize(chart.update);

      return chart;
  });

【问题讨论】:

    标签: javascript angularjs d3.js charts nvd3.js


    【解决方案1】:

    刚刚想通了!

    d3.select('#chart svg')
        .datum(res)
        .transition().duration(500)
        .call(chart);
    
    chart.update();  // <- Needed to add this
    
    // nv.utils.windowResize(chart.update);
    

    下面这行完全没用: nv.utils.windowResize(chart.update);

    我将 chart.update 移出,显然这是触发时间跨度延长所需要的。

    【讨论】:

    • nv.utils.windowResize(chart.update); 行并非完全没用,如果您需要图表响应窗口大小调整,您将需要它。
    • 啊,是的@shabeer90 我想我只是很沮丧,呵呵......还构建了一个移动应用程序,所以它不需要windowResize 但是当我回到我们的桌面仪表板并使用这张图表,我会保留这条线。
    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2015-11-26
    • 1970-01-01
    • 2020-04-26
    • 2015-05-04
    • 2020-07-05
    相关资源
    最近更新 更多