Exclude base-line from bar size (#8648)

* Exclude base-line from bar size

* lint

* add to types
This commit is contained in:
Jukka Kurkela 2021-03-16 14:49:19 +02:00 committed by GitHub
parent 0a579b6813
commit 420aa027b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 367 additions and 4 deletions

View File

@ -447,6 +447,18 @@ export default class BarController extends DatasetController {
head = base + size;
}
const actualBase = baseValue || 0;
if (base === vScale.getPixelForValue(actualBase)) {
const halfGrid = vScale.getLineWidthForValue(actualBase) / 2;
if (size > 0) {
base += halfGrid;
size -= halfGrid;
} else if (size < 0) {
base -= halfGrid;
size += halfGrid;
}
}
return {
size,
base,

View File

@ -1606,6 +1606,21 @@ export default class Scale extends Element {
}
}
getLineWidthForValue(value) {
const me = this;
const grid = me.options.grid;
if (!me._isVisible() || !grid.display) {
return 0;
}
const ticks = me.ticks;
const index = ticks.findIndex(t => t.value === value);
if (index >= 0) {
const opts = grid.setContext(me.getContext(index));
return opts.lineWidth;
}
return 0;
}
/**
* @protected
*/

View File

@ -0,0 +1,40 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, 2]
}]
},
options: {
scales: {
x: {
display: false
},
y: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 901 B

View File

@ -0,0 +1,41 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, 2]
}]
},
options: {
indexAxis: 'y',
scales: {
y: {
display: false
},
x: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 647 B

View File

@ -0,0 +1,41 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, -1]
}]
},
options: {
indexAxis: 'y',
scales: {
y: {
display: false
},
x: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 758 B

View File

@ -0,0 +1,40 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, -1]
}]
},
options: {
scales: {
x: {
display: false
},
y: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 930 B

View File

@ -0,0 +1,42 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [-1, -2]
}]
},
options: {
indexAxis: 'y',
scales: {
y: {
display: false
},
x: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0,
borderWidth: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 611 B

View File

@ -0,0 +1,41 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [-1, -2]
}]
},
options: {
scales: {
x: {
display: false
},
y: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 0 ? 'red' : 'transparent';
},
borderWidth: 0,
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 895 B

View File

@ -0,0 +1,42 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, 3]
}]
},
options: {
base: 2,
indexAxis: 'y',
scales: {
y: {
display: false
},
x: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 2 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 943 B

View File

@ -0,0 +1,41 @@
module.exports = {
config: {
type: 'bar',
data: {
labels: ['a', 'b'],
datasets: [{
backgroundColor: '#AAFFCC',
borderColor: '#0000FF',
borderWidth: 1,
data: [1, 3]
}]
},
options: {
base: 2,
scales: {
x: {
display: false
},
y: {
ticks: {
display: false
},
grid: {
color: function(context) {
return context.tick.value === 2 ? 'red' : 'transparent';
},
lineWidth: 5,
tickLength: 0
},
}
},
maintainAspectRatio: false
}
},
options: {
canvas: {
width: 128,
height: 128
}
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 951 B

View File

@ -1528,9 +1528,10 @@ describe('Chart.controllers.bar', function() {
});
var data = chart.getDatasetMeta(0).data;
var halfBaseLine = chart.scales.y.getLineWidthForValue(0) / 2;
expect(data[0].base - minBarLength).toEqual(data[0].y);
expect(data[1].base + minBarLength).toEqual(data[1].y);
expect(data[0].base - minBarLength + halfBaseLine).toEqual(data[0].y);
expect(data[1].base + minBarLength - halfBaseLine).toEqual(data[1].y);
});
it('minBarLength settings should be used on X axis on horizontal bar chart', function() {
@ -1547,9 +1548,10 @@ describe('Chart.controllers.bar', function() {
});
var data = chart.getDatasetMeta(0).data;
var halfBaseLine = chart.scales.x.getLineWidthForValue(0) / 2;
expect(data[0].base + minBarLength).toEqual(data[0].x);
expect(data[1].base - minBarLength).toEqual(data[1].x);
expect(data[0].base + minBarLength - halfBaseLine).toEqual(data[0].x);
expect(data[1].base - minBarLength + halfBaseLine).toEqual(data[1].x);
});
it('should respect the data visibility settings', function() {

View File

@ -1199,6 +1199,12 @@ export interface Scale<O extends CoreScaleOptions = CoreScaleOptions> extends El
* @return {string}
*/
getLabelForValue(value: number): string;
/**
* Returns the grid line width at given value
*/
getLineWidthForValue(value: number): number;
/**
* Returns the location of the given data point. Value can either be an index or a numerical value
* The coordinate (0, 0) is at the upper-left corner of the canvas