Chart.JS spacing and padding settings - Javascript Chart.js

Javascript examples for Chart.js:Chart Configuration

Description

Chart.JS spacing and padding settings

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-1.11.0.js"></script> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script> 
      <style id="compiled-css" type="text/css">
canvas {/*from w  ww  .jav  a  2 s  . co m*/
   border: solid 2px black;
}
      </style> 
      <script type="text/javascript">
    $(window).load(function(){
Chart.types.Bar.extend({
    name: "BarAlt",
    initialize: function(data){
        data.labels.forEach(function(item, index) {
            data.labels[index] += Array(Math.max(30 - item.length, 0)).join(" ");
        })
        Chart.types.Bar.prototype.initialize.apply(this, arguments);
    },
    draw: function(){
        var xScalePaddingRight = 120
        this.scale.xScalePaddingRight = xScalePaddingRight
        Chart.types.Bar.prototype.draw.apply(this, arguments);
        this.chart.ctx.fillStyle="#FFF";
        this.chart.ctx.fillRect(this.chart.canvas.width - xScalePaddingRight, 0, xScalePaddingRight, this.chart.canvas.height);
    }
});
var barData = {
    labels: ["John W", "Ben T", "Jenny H", "Samantha D", "Anothony P"],
    datasets: [
        {
            fillColor: "rgba(153, 153, 155, 0.4)",
            highlightFill: "#7C7C7C",
            strokeColor: "#7C7C7C",
            pointColor: "#7C7C7C",
            pointStrokeColor: "#7C7C7C",
            pointHighlightFill: "#fff",
            data: [25, 94, 68, 175, 66]
        },
        {
            fillColor: "rgba(236,166,166,0.4)",
            highlightFill: "#C3090A",
            strokeColor: "#f9090a",
            pointColor: "#fff",
            pointStrokeColor: "#C3090A",
            pointHighlightFill: "#f9090a",
            data: [134, 112, 92, 160, 52]
        }
    ]
};
var ctx = $("#bar").get(0).getContext("2d");
var myChart = new Chart(ctx).BarAlt(barData);
    });

      </script> 
   </head> 
   <body> 
      <canvas id="bar" width="390" height="225"></canvas>  
   </body>
</html>

Related Tutorials