Bar chart width in chartjs - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Bar chart width in chartjs

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>chart.js 2.0 bar</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){//from ww w  . j  a  v  a  2s  . com
var canvas = document.getElementById('myChart');
var data = {
    labels: ["January","January","January","January","January"],
    datasets: [
        {
            label: "My First dataset",
            backgroundColor: "rgba(255,99,132,0.2)",
            borderColor: "rgba(255,99,132,1)",
            borderWidth: 2,
            hoverBackgroundColor: "rgba(255,99,132,0.4)",
            hoverBorderColor: "rgba(255,99,132,1)",
            data: [65,65,65,65,65],
        }
    ]
};
var option = {
 maintainAspectRatio: true,
    responsive: false,
   scales: {
     yAxes:[{
          stacked:true,
        gridLines: {
           display:true,
          color:"rgba(255,99,132,0.2)"
        }
    }],
    xAxes:[{
     barThickness : 30,
          gridLines: {
           display:false
        }
    }]
  }
 };
var width = (data.labels.length + 5) * 40;
document.getElementById('myChart').setAttribute("width",(width))
document.getElementById('myChart').setAttribute("height",500)
console.log(data.labels.length);
var myBarChart = Chart.Bar(canvas,{
   data:data,
  options:option
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart"></canvas>  
   </body>
</html>

Related Tutorials