Based on this stackoverflow answer I was trying to copy essential parts to get proper scale for timeline. I use multibarcharts for multible graphs, from few records to hundreds with X-axis having data from 1930 to today.
I've copied it like this, but I have two issues:
- Last bar is always outside of graph
bars overlap, which I can partly fix by altering
numTicks
, but isn't there a better way?<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script> <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.js"></script> <script type="text/javascript"> nv.addGraph(function() { var data = [{ "values": [ { x: new Date("1999-12-01"), y: 42.27 } , { x: new Date("2000-12-01"), y: 41.73 } , { x: new Date("2001-12-01"), y: 41.34 } , { x: new Date("2002-12-01"), y: 41.84 } , { x: new Date("2003-12-01"), y: 43.93 } , { x: new Date("2004-12-01"), y: 42.18 } , { x: new Date("2005-12-01"), y: 42.31 } , { x: new Date("2006-12-01"), y: 43.14 } , { x: new Date("2007-12-01"), y: 43.24 } , { x: new Date("2008-12-01"), y: 39.30 } , { x: new Date("2009-12-01"), y: 43.80 } , { x: new Date("2010-12-01"), y: 44.10 } , { x: new Date("2011-12-01"), y: 54.10 } , { x: new Date("2012-12-01"), y: 62.10 } , { x: new Date("2013-12-01"), y: 56.70 } , { x: new Date("2014-12-01"), y: 45 } , { x: new Date("2015-12-01"), y: 55.60 } , { x: new Date("2026-12-01"), y: 54.40 } , { x: new Date("2027-12-01"), y: 57 } ], "bar": true, "key": "Payout Ratio" }]; var chart = nv.models.multiBarChart(), container = d3.select('#payout_ratio_chart svg'), availableWidth, numTicks = data[0].values.length, xScale = d3.time.scale(); function updateAvailableWidth() { availableWidth = (chart.width() || parseInt(container.style('width')) || 960) - chart.margin().left - chart.margin().right; } updateAvailableWidth(); nv.utils.windowResize(updateAvailableWidth); xScale.rangeBands = xScale.range; xScale.rangeBand = function() { return (1 - chart.groupSpacing()) * availableWidth / numTicks; }; chart.multibar.xScale(xScale); var last_date = data[0].values[data[0].values.length-1].x; last_date.setMonth(last_date.getMonth() + 10); chart.xDomain([data[0].values[0].x, last_date]); chart.xAxis.tickFormat(function(d){ return d3.time.format('%b %Y')(new Date(d)); }); chart.yAxis.tickFormat(d3.format(',f')); chart.showControls(false); container.datum(data).transition().duration(500).call(chart); nv.utils.windowResize(chart.update); return chart; }); </script>