Chart.js/src/elements/element.rectangle.js

91 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-06-12 22:00:48 +02:00
(function() {
2015-06-13 16:15:21 +02:00
"use strict";
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
var root = this,
Chart = root.Chart,
helpers = Chart.helpers;
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
Chart.defaults.global.elements.rectangle = {
backgroundColor: Chart.defaults.global.defaultColor,
borderWidth: 0,
borderColor: Chart.defaults.global.defaultColor,
};
2015-06-12 22:00:48 +02:00
Chart.elements.Rectangle = Chart.Element.extend({
2015-06-13 16:15:21 +02:00
draw: function() {
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
var ctx = this._chart.ctx;
var vm = this._view;
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
var halfWidth = vm.width / 2,
leftX = vm.x - halfWidth,
rightX = vm.x + halfWidth,
top = vm.base - (vm.base - vm.y),
halfStroke = vm.borderWidth / 2;
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
// Canvas doesn't allow us to stroke inside the width so we can
// adjust the sizes to fit if we're setting a stroke on the line
if (vm.borderWidth) {
leftX += halfStroke;
rightX -= halfStroke;
top += halfStroke;
}
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
ctx.beginPath();
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
ctx.fillStyle = vm.backgroundColor;
ctx.strokeStyle = vm.borderColor;
ctx.lineWidth = vm.borderWidth;
2015-06-12 22:00:48 +02:00
2015-06-13 16:15:21 +02:00
// It'd be nice to keep this class totally generic to any rectangle
// and simply specify which border to miss out.
ctx.moveTo(leftX, vm.base);
ctx.lineTo(leftX, top);
ctx.lineTo(rightX, top);
ctx.lineTo(rightX, vm.base);
ctx.fill();
if (vm.borderWidth) {
ctx.stroke();
}
},
height: function() {
var vm = this._view;
return vm.base - vm.y;
},
inRange: function(mouseX, mouseY) {
var vm = this._view;
2015-08-29 14:53:42 +02:00
var inRange = false;
if (vm) {
if (vm.y < vm.base) {
inRange = (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2) && (mouseY >= vm.y && mouseY <= vm.base);
} else {
inRange = (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2) && (mouseY >= vm.base && mouseY <= vm.y);
}
2015-09-18 19:31:25 +02:00
}
2015-08-29 14:53:42 +02:00
return inRange;
2015-06-13 16:15:21 +02:00
},
inLabelRange: function(mouseX) {
2015-06-13 16:15:21 +02:00
var vm = this._view;
2015-08-29 14:53:42 +02:00
if (vm) {
return (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2);
} else {
return false;
}
2015-06-13 16:15:21 +02:00
},
tooltipPosition: function() {
var vm = this._view;
return {
x: vm.x,
y: vm.y
};
2015-06-13 16:15:21 +02:00
},
});
2015-06-12 22:00:48 +02:00
2015-06-12 22:00:48 +02:00
}).call(this);