Align two bar chart Chartjs side by side - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Align two bar chart Chartjs side by side

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/Chart.js/1.0.2/Chart.js"></script> 
      <style id="compiled-css" type="text/css">

#chart_a, #chart_b {
   width: 320px;
   height: 350px;
}
.label {/*from  w  ww. ja  v  a 2  s  .  co  m*/
   text-align: center;
   width: 600px;
   font-size: 20px;
   font-weight: bold;
   margin: 20px;
}
.chart_container {
   float: left;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var data, ctx, mychart;
data = {
    labels: ["D", "Y", "A"],
    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: [50, 88, 15]
    }]
};
ctx = document.getElementById("chart_b").getContext("2d");
mychart2 = new Chart(ctx).Bar(data, {
    animation: false
});
Chart.types.Bar.extend({
    name: "BarAlt",
    draw: function () {
        this.scale.endPoint = this.options.endPoint;
        Chart.types.Bar.prototype.draw.apply(this, arguments);
    }
});
data = {
    labels: ["Iantojones", "Jackharkness", "Owenharper"],
    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: [50, 88, 15]
    }]
};
ctx = document.getElementById("chart_a").getContext("2d");
mychart1 = new Chart(ctx).BarAlt(data, {
    endPoint: mychart2.scale.endPoint,
    animation: false
});
    }

      </script> 
   </head> 
   <body> 
      <div id="a"></div> 
      <div id="aligned"> 
         <div class="label">
            Aligned
         </div> 
         <div class="chart_container"> 
            <canvas id="chart_a"></canvas> 
         </div> 
         <div class="chart_container"> 
            <canvas id="chart_b"></canvas> 
         </div> 
      </div>  
   </body>
</html>

Related Tutorials