Skip to content

Commit 945d91a

Browse files
etimbergsimonbrunel
authored andcommitted
Provide a rectangle getArea implementation for horizontal bars (#6027)
1 parent 93d5ac9 commit 945d91a

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/elements/element.rectangle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ module.exports = Element.extend({
206206

207207
getArea: function() {
208208
var vm = this._view;
209-
return vm.width * Math.abs(vm.y - vm.base);
209+
210+
return isVertical(this)
211+
? vm.width * Math.abs(vm.y - vm.base)
212+
: vm.height * Math.abs(vm.x - vm.base);
210213
},
211214

212215
tooltipPosition: function() {

test/specs/element.rectangle.tests.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Rectangle element tests', function() {
132132
});
133133
});
134134

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

152+
it ('should get the correct horizontal area', function() {
153+
var rectangle = new Chart.elements.Rectangle({
154+
_datasetIndex: 2,
155+
_index: 1
156+
});
157+
158+
// Attach a view object as if we were the controller
159+
rectangle._view = {
160+
base: 0,
161+
height: 4,
162+
x: 10,
163+
y: 15
164+
};
165+
166+
expect(rectangle.getArea()).toEqual(40);
167+
});
168+
152169
it ('should get the center', function() {
153170
var rectangle = new Chart.elements.Rectangle({
154171
_datasetIndex: 2,

0 commit comments

Comments
 (0)