Skip to content

Commit

Permalink
Fix: Bar chart with indexAxis: 'y' requires data.labels (whereas 'x' …
Browse files Browse the repository at this point in the history
…does not) (chartjs#11684)
  • Loading branch information
ItsRkaj committed Feb 28, 2024
1 parent 4068bd8 commit 923314b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/core.config.js
Expand Up @@ -132,6 +132,11 @@ function initConfig(config) {

initOptions(config);

if (config.options.indexAxis === 'y' && config.data.labels.length === 0) {
config.data.labels = Object.keys(config.data.datasets[0].data);
config.data.datasets[0].data = Object.values(config.data.datasets[0].data);
}

return config;
}

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/controller.bar/bar-indexAxis-y.js
@@ -0,0 +1,20 @@
module.exports = {
config: {
type: 'bar',
data: {
datasets: [{
label: '# of Votes',
data: {a: 1, b: 3, c: 2}
}]
},
options: {
indexAxis: 'y'
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};
Binary file added test/fixtures/controller.bar/bar-indexAxis-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/specs/controller.bar.tests.js
Expand Up @@ -1721,4 +1721,23 @@ describe('Chart.controllers.bar', function() {
after: []
}]);
});

it('should treat and render indexAxis y as indexAxis x can without setting data labels', function() {
const chart = window.acquireChart({
type: 'bar',
data: {
datasets: [
{
label: '# of Votes',
data: {a: 1, b: 3, c: 2},
},
],
},
options: {
indexAxis: 'y',
},
});

expect(chart.data.labels.length).toBe(3);
});
});

0 comments on commit 923314b

Please sign in to comment.