I want padding at the beginning of the X-axis as shown in the image below.
But padding doesn’t work in my code.
What’s the matter?
My Chart Screen
Chart.js Version 2.9.4
here is code
html
JavaScript
x
3
1
<div class="chart-container" style="position: relative; height:40vh; width:80vw">
2
</div>
3
javascript
JavaScript
1
76
76
1
$('#myChart').remove();
2
$('.chart-container').append('<canvas id="myChart" style="height:400vh; width:500vw;"></canvas>');
3
4
5
6
let options = {
7
spanGaps: true,
8
responsive: true,
9
maintainAspectRatio : false,
10
scales: {
11
xAxes:[{
12
afterFit:(axis)=>{
13
// console.log(axis)
14
axis.paddingLeft = 25;
15
axis.paddingRight = 15;
16
},
17
// padding : 20,
18
ticks: {
19
fontSize: 15,
20
padding :20,
21
drawTicks:true,
22
offset: true,
23
// labelOffset: 70,
24
userCallback: function(value, index, values) { },
25
},
26
gridLines:{
27
display: xLineState,
28
lineWidth: xLine,
29
// offsetGridLines: true
30
},
31
}],
32
yAxes: [{
33
afterFit:(axis)=>{
34
},
35
padding : 10,
36
ticks: {
37
// min:10,
38
fontSize: 15,
39
padding :15,
40
beginAtZero: true,
41
userCallback: function(value, index, values) { }
42
},
43
gridLines:{
44
display: yLineState,
45
lineWidth: yLine
46
}
47
}],
48
49
},
50
scaleLabel: {
51
display: true,
52
labelString: 'Moola',
53
fontSize: 20,
54
},
55
tooltips: {
56
enabled: false,
57
mode: 'index',
58
position: 'nearest',
59
custom: customTooltips,
60
callbacks:
61
{
62
label:function (tooltipItem, data) { },
63
footer:function (tooltipItems, data) { }
64
}
65
},
66
legend:{
67
labels: {
68
fontSize: legendFontSize,
69
},
70
onClick:function (e,legendItem) { }
71
}};
72
73
74
75
new Chart(ctx, config);
76
I want padding at the beginning of the X-axis as shown in the image below.
But padding doesn’t work in my code.
What’s the matter? paddingLeft is not working..plz
Advertisement
Answer
You are doing nothing wrong, it seems like it is a bug in chart.js. version 2.4.0 is the latest version I can see it works in.
Working example:
JavaScript
1
36
36
1
var options = {
2
type: 'line',
3
data: {
4
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
5
datasets: [{
6
label: '# of Votes',
7
data: [12, 19, 3, 5, 2, 3],
8
borderWidth: 1
9
},
10
{
11
label: '# of Points',
12
data: [7, 11, 5, 8, 3, 7],
13
borderWidth: 1
14
}
15
]
16
},
17
options: {
18
scales: {
19
xAxes: [{
20
afterFit: (axis) => {
21
// console.log(axis)
22
axis.paddingLeft = 25;
23
axis.paddingRight = 15;
24
},
25
}],
26
yAxes: [{
27
ticks: {
28
reverse: false
29
}
30
}]
31
}
32
}
33
}
34
35
var ctx = document.getElementById('chartJSContainer').getContext('2d');
36
new Chart(ctx, options);
JavaScript
1
3
1
canvas {
2
background-color: #eee;
3
}
JavaScript
1
5
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.js" integrity="sha512-K89oirx9CqibxV7RiVHkr4Bu5l9ZCUGt8sKQGMAFWM1gfmjXYNp0/IsL94rzQCjzOUYU8yNN4TiOQ7Vx8J1Iig==" crossorigin="anonymous"></script>
2
3
<body>
4
<canvas id="chartJSContainer" width="600" height="400"></canvas>
5
</body>
fiddle link: https://jsfiddle.net/Leelenaleee/dxrm918n/2/ Filed an issue for this on there git: https://github.com/chartjs/Chart.js/issues/8344