Skip to content

Commit d4419c8

Browse files
adubesimonbrunel
authored andcommitted
Introduce the 'minBarLength' bar option (chartjs#5741)
1 parent a41ca6e commit d4419c8

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docs/charts/bar.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ The bar chart defines the following configuration options. These options are mer
9797
| `categoryPercentage` | `Number` | `0.8` | Percent (0-1) of the available width each category should be within the sample width. [more...](#barpercentage-vs-categorypercentage)
9898
| `barThickness` | `Number/String` | | Manually set width of each bar in pixels. If set to `'flex'`, it computes "optimal" sample widths that globally arrange bars side by side. If not set (default), bars are equally sized based on the smallest interval. [more...](#barthickness)
9999
| `maxBarThickness` | `Number` | | Set this to ensure that bars are not sized thicker than this.
100+
| `minBarLength` | `Number` | | Set this to ensure that bars have a minimum length in pixels.
100101
| `gridLines.offsetGridLines` | `Boolean` | `true` | If true, the bars for a particular data point fall between the grid lines. The grid line will move to the left by one half of the tick interval. If false, the grid line will go right down the middle of the bars. [more...](#offsetgridlines)
101102

102103
### barThickness

src/controllers/controller.bar.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ module.exports = function(Chart) {
382382
var chart = me.chart;
383383
var meta = me.getMeta();
384384
var scale = me.getValueScale();
385+
var isHorizontal = scale.isHorizontal();
385386
var datasets = chart.data.datasets;
386387
var value = scale.getRightValue(datasets[datasetIndex].data[index]);
388+
var minBarLength = scale.options.minBarLength;
387389
var stacked = scale.options.stacked;
388390
var stack = meta.stack;
389391
var start = 0;
@@ -410,6 +412,15 @@ module.exports = function(Chart) {
410412
head = scale.getPixelForValue(start + value);
411413
size = (head - base) / 2;
412414

415+
if (minBarLength !== undefined && Math.abs(size) < minBarLength) {
416+
size = minBarLength;
417+
if (value >= 0 && !isHorizontal || value < 0 && isHorizontal) {
418+
head = base - minBarLength;
419+
} else {
420+
head = base + minBarLength;
421+
}
422+
}
423+
413424
return {
414425
size: size,
415426
base: base,

test/specs/scale.linear.tests.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,4 +1046,52 @@ describe('Linear Scale', function() {
10461046

10471047
expect(chart.scales['x-axis-0'].max).toEqual(0);
10481048
});
1049+
1050+
it('minBarLength settings should be used on Y axis on bar chart', function() {
1051+
var minBarLength = 4;
1052+
var chart = window.acquireChart({
1053+
type: 'bar',
1054+
data: {
1055+
datasets: [{
1056+
data: [0.05, -0.05, 10, 15, 20, 25, 30, 35]
1057+
}]
1058+
},
1059+
options: {
1060+
scales: {
1061+
yAxes: [{
1062+
minBarLength: minBarLength
1063+
}]
1064+
}
1065+
}
1066+
});
1067+
1068+
var data = chart.getDatasetMeta(0).data;
1069+
1070+
expect(data[0]._model.base - minBarLength).toEqual(data[0]._model.y);
1071+
expect(data[1]._model.base + minBarLength).toEqual(data[1]._model.y);
1072+
});
1073+
1074+
it('minBarLength settings should be used on X axis on horizontalBar chart', function() {
1075+
var minBarLength = 4;
1076+
var chart = window.acquireChart({
1077+
type: 'horizontalBar',
1078+
data: {
1079+
datasets: [{
1080+
data: [0.05, -0.05, 10, 15, 20, 25, 30, 35]
1081+
}]
1082+
},
1083+
options: {
1084+
scales: {
1085+
xAxes: [{
1086+
minBarLength: minBarLength
1087+
}]
1088+
}
1089+
}
1090+
});
1091+
1092+
var data = chart.getDatasetMeta(0).data;
1093+
1094+
expect(data[0]._model.base + minBarLength).toEqual(data[0]._model.x);
1095+
expect(data[1]._model.base - minBarLength).toEqual(data[1]._model.x);
1096+
});
10491097
});

0 commit comments

Comments
 (0)