Need help lining up x axis ticks with bars in D3.js bar chart

半世苍凉 提交于 2019-12-23 19:51:30

问题


I have a working linear bar chart using D3.js, which also has time-based x axis. The bars are bound to a count attribute, while the axis is bound to a date attribute. The ticks in the axis are not lining up with the bars. Any idea how to line them both up?

Here's the jsFiddle: http://jsfiddle.net/MmEjF/


回答1:


This is happening because the chart has 14 bars, but 15 tick marks.

The first tick mark should be the month prior to the start of your data set.

You can fix this by pulling forward the start of xTimeDomain by one month:

xTimeDomain[0] = new Date(new Date(xTimeDomain[0]).setMonth(xTimeDomain[0].getMonth()-1));


来源:https://stackoverflow.com/questions/11814399/need-help-lining-up-x-axis-ticks-with-bars-in-d3-js-bar-chart

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