The StrangeWill answer was great as a basis, but I need vertical lines with data located on the histogram, so I changed it like this:
First, I added the property to an array of parameters (adding another dataset is probably cleaner, but I would have to tell ChartJS to ignore it), where each parameter is an index of the data value that I want to allocate, something like this:
ChartDefaults.verticalOverlayAtBar = [itemOne, itemTwo] ... var HistoryChart = new Chart(ctx).BarOverlay(ChartData, ChartDefaults);
Then I modified the StrangeWill code, expanding it to iterate over the array of elements on top and draw vertical lines on the specified strip.
Chart.types.Bar.extend({ name: 'BarOverlay', draw: function (ease) { // First draw the main chart Chart.types.Bar.prototype.draw.apply(this); var ctx = this.chart.ctx; var barWidth = this.scale.calculateBarWidth(this.datasets.length); for (var i = 0; i < this.options.verticalOverlayAtBar.length; ++i) { var overlayBar = this.options.verticalOverlayAtBar[i]; // I'm hard-coding this to only work with the first dataset, and using a Y value that I know is maximum var x = this.scale.calculateBarX(this.datasets.length, 0, overlayBar); var y = this.scale.calculateY(2000); var bar_base = this.scale.endPoint ctx.beginPath(); ctx.lineWidth = 2; ctx.strokeStyle = 'rgba(255, 0, 0, 1.0)'; ctx.moveTo(x, bar_base); ctx.lineTo(x, y); ctx.stroke(); } ctx.closePath(); } });
This is not ideal, since I am not familiar with the internal ChartJs code, in particular my lines were from 2 bars, although my suspicion is that the error in the fencepost in the data array, and not in the calculation of the chart. However, I hope this is a useful step for someone else.
source share