drawing an arbitrary vertical line in a line chart - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

drawing an arbitrary vertical line in a line chart

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script> 
      <script type="text/javascript" src="https://rawgit.com/nnnick/Chart.js/v1.0.2/Chart.min.js"></script> 
      <script type="text/javascript">
    $(window).load(function(){//from  w  w w  .  j  a va2s.  com
var data = {
    labels: ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"],
    datasets: [{
        data: [12, 3, 2, 1, 8, 8, 2, 2, 3, 5, 7, 1]
    }]
};
var ctx = document.getElementById("LineWithLine").getContext("2d");
Chart.types.Line.extend({
    name: "LineWithLine",
    draw: function () {
        Chart.types.Line.prototype.draw.apply(this, arguments);
        var point = this.datasets[0].points[this.options.lineAtIndex]
        var scale = this.scale
        // draw line
        this.chart.ctx.beginPath();
        this.chart.ctx.moveTo(point.x, scale.startPoint + 24);
        this.chart.ctx.strokeStyle = '#ff0000';
        this.chart.ctx.lineTo(point.x, scale.endPoint);
        this.chart.ctx.stroke();
        // write TODAY
        this.chart.ctx.textAlign = 'center';
        this.chart.ctx.fillText("TODAY", point.x, scale.startPoint + 12);
    }
});
new Chart(ctx).LineWithLine(data, {
    datasetFill : false,
    lineAtIndex: 2
});
    });

      </script> 
   </head> 
   <body> 
      <canvas id="LineWithLine" width="600" height="400"></canvas>  
   </body>
</html>

Related Tutorials