Display multiple chart with same height and width
Hello I am displaying multiple doughnut chart is same page. Issue is they both showing different height and width due to different size of there labels.
So is there any way to display both chart same size? I have attached screenshot of chart and bellow is the JS code and also here is the codepen
var Chart1 = '';
function myfunctionChart(positions, chartId, chartdata1) {
var ChartDataTitle = [];
var ChartDataColor = [];
var ChartDataValue = [];
$('.' + chartdata1 + ' span').each(function() {
ChartDataTitle.push($(this).data('title'));
ChartDataColor.push($(this).data('color'));
ChartDataValue.push($(this).data('value'));
});
var config = {
type: 'doughnut',
data: {
datasets: [{
data: ChartDataValue,
backgroundColor: ChartDataColor,
label: 'Dataset 1'
}],
labels: ChartDataTitle
},
options: {
responsive: false,
maintainAspectRatio: true,
legend: {
position: positions,
},
title: {
display: false,
text: 'Chart.js Doughnut Chart'
},
animation: {
animateScale: true,
animateRotate: true
},
},
};
var elem1 = document.getElementById("mychart1");
if (typeof elem1 !== 'undefined' && elem1 !== null) {
var ctx = document.getElementById(chartId).getContext("2d");
// window.myDoughnut = new Chart(ctx, config);
Chart1 = new Chart(ctx, config);
return Chart1;
}
}
myfunctionChart('right', 'mychart1', 'chartdata1');
myfunctionChart('right', 'mychart2', 'chartdata2');
if ($(window).width() < 1024) {
Chart1.destroy();
myfunctionChart('bottom', 'mychart1', 'chartdata1');
myfunctionChart('bottom', 'mychart2', 'chartdata2');
}

@chiraggmodi This is related to #3458. One solution is to not use the on canvas legend and instead use an HTML legend.
I'm having the exact same issue but with bar graphs. The different labels result in charts of different widths. I've been trying to resolve the issue for over 8 hours now.

(Hiding the legend did not resolve the issue)
@ariellevitsky do you have a fiddle that reproduces that issue?
Just made one now!
https://jsfiddle.net/163s63e6/
@etimberg The problem is caused by calculating a left and right padding for the x-axis core.scale.js#L463-L464, which in the case of a category axis might better be treated differently, since the labels are centered under the bars.
Edit: this is not the case for the original problem, where the legend width is causing the available chartArea to be different.
@chiraggmodi For the original problem you could hack the afterFit function to set the minSize.width and the width of the legend to a fixed value.
codepen example N.B. I changed the div-classes to div-IDs, as they were used as unique identifiers.

