I have a bar chart created in chart.js and its config is below:
import Chart from 'chart.js/auto';
document.addEventListener("turbo:load", function(e) {
let manufacturersPieChartCanvas = document.getElementById('manufacturers-pie-chart');
if (manufacturersPieChartCanvas) {
let labels = JSON.parse(manufacturersPieChartCanvas.dataset.manufacturers_chart_labels);
let data = JSON.parse(manufacturersPieChartCanvas.dataset.manufacturers_chart_data);
const config = {
type: 'bar',
data: {
labels: labels,
datasets: [{
backgroundColor: ['yellow', 'red', 'green', 'blue'],
data: data,
hoverOffset: 4
}]
},
options: {
responsive: true,
categoryPercentage: 1,
barPercentage: 0.8,
scaleShowValues: false,
indexAxis: 'y',
scales: {
yAxes: [{
ticks: {
autoSkip: false
}
}]
}
}
};
let manufacturersPieChart = new Chart(
manufacturersPieChartCanvas,
config
);
}
})
A portion of it looks like so:
I am trying to allow ALL the bar titles to show up. I have followed suggestions on SO, in regards to autoSkip false but they don't seem to have any effect. For this chart, it is imperative that all bar titles display all the time. Also, i'd prefer the height to be increased to make each bar slightly thicker. I have tried with config options and can make them thicker but the overall chart area doesn't increase to accommodate them?
Any suggestions to fix these issues?
Thanks.
You are trying to define your scales in V2 syntax while using V3, I advise reading the migration guide first if you are transfering from V2.
To make the bars a bit thicker you can increase the barPercentage and categoryPercentage to 1 but this will remove all the space between the bars. The only other option to make them bigger is by giving the chart actually more space by making the canvas bigger using CSS
var options = {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'pink',
barPercentage: 1,
categoryPercentage: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
backgroundColor: 'orange',
barPercentage: 1,
categoryPercentage: 1
}
]
},
options: {
indexAxis: 'y',
scales: {
y: {
ticks: {
autoSkip: false
}
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
</body>