Line ChartJS with empty and null values - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

Line ChartJS with empty and null values

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Chart Js Template</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script> 
   </head> 
   <body> 
      <canvas id="chart" width="800" height="400"></canvas> 
      <script type="text/javascript">
Chart.types.Line.extend({/*from   ww w.  ja  va  2s .  c  om*/
    name: "LineAlt",
    draw: function () {
        Chart.types.Line.prototype.draw.apply(this, arguments);
        var ctx = this.chart.ctx
        this.datasets.forEach(function (dataset) {
            ctx.strokeStyle = dataset.strokeColor
            var previousPoint = {
                value: null
            };
            dataset.points.forEach(function (point) {
                if (previousPoint.value !== null && point.value !== null) {
                    ctx.beginPath();
                    ctx.moveTo(previousPoint.x, previousPoint.y);
                    ctx.lineTo(point.x, point.y);
                    ctx.stroke();
                }
                previousPoint = point;
            })
        })
    }
});
var ctx = document.getElementById("chart").getContext("2d");
var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
        label: "My First dataset",
        fillColor: "rgba(220,220,220,0.5)",
        strokeColor: "rgba(220,220,220,0.8)",
        highlightFill: "rgba(220,220,220,0.75)",
        highlightStroke: "rgba(220,220,220,1)",
        data: [65, null, 80, 81, 56, 55, 40]
    }, {
        label: "My Second dataset",
        fillColor: "rgba(151,187,205,0.5)",
        strokeColor: "rgba(151,187,205,0.8)",
        highlightFill: "rgba(151,187,205,0.75)",
        highlightStroke: "rgba(151,187,205,1)",
        data: [28, 48, null, 19, null, 27, 90]
    }]
};
var myLineChart = new Chart(ctx).LineAlt(data, {
    datasetFill: false,
    pointDotRadius: 2,
    pointDotStrokeWidth: 3,
    datasetStrokeWidth: 0.01
});

      </script>  
   </body>
</html>

Related Tutorials