Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude base-line from bar size #8648

Merged
merged 3 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,21 @@ export default class Scale extends Element {
}
}

getLineWidthForValue(value) {
kurkle marked this conversation as resolved.
Show resolved Hide resolved
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
*/
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/controller.bar/baseLine/bottom.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/bottom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/fixtures/controller.bar/baseLine/left.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/fixtures/controller.bar/baseLine/mid-x.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/mid-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions test/fixtures/controller.bar/baseLine/mid-y.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/mid-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions test/fixtures/controller.bar/baseLine/right.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/fixtures/controller.bar/baseLine/top.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/top.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions test/fixtures/controller.bar/baseLine/value-x.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/value-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions test/fixtures/controller.bar/baseLine/value-y.js
Original file line number Diff line number Diff line change
@@ -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 added test/fixtures/controller.bar/baseLine/value-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.