Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase Chart.js Scale Padding/Margin

How do I increase the separation between the border and the bar?

Example image:
http://imgur.com/a/AamLT

image

Current Code:

var canvas = document.getElementById('myChart');
var data = {
    labels: ["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, 59, 30, 81, 56, 55, 40],
        }
    ]
};
var option = {

};


var myBarChart = Chart.Bar(canvas,{
	data:data,
  options:option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="500"></canvas>

JSFiddle:
https://jsfiddle.net/palaceslittle/mfvmoy64/217/

like image 919
nawelittle Avatar asked Jun 05 '17 17:06

nawelittle


People also ask

How do I add padding to a chart?

Padding values in Chart options can be supplied in couple of different formats. If this value is a number, it is applied to all sides (left, top, right, bottom). For example, defining a 20px padding to all sides of chart: Copied! If this value is an object, the left property defines the left padding.

How do I set the margin on a chart?

Chart margin is the space between the chart border and the canvas border. You can set the chart margins on any one of the chart's four sides. To do so, the following attributes are used: Set the amount of empty space on the left of the chart using the chartLeftMargin attribute.

What is canvas padding for a chart?

For a line/area chart, canvas padding is the space between the canvas border and the position where the line/area chart begins. Specify the amount of gap using the canvasPadding attribute. Were you able to implement this?

How do I increase the width of a chart bar?

Once you update to a latest version of Chart.js (currently v2.6.0), in the bar config section, you can set a maxBarThickness. Doing this will allow you to increase the chart canvas width, without needing to worry about the bar getting larger.


Video Answer


1 Answers

Once you update to a latest version of Chart.js (currently v2.6.0), in the bar config section, you can set a maxBarThickness. Doing this will allow you to increase the chart canvas width, without needing to worry about the bar getting larger.

Your options section would look as following:

var option = {
    scales: {
        xAxes: [{
            maxBarThickness: 100
        }]
    }
};

JSFiddle: https://jsfiddle.net/dhhnssgc/

See the below documentation for more information about the bar chart configuration options:
http://www.chartjs.org/docs/latest/charts/bar.html#configuration-options

Depending on how you set up the rest of your chart, the barPercentage and barThickness options may also work for you.


If you want to move your entire chart away from the edge of the canvas, you can use the layout padding option: http://www.chartjs.org/docs/latest/configuration/layout.html

options: {
    layout: {
        padding: {
            left: 0,
            right: 50,
            top: 0,
            bottom: 0
        }
    }
}

JSFiddle: https://jsfiddle.net/dhhnssgc/2/

like image 108
Tot Zam Avatar answered Jan 05 '23 14:01

Tot Zam