Chart.js line chart with horizontal line - Javascript Chart.js

Javascript examples for Chart.js:Line Chart

Description

Chart.js line chart with horizontal line

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://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.js"></script> 
      <script type="text/javascript">
    window.onload=function(){/*w w  w.  jav a 2s . co  m*/
 var randomScalingFactor = function() {
   return Math.round(Math.random() * 100 * (Math.random() > 0.5 ? -1 : 1));
 };
 var randomColorFactor = function() {
   return Math.round(Math.random() * 255);
 };
 var randomColor = function(opacity) {
   return 'rgba(' + randomColorFactor() + ',' + randomColorFactor() + ',' + randomColorFactor() + ',' + (opacity || '.3') + ')';
 };
 var config = {
   type: 'line',
   data: {
     labels: ["January", "February", "March", "April", "May", "June", "July"],
     datasets: [{
       label: "My First dataset",
       data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
       fill: false,
       borderDash: [5, 5],
     }, {
       label: "My Second dataset",
       data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
       fill: false,
       borderDash: [5, 5],
     }, {
       label: "My Third dataset - No bezier",
       data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()],
       lineTension: 0,
       fill: false,
     }, {
       label: "My Fourth dataset",
       data: [100, 100, 100, 100, 100, 100, 100],
       fill: false,
     }]
   },
   options: {
     responsive: true,
     legend: {
       position: 'bottom',
     },
     hover: {
       mode: 'label'
     },
     scales: {
       xAxes: [{
         display: true,
         scaleLabel: {
           display: true,
           labelString: 'Month'
         }
       }],
       yAxes: [{
         display: true,
         scaleLabel: {
           display: true,
           labelString: 'Value'
         }
       }]
     }
   }
 };
 $.each(config.data.datasets, function(i, dataset) {
   var background = randomColor(0.5);
   dataset.borderColor = background;
   dataset.backgroundColor = background;
   dataset.pointBorderColor = background;
   dataset.pointBackgroundColor = background;
   dataset.pointBorderWidth = 1;
 });
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx, config);
    }

      </script> 
   </head> 
   <body> 
      <div style="width:75%;"> 
         <canvas id="canvas"></canvas> 
      </div>  
   </body>
</html>

Related Tutorials