mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-09 09:46:51 +01:00
Fixed miscalculation of Bar width.
for Bar and horizontalBar type, include stacked scale. issue #3589
This commit is contained in:
committed by
Evert Timberg
parent
b39c0e1f93
commit
bdcdbc2abf
@@ -240,7 +240,8 @@ describe('Bar controller tests', function() {
|
||||
scales: {
|
||||
xAxes: [{
|
||||
type: 'category',
|
||||
stacked: true
|
||||
stacked: true,
|
||||
barPercentage: 1
|
||||
}],
|
||||
yAxes: [{
|
||||
type: 'linear',
|
||||
@@ -296,7 +297,8 @@ describe('Bar controller tests', function() {
|
||||
scales: {
|
||||
xAxes: [{
|
||||
type: 'category',
|
||||
stacked: true
|
||||
stacked: true,
|
||||
barPercentage: 1
|
||||
}],
|
||||
yAxes: [{
|
||||
type: 'linear',
|
||||
@@ -487,4 +489,154 @@ describe('Bar controller tests', function() {
|
||||
expect(bar._model.borderColor).toBe('rgb(0, 255, 0)');
|
||||
expect(bar._model.borderWidth).toBe(1.5);
|
||||
});
|
||||
|
||||
describe('Bar width', function() {
|
||||
beforeEach(function() {
|
||||
// 2 datasets
|
||||
this.data = {
|
||||
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
|
||||
datasets: [{
|
||||
data: [10, 20, 30, 40, 50, 60, 70],
|
||||
}, {
|
||||
data: [10, 20, 30, 40, 50, 60, 70],
|
||||
}]
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
var chart = window.acquireChart(this.config);
|
||||
var meta = chart.getDatasetMeta(0);
|
||||
var xScale = chart.scales[meta.xAxisID];
|
||||
|
||||
var categoryPercentage = xScale.options.categoryPercentage;
|
||||
var barPercentage = xScale.options.barPercentage;
|
||||
var stacked = xScale.options.stacked;
|
||||
|
||||
var totalBarWidth = 0;
|
||||
for (var i = 0; i < chart.data.datasets.length; i++) {
|
||||
var bars = chart.getDatasetMeta(i).data;
|
||||
for (var j = xScale.minIndex; j <= xScale.maxIndex; j++) {
|
||||
totalBarWidth += bars[j]._model.width;
|
||||
}
|
||||
if (stacked) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var actualValue = totalBarWidth;
|
||||
var expectedValue = xScale.width * categoryPercentage * barPercentage;
|
||||
expect(actualValue).toBeCloseToPixel(expectedValue);
|
||||
|
||||
});
|
||||
|
||||
it('should correctly set bar width when min and max option is set.', function() {
|
||||
this.config = {
|
||||
type: 'bar',
|
||||
data: this.data,
|
||||
options: {
|
||||
scales: {
|
||||
xAxes: [{
|
||||
ticks: {
|
||||
min: 'March',
|
||||
max: 'May',
|
||||
},
|
||||
}]
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('should correctly set bar width when scale are stacked with min and max options.', function() {
|
||||
this.config = {
|
||||
type: 'bar',
|
||||
data: this.data,
|
||||
options: {
|
||||
scales: {
|
||||
xAxes: [{
|
||||
ticks: {
|
||||
min: 'March',
|
||||
max: 'May',
|
||||
},
|
||||
stacked: true,
|
||||
}]
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
describe('Bar height (horizontalBar type)', function() {
|
||||
beforeEach(function() {
|
||||
// 2 datasets
|
||||
this.data = {
|
||||
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
|
||||
datasets: [{
|
||||
data: [10, 20, 30, 40, 50, 60, 70],
|
||||
}, {
|
||||
data: [10, 20, 30, 40, 50, 60, 70],
|
||||
}]
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
var chart = window.acquireChart(this.config);
|
||||
var meta = chart.getDatasetMeta(0);
|
||||
var yScale = chart.scales[meta.yAxisID];
|
||||
|
||||
var categoryPercentage = yScale.options.categoryPercentage;
|
||||
var barPercentage = yScale.options.barPercentage;
|
||||
var stacked = yScale.options.stacked;
|
||||
|
||||
var totalBarHeight = 0;
|
||||
for (var i = 0; i < chart.data.datasets.length; i++) {
|
||||
var bars = chart.getDatasetMeta(i).data;
|
||||
for (var j = yScale.minIndex; j <= yScale.maxIndex; j++) {
|
||||
totalBarHeight += bars[j]._model.height;
|
||||
}
|
||||
if (stacked) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var actualValue = totalBarHeight;
|
||||
var expectedValue = yScale.height * categoryPercentage * barPercentage;
|
||||
expect(actualValue).toBeCloseToPixel(expectedValue);
|
||||
|
||||
});
|
||||
|
||||
it('should correctly set bar height when min and max option is set.', function() {
|
||||
this.config = {
|
||||
type: 'horizontalBar',
|
||||
data: this.data,
|
||||
options: {
|
||||
scales: {
|
||||
yAxes: [{
|
||||
ticks: {
|
||||
min: 'March',
|
||||
max: 'May',
|
||||
},
|
||||
}]
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
it('should correctly set bar height when scale are stacked with min and max options.', function() {
|
||||
this.config = {
|
||||
type: 'horizontalBar',
|
||||
data: this.data,
|
||||
options: {
|
||||
scales: {
|
||||
yAxes: [{
|
||||
ticks: {
|
||||
min: 'March',
|
||||
max: 'May',
|
||||
},
|
||||
stacked: true,
|
||||
}]
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user