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

96 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-06-12 22:00:48 +02:00
/*!
* Chart.js
* http://chartjs.org/
* Version: {{ version }}
*
* Copyright 2015 Nick Downie
* Released under the MIT license
* https://github.com/nnnick/Chart.js/blob/master/LICENSE.md
*/
(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
2015-06-13 16:15:21 +02:00
Chart.Rectangle = Chart.Element.extend({
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;
if (vm.y < vm.base) {
return (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2) && (mouseY >= vm.y && mouseY <= vm.base);
} else {
return (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2) && (mouseY >= vm.base && mouseY <= vm.y);
}
},
inGroupRange: function(mouseX) {
var vm = this._view;
return (mouseX >= vm.x - vm.width / 2 && mouseX <= vm.x + vm.width / 2);
},
tooltipPosition: function() {
var vm = this._view;
if (vm.y < vm.base) {
return {
x: vm.x,
y: vm.y
};
} else {
return {
x: vm.x,
y: vm.base
};
}
},
});
2015-06-12 22:00:48 +02:00
}).call(this);