x-axis dates don't align with y-axis data in nvd3

六月ゝ 毕业季﹏ 提交于 2019-12-05 17:55:58

A better suggestion here as d3 does not scale the x-axis represented by dates well if it is not declared a timescale.

  chart.xAxis
      .tickFormat(function(d) {
          return d3.time.format('%d-%m-%y')(new Date(d))
      });

  chart.xScale(d3.time.scale()); //fixes misalignment of timescale with line graph

Ok, got it! What I do is I create a sorted list of timestamps which are to be used as data on the x-axis and round them to the nearest day. Then I force NVD3 to use this data as intervals instead of the auto-generated intervals by doing:

 chart.xAxis
     .tickValues({{x_data}})

where x_data is the list. And voila ! ...

You can explicitly control how the ticks are generated. To have ticks 24 hours apart, the code would be something like

chart.xAxis.ticks(d3.time.day, 1);

Edit

It looks like NVD3 doesn't actually use the D3 axis component to generate the labels, hence all of this has no effect. You would have to modify the source of NVD3 to achieve what you want.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!