Creating Custom Chart Types - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Creating Custom Chart Types

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 ww  .  j  a va2 s .  c om*/
Chart.types.Line.extend({
  name: "LineAlt",
  draw: function () {
    Chart.types.Line.prototype.draw.apply(this, arguments);
    var scale = this.scale
    // draw lines
    this.chart.ctx.save();
    this.chart.ctx.strokeStyle = '#ff0000';
    this.chart.ctx.beginPath();
    this.chart.ctx.moveTo(scale.calculateX(1.5), scale.startPoint);
    this.chart.ctx.lineTo(scale.calculateX(1.5), scale.endPoint);
    this.chart.ctx.stroke();
    this.chart.ctx.beginPath();
    this.chart.ctx.moveTo(scale.calculateX(6.5), scale.startPoint);
    this.chart.ctx.lineTo(scale.calculateX(6.5), scale.endPoint);
    this.chart.ctx.stroke();
    this.chart.ctx.restore();
  }
});
var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "rgba(220,220,220,0.2)",
      strokeColor: "rgba(220,220,220,1)",
      pointColor: "rgba(220,220,220,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(220,220,220,1)",
      data: [28, 48, null, null, null, null, null, null, null]
    },
    {
      label: "My Second dataset",
      fillColor: "rgba(151,187,205,0.2)",
      strokeColor: "rgba(151,187,205,1)",
      pointColor: "rgba(151,187,205,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(151,187,205,1)",
      data: [null, null, 40, 19, 86, 27, 90, null, null]
    },
    {
      label: "My Third dataset",
      fillColor: "rgba(151,205,187,0.2)",
      strokeColor: "rgba(151,205,187,1)",
      pointColor: "rgba(151,205,187,1)",
      pointStrokeColor: "#fff",
      pointHighlightFill: "#fff",
      pointHighlightStroke: "rgba(151,205,187,1)",
      data: [null, null, null, null, null, null, null, 24, 32]
    }
  ]
};
var ctx = document.getElementById('chart').getContext('2d');
new Chart(ctx).LineAlt(data);
    });

      </script> 
   </head> 
   <body> 
      <canvas id="chart" width="600" height="300"></canvas>  
   </body>
</html>

Related Tutorials