Core.Title reductions

This commit is contained in:
Evert Timberg 2016-05-05 21:01:48 -04:00
parent 655163a7f5
commit 1b6ffd623c

View File

@ -104,24 +104,27 @@ module.exports = function(Chart) {
beforeFit: noop, beforeFit: noop,
fit: function() { fit: function() {
var ctx = this.ctx, var _this = this,
ctx = _this.ctx,
valueOrDefault = helpers.getValueOrDefault, valueOrDefault = helpers.getValueOrDefault,
opts = this.options, opts = _this.options,
globalDefaults = Chart.defaults.global, globalDefaults = Chart.defaults.global,
display = opts.display, display = opts.display,
fontSize = valueOrDefault(opts.fontSize, globalDefaults.defaultFontSize), fontSize = valueOrDefault(opts.fontSize, globalDefaults.defaultFontSize),
minSize = this.minSize; minSize = _this.minSize,
width = "width",
height = "height";
if (this.isHorizontal()) { if (_this.isHorizontal()) {
minSize.width = this.maxWidth; // fill all the width minSize[width] = _this.maxWidth; // fill all the width
minSize.height = display ? fontSize + (opts.padding * 2) : 0; minSize[height] = display ? fontSize + (opts.padding * 2) : 0;
} else { } else {
minSize.width = display ? fontSize + (opts.padding * 2) : 0; minSize[width] = display ? fontSize + (opts.padding * 2) : 0;
minSize.height = this.maxHeight; // fill all the height minSize[height] = _this.maxHeight; // fill all the height
} }
this.width = minSize.width; _this[width] = minSize[width];
this.height = minSize.height; _this[height] = minSize[height];
}, },
afterFit: noop, afterFit: noop,
@ -134,9 +137,10 @@ module.exports = function(Chart) {
// Actualy draw the title block on the canvas // Actualy draw the title block on the canvas
draw: function() { draw: function() {
var ctx = this.ctx, var _this = this,
ctx = _this.ctx,
valueOrDefault = helpers.getValueOrDefault, valueOrDefault = helpers.getValueOrDefault,
opts = this.options, opts = _this.options,
globalDefaults = Chart.defaults.global; globalDefaults = Chart.defaults.global;
if (opts.display) { if (opts.display) {
@ -146,18 +150,22 @@ module.exports = function(Chart) {
titleFont = helpers.fontString(fontSize, fontStyle, fontFamily), titleFont = helpers.fontString(fontSize, fontStyle, fontFamily),
rotation = 0, rotation = 0,
titleX, titleX,
titleY; titleY,
top = _this.top,
left = _this.left,
bottom = _this.bottom,
right = _this.right;
ctx.fillStyle = valueOrDefault(opts.fontColor, globalDefaults.defaultFontColor); // render in correct colour ctx.fillStyle = valueOrDefault(opts.fontColor, globalDefaults.defaultFontColor); // render in correct colour
ctx.font = titleFont; ctx.font = titleFont;
// Horizontal // Horizontal
if (this.isHorizontal()) { if (_this.isHorizontal()) {
titleX = this.left + ((this.right - this.left) / 2); // midpoint of the width titleX = left + ((right - left) / 2); // midpoint of the width
titleY = this.top + ((this.bottom - this.top) / 2); // midpoint of the height titleY = top + ((bottom - top) / 2); // midpoint of the height
} else { } else {
titleX = opts.position === 'left' ? this.left + (fontSize / 2) : this.right - (fontSize / 2); titleX = opts.position === 'left' ? left + (fontSize / 2) : right - (fontSize / 2);
titleY = this.top + ((this.bottom - this.top) / 2); titleY = top + ((bottom - top) / 2);
rotation = Math.PI * (opts.position === 'left' ? -0.5 : 0.5); rotation = Math.PI * (opts.position === 'left' ? -0.5 : 0.5);
} }