Hey guys how is it going?
Im trying to use Chart.js in order to build my data visualization part of my project.
But I'm having a problem trying to use 2 canvas one above each other.
It seems like when using only 1 chart everything is fine but when using two like I mentioned it seems to break the parent div padding .
Using 2.6.0 version
What I tried :
Wrapping them inside another div.
Wrapping each with his own div.
Using grid in CSS each taking a row, but still same problem.
Using floats and or several display and position config.
Relative position worked but they overlap each other.
bits of code :
`
13
243.2
</div>
</div>
</div>
<canvas id="hourChart" width="400" height="120"></canvas>
<canvas id="payChart" width="400" height="120"></canvas>
`
var hourChart = document.getElementById('hourChart').getContext('2d');
var myHourChart = new Chart(hourChart, {
type: 'bar',
data: {
labels: [['23.02.2017','ืืื ืจืืฉืื'],
'Blue','5',6,7,5,1,4,2,5,7,8],
datasets: [{
label: 'ืฉืขืืช ืืืื',
data: [12, 15,5,6,7,5,1,4,2,5,7,8,],
backgroundColor: '#f67a82',
borderWidth:0,
borderColor: '#c5c5c5',
hoverBorderWidth:2,
}],
},
options: {
title: {
display: true,
text: 'ืกืืืืกืืืงื ืฉืขืชืืช ืืืืืช',
fontSize: 20
},
legend: {
display: true,
labels: {
}
}
}
});
var payChart = document.getElementById('payChart').getContext('2d');
var myPayChart = new Chart(payChart, {
type: 'bar',
data: {
labels: [['23.02.2017','ืืื ืจืืฉืื'],
'Blue','5',6,7,5,1,4,2,5,7,8],
datasets: [{
label: 'ืฉืขืืช ืืืื',
data: [12, 15,5,6,7,5,1,4,2,5,7,8,],
backgroundColor: '#f67a82',
borderWidth:0,
borderColor: '#c5c5c5',
hoverBorderWidth:2,
}],
},
options: {
title: {
display: true,
text: 'ืกืืืืกืืืงื ืฉืขืชืืช ืืืืืช',
fontSize: 20
},
legend: {
display: true,
labels: {
}
}
}
});`
Pics :
With two canvas :
http://imgur.com/yEwrdDv
With one canvas : (otherwise same code)
http://imgur.com/TXNqqFZ
@TheWiggleStorm this is likely due to how we detect resizes of the chart container. Adding a <div> around each canvas should fix the issue
@etimberg Thank you friend, but as i've told you, I've already tried that. (what I tried section)
any other ideas?
Did you try a dedicated parent for each canvas, relatively positioned with no padding?
This is only required if your chart is responsive (default), else you don't need these wrappers. When responsive, you also don't need to specify width and height on the canvas.
.chart-wrapper {
position: relative;
}
md5-d5ef5bba618d8d39cfd81ae502d03caa
md5-905597c0323b18940ba589d382939d9a
If that doesn't work, can you provide a jsfiddle that reproduces your issue?
@simonbrunel I did try to put them in the same div with position realetive, probably got the wrong idea from the documentary.
Anyways, worked like a charm!
Thanks guys
Most helpful comment
Did you try a dedicated parent for each canvas, relatively positioned with no padding?
This is only required if your chart is responsive (default), else you don't need these wrappers. When responsive, you also don't need to specify width and height on the canvas.
If that doesn't work, can you provide a jsfiddle that reproduces your issue?