mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-04 07:24:02 +01:00
The `clone` method now accepts any type of input but also recursively perform a deep copy of the array items. Rewrite the `configMerge` and `scaleMerge` helpers which now rely on a new generic and customizable `merge` method, that one accepts a target object in which multiple sources are deep copied. Note that the target (first argument) is not cloned and will be modified after calling `merge(target, sources)`. Add a `mergeIf` helper which merge the source properties only if they do not exist in the target object.
242 lines
5.6 KiB
JavaScript
242 lines
5.6 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
var helpers = Chart.helpers;
|
|
var layout = Chart.layoutService;
|
|
var noop = helpers.noop;
|
|
|
|
Chart.defaults.global.title = {
|
|
display: false,
|
|
position: 'top',
|
|
fullWidth: true,
|
|
weight: 2000, // by default greater than legend (1000) to be above
|
|
fontStyle: 'bold',
|
|
padding: 10,
|
|
|
|
// actual title
|
|
text: ''
|
|
};
|
|
|
|
Chart.Title = Chart.Element.extend({
|
|
initialize: function(config) {
|
|
var me = this;
|
|
helpers.extend(me, config);
|
|
|
|
// Contains hit boxes for each dataset (in dataset order)
|
|
me.legendHitBoxes = [];
|
|
},
|
|
|
|
// These methods are ordered by lifecycle. Utilities then follow.
|
|
|
|
beforeUpdate: noop,
|
|
update: function(maxWidth, maxHeight, margins) {
|
|
var me = this;
|
|
|
|
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
|
|
me.beforeUpdate();
|
|
|
|
// Absorb the master measurements
|
|
me.maxWidth = maxWidth;
|
|
me.maxHeight = maxHeight;
|
|
me.margins = margins;
|
|
|
|
// Dimensions
|
|
me.beforeSetDimensions();
|
|
me.setDimensions();
|
|
me.afterSetDimensions();
|
|
// Labels
|
|
me.beforeBuildLabels();
|
|
me.buildLabels();
|
|
me.afterBuildLabels();
|
|
|
|
// Fit
|
|
me.beforeFit();
|
|
me.fit();
|
|
me.afterFit();
|
|
//
|
|
me.afterUpdate();
|
|
|
|
return me.minSize;
|
|
|
|
},
|
|
afterUpdate: noop,
|
|
|
|
//
|
|
|
|
beforeSetDimensions: noop,
|
|
setDimensions: function() {
|
|
var me = this;
|
|
// Set the unconstrained dimension before label rotation
|
|
if (me.isHorizontal()) {
|
|
// Reset position before calculating rotation
|
|
me.width = me.maxWidth;
|
|
me.left = 0;
|
|
me.right = me.width;
|
|
} else {
|
|
me.height = me.maxHeight;
|
|
|
|
// Reset position before calculating rotation
|
|
me.top = 0;
|
|
me.bottom = me.height;
|
|
}
|
|
|
|
// Reset padding
|
|
me.paddingLeft = 0;
|
|
me.paddingTop = 0;
|
|
me.paddingRight = 0;
|
|
me.paddingBottom = 0;
|
|
|
|
// Reset minSize
|
|
me.minSize = {
|
|
width: 0,
|
|
height: 0
|
|
};
|
|
},
|
|
afterSetDimensions: noop,
|
|
|
|
//
|
|
|
|
beforeBuildLabels: noop,
|
|
buildLabels: noop,
|
|
afterBuildLabels: noop,
|
|
|
|
//
|
|
|
|
beforeFit: noop,
|
|
fit: function() {
|
|
var me = this,
|
|
valueOrDefault = helpers.valueOrDefault,
|
|
opts = me.options,
|
|
globalDefaults = Chart.defaults.global,
|
|
display = opts.display,
|
|
fontSize = valueOrDefault(opts.fontSize, globalDefaults.defaultFontSize),
|
|
minSize = me.minSize,
|
|
lineCount = helpers.isArray(opts.text) ? opts.text.length : 1,
|
|
lineHeight = valueOrDefault(opts.lineHeight, fontSize),
|
|
textSize = display ? (lineCount * lineHeight) + (opts.padding * 2) : 0;
|
|
|
|
if (me.isHorizontal()) {
|
|
minSize.width = me.maxWidth; // fill all the width
|
|
minSize.height = textSize;
|
|
} else {
|
|
minSize.width = textSize;
|
|
minSize.height = me.maxHeight; // fill all the height
|
|
}
|
|
|
|
me.width = minSize.width;
|
|
me.height = minSize.height;
|
|
|
|
},
|
|
afterFit: noop,
|
|
|
|
// Shared Methods
|
|
isHorizontal: function() {
|
|
var pos = this.options.position;
|
|
return pos === 'top' || pos === 'bottom';
|
|
},
|
|
|
|
// Actually draw the title block on the canvas
|
|
draw: function() {
|
|
var me = this,
|
|
ctx = me.ctx,
|
|
valueOrDefault = helpers.valueOrDefault,
|
|
opts = me.options,
|
|
globalDefaults = Chart.defaults.global;
|
|
|
|
if (opts.display) {
|
|
var fontSize = valueOrDefault(opts.fontSize, globalDefaults.defaultFontSize),
|
|
fontStyle = valueOrDefault(opts.fontStyle, globalDefaults.defaultFontStyle),
|
|
fontFamily = valueOrDefault(opts.fontFamily, globalDefaults.defaultFontFamily),
|
|
titleFont = helpers.fontString(fontSize, fontStyle, fontFamily),
|
|
lineHeight = valueOrDefault(opts.lineHeight, fontSize),
|
|
rotation = 0,
|
|
titleX,
|
|
titleY,
|
|
top = me.top,
|
|
left = me.left,
|
|
bottom = me.bottom,
|
|
right = me.right,
|
|
maxWidth;
|
|
|
|
ctx.fillStyle = valueOrDefault(opts.fontColor, globalDefaults.defaultFontColor); // render in correct colour
|
|
ctx.font = titleFont;
|
|
|
|
// Horizontal
|
|
if (me.isHorizontal()) {
|
|
titleX = left + ((right - left) / 2); // midpoint of the width
|
|
titleY = top + ((bottom - top) / 2); // midpoint of the height
|
|
maxWidth = right - left;
|
|
} else {
|
|
titleX = opts.position === 'left' ? left + (fontSize / 2) : right - (fontSize / 2);
|
|
titleY = top + ((bottom - top) / 2);
|
|
maxWidth = bottom - top;
|
|
rotation = Math.PI * (opts.position === 'left' ? -0.5 : 0.5);
|
|
}
|
|
|
|
ctx.save();
|
|
ctx.translate(titleX, titleY);
|
|
ctx.rotate(rotation);
|
|
ctx.textAlign = 'center';
|
|
ctx.textBaseline = 'middle';
|
|
|
|
var text = opts.text;
|
|
if (helpers.isArray(text)) {
|
|
var y = 0;
|
|
for (var i = 0; i < text.length; ++i) {
|
|
ctx.fillText(text[i], 0, y, maxWidth);
|
|
y += lineHeight;
|
|
}
|
|
} else {
|
|
ctx.fillText(text, 0, 0, maxWidth);
|
|
}
|
|
|
|
ctx.restore();
|
|
}
|
|
}
|
|
});
|
|
|
|
function createNewTitleBlockAndAttach(chart, titleOpts) {
|
|
var title = new Chart.Title({
|
|
ctx: chart.ctx,
|
|
options: titleOpts,
|
|
chart: chart
|
|
});
|
|
|
|
layout.configure(chart, title, titleOpts);
|
|
layout.addBox(chart, title);
|
|
chart.titleBlock = title;
|
|
}
|
|
|
|
return {
|
|
id: 'title',
|
|
|
|
beforeInit: function(chart) {
|
|
var titleOpts = chart.options.title;
|
|
|
|
if (titleOpts) {
|
|
createNewTitleBlockAndAttach(chart, titleOpts);
|
|
}
|
|
},
|
|
|
|
beforeUpdate: function(chart) {
|
|
var titleOpts = chart.options.title;
|
|
var titleBlock = chart.titleBlock;
|
|
|
|
if (titleOpts) {
|
|
helpers.mergeIf(titleOpts, Chart.defaults.global.title);
|
|
|
|
if (titleBlock) {
|
|
layout.configure(chart, titleBlock, titleOpts);
|
|
titleBlock.options = titleOpts;
|
|
} else {
|
|
createNewTitleBlockAndAttach(chart, titleOpts);
|
|
}
|
|
} else if (titleBlock) {
|
|
Chart.layoutService.removeBox(chart, titleBlock);
|
|
delete chart.titleBlock;
|
|
}
|
|
}
|
|
};
|
|
};
|