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

Provide a rectangle getArea implementation for horizontal bars #6027

Merged
merged 2 commits into from Feb 4, 2019
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
5 changes: 4 additions & 1 deletion src/elements/element.rectangle.js
Expand Up @@ -206,7 +206,10 @@ module.exports = Element.extend({

getArea: function() {
var vm = this._view;
return vm.width * Math.abs(vm.y - vm.base);

return isVertical(this)
? vm.width * Math.abs(vm.y - vm.base)
: vm.height * Math.abs(vm.x - vm.base);
},

tooltipPosition: function() {
Expand Down
19 changes: 18 additions & 1 deletion test/specs/element.rectangle.tests.js
Expand Up @@ -132,7 +132,7 @@ describe('Rectangle element tests', function() {
});
});

it ('should get the correct area', function() {
it ('should get the correct vertical area', function() {
var rectangle = new Chart.elements.Rectangle({
_datasetIndex: 2,
_index: 1
Expand All @@ -149,6 +149,23 @@ describe('Rectangle element tests', function() {
expect(rectangle.getArea()).toEqual(60);
});

it ('should get the correct horizontal area', function() {
var rectangle = new Chart.elements.Rectangle({
_datasetIndex: 2,
_index: 1
});

// Attach a view object as if we were the controller
rectangle._view = {
base: 0,
height: 4,
x: 10,
y: 15
};

expect(rectangle.getArea()).toEqual(40);
});

it ('should get the center', function() {
var rectangle = new Chart.elements.Rectangle({
_datasetIndex: 2,
Expand Down