var charts = [];
/**
* set fixed width/height option for the legend
*/
Chart.Legend.prototype.afterFit = function() {
var opts = this.options;
if (typeof this.options.width === 'number') {
this.minSize.width = opts.width;
this.width = opts.width;
}
if (typeof this.options.height === 'number') {
this.minSize.height = opts.height;
this.height = opts.height;
}
}
/**
* Equalize legend width/height for all charts
* uses maximum width/height
*/
function equalizeLegendWidth() {
var maxLegendSize = 0;
charts.forEach(function(chart) {
var legend = chart.legend;
var size = legend.isHorizontal()
? legend.height
: legend.width;
if (size > maxLegendSize) {
maxLegendSize = size;
}
});
if (maxLegendSize > 0) {
charts.forEach(function(chart) {
var legend = chart.legend;
if (legend.isHorizontal()) {
chart.legend.options.height = maxLegendSize;
} else {
chart.legend.options.width = maxLegendSize;
}
chart.update();
});
}
}
/**
* get the preferred legend position
*/
function getLegendPosition() {
return ($(window).width() < 1024)
? 'bottom'
: 'right';
}
/**
* update charts
*/
function updateCharts() {
if (charts.length > 0) {
legendPosition = getLegendPosition();
if (charts[0].legend.options.position !== legendPosition) {
buildCharts();
}
}
}
/**
* build charts
*/
function buildCharts() {
var containerId = 'chartdata';
var numcharts = $('[id^=' + containerId + ']').length;
var legendPosition = getLegendPosition();
// reset charts
while(charts.length) {
charts.pop().destroy();
}
// rebuild charts
for (var i = 1; i <= numcharts; i += 1) {
var chartId = 'mychart' + i;
var chartDataId = 'chartdata' + i;
charts.push(constructChart(legendPosition, chartId, chartDataId));
}
equalizeLegendWidth();
}
function constructChart(legendPosition, chartId, chartDataId) {
var backgroundColor = [];
var labels = [];
var data = [];
var ctx = document.getElementById(chartId).getContext("2d");
$('#' + chartDataId + ' span').each(function(ignore, el) {
var elData = el.dataset;
labels.push(elData.title);
backgroundColor.push(elData.color);
data.push(elData.value);
});
var config = {
type: 'doughnut',
data: {
datasets: [{
data: data,
backgroundColor: backgroundColor,
}],
labels: labels
},
options: {
responsive: false,
maintainAspectRatio: true,
legend: {
position: legendPosition,
},
animation: {
animateScale: true,
animateRotate: true
},
},
};
newChart = new Chart(ctx, config);
return newChart;
}
$(function() {
buildCharts();
$(window).on('resize', updateCharts);
});
Edit: added updateCharts to only rebuild charts when the legend position changed, or only update the legend position and recalculate update like shown below.
/**
* update charts
*/
function updateCharts() {
if (charts.length > 0) {
legendPosition = getLegendPosition();
if (charts[0].legend.options.position !== legendPosition) {
charts.forEach(function(chart) {
chart.options.legend.position = legendPosition;
delete chart.options.legend.width;
delete chart.options.legend.height;
chart.update();
});
equalizeLegendWidth();
}
}
}
Good find @jcopperfield on the padding. That should be removed once we know that the label will be drawn from a different point later on.
@jcopperfield thanks man, it working great!! there is some issue in mobile view but will correct that.
@chiraggmodi What is the issue with mobile view?
Is there a similar solution for the bar chart?
@jcopperfield in responsive due to large legend's, it showing scroll bar.
I have attached a screenshot

@ariellevitsky Yes the solution for the bar chart is much simpler. Just add the following code to your scales configuration.
xAxes: [{
type: 'category',
afterFit: function(me) {
me.paddingLeft = 0;
me.paddingRight = 0;
}
}],

1

2

3

Hi! Excuse mi english please (I´m from Agrentina). To resolve two things to “Display multiple chart with same height and width” I add two attributes (one to doughnut, one to legend) on *.component.ts constructor
this.options = {
layout: {
padding: {
left: 0,
right: 0,
top: 0,
bottom: 0
}
},
legend: {
fullWidth: false,
position: 'bottom',
display: true,
positionDoughnutLabelExtended: 'bottomLeft', (this align the legend at the left)
labels: {
fontSize:10,
usePointStyle: true,
}
},
cutoutPercentage:80,
positionPDExtended:'absolute', (this put all the doughnuts on the same place)
};
I can put this attributes to modify the position. On the capture 1 are two attributes, on capture two only de legend attribute (positionDoughnutLabelExtended), on capture 3 without attributes (original set from doughnut and legend). To do this I modify chart.js Someone can resolve this better? Thanks!
Your code works to Chart.js Version 4. Thank you @oliver-j-holmes!
1
2
3
Hi! Excuse mi english please (I´m from Agrentina). To resolve two things to “Display multiple chart with same height and width” I add two attributes (one to doughnut, one to legend) on *.component.ts constructor
this.options = { layout: { padding: { left: 0, right: 0, top: 0, bottom: 0 } }, legend: { fullWidth: false, position: 'bottom', display: true, positionDoughnutLabelExtended: 'bottomLeft', (this align the legend at the left) labels: { fontSize:10, usePointStyle: true, } }, cutoutPercentage:80, positionPDExtended:'absolute', (this put all the doughnuts on the same place) };
I can put this attributes to modify the position. On the capture 1 are two attributes, on capture two only de legend attribute (positionDoughnutLabelExtended), on capture 3 without attributes (original set from doughnut and legend). To do this I modify chart.js Someone can resolve this better? Thanks!