2016-09-14 20:05:19 +02:00
|
|
|
'use strict';
|
2016-02-12 04:30:53 +01:00
|
|
|
|
|
|
|
module.exports = function(Chart) {
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
var helpers = Chart.helpers;
|
2016-05-05 01:28:22 +02:00
|
|
|
var noop = helpers.noop;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
Chart.defaults.global.legend = {
|
|
|
|
|
|
|
|
display: true,
|
|
|
|
position: 'top',
|
|
|
|
fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes)
|
2016-02-17 20:42:09 +01:00
|
|
|
reverse: false,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// a callback that will handle
|
|
|
|
onClick: function(e, legendItem) {
|
2016-04-21 19:12:12 +02:00
|
|
|
var index = legendItem.datasetIndex;
|
2016-05-05 01:28:22 +02:00
|
|
|
var ci = this.chart;
|
|
|
|
var meta = ci.getDatasetMeta(index);
|
2016-04-21 19:12:12 +02:00
|
|
|
|
|
|
|
// See controller.isDatasetVisible comment
|
2016-05-05 01:28:22 +02:00
|
|
|
meta.hidden = meta.hidden === null? !ci.data.datasets[index].hidden : null;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// We hid a dataset ... rerender the chart
|
2016-05-05 01:28:22 +02:00
|
|
|
ci.update();
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
|
2016-09-07 16:59:33 +02:00
|
|
|
onHover: null,
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
labels: {
|
|
|
|
boxWidth: 40,
|
|
|
|
padding: 10,
|
|
|
|
// Generates labels shown in the legend
|
|
|
|
// Valid properties to return:
|
|
|
|
// text : text to display
|
|
|
|
// fillStyle : fill of coloured box
|
|
|
|
// strokeStyle: stroke of coloured box
|
|
|
|
// hidden : if this legend item refers to a hidden item
|
|
|
|
// lineCap : cap style for line
|
|
|
|
// lineDash
|
|
|
|
// lineDashOffset :
|
|
|
|
// lineJoin :
|
|
|
|
// lineWidth :
|
2016-04-21 17:11:52 +02:00
|
|
|
generateLabels: function(chart) {
|
|
|
|
var data = chart.data;
|
2016-03-30 02:10:29 +02:00
|
|
|
return helpers.isArray(data.datasets) ? data.datasets.map(function(dataset, i) {
|
2016-02-14 23:06:00 +01:00
|
|
|
return {
|
|
|
|
text: dataset.label,
|
2016-06-04 20:14:16 +02:00
|
|
|
fillStyle: (!helpers.isArray(dataset.backgroundColor) ? dataset.backgroundColor : dataset.backgroundColor[0]),
|
2016-04-21 19:12:12 +02:00
|
|
|
hidden: !chart.isDatasetVisible(i),
|
2016-02-14 23:06:00 +01:00
|
|
|
lineCap: dataset.borderCapStyle,
|
|
|
|
lineDash: dataset.borderDash,
|
|
|
|
lineDashOffset: dataset.borderDashOffset,
|
|
|
|
lineJoin: dataset.borderJoinStyle,
|
|
|
|
lineWidth: dataset.borderWidth,
|
|
|
|
strokeStyle: dataset.borderColor,
|
2016-07-06 04:08:29 +02:00
|
|
|
pointStyle: dataset.pointStyle,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Below is extra data used for toggling the datasets
|
|
|
|
datasetIndex: i
|
|
|
|
};
|
2016-03-30 02:10:29 +02:00
|
|
|
}, this) : [];
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Chart.Legend = Chart.Element.extend({
|
|
|
|
|
|
|
|
initialize: function(config) {
|
|
|
|
helpers.extend(this, config);
|
|
|
|
|
|
|
|
// Contains hit boxes for each dataset (in dataset order)
|
|
|
|
this.legendHitBoxes = [];
|
|
|
|
|
|
|
|
// Are we in doughnut mode which has a different data type
|
|
|
|
this.doughnutMode = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
// These methods are ordered by lifecyle. Utilities then follow.
|
|
|
|
// Any function defined here is inherited by all legend types.
|
|
|
|
// Any function can be extended by the legend type
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
beforeUpdate: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
update: function(maxWidth, maxHeight, margins) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
|
2016-06-04 20:14:16 +02:00
|
|
|
me.beforeUpdate();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Absorb the master measurements
|
2016-06-04 20:14:16 +02:00
|
|
|
me.maxWidth = maxWidth;
|
|
|
|
me.maxHeight = maxHeight;
|
|
|
|
me.margins = margins;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Dimensions
|
2016-06-04 20:14:16 +02:00
|
|
|
me.beforeSetDimensions();
|
|
|
|
me.setDimensions();
|
|
|
|
me.afterSetDimensions();
|
2016-02-14 23:06:00 +01:00
|
|
|
// Labels
|
2016-06-04 20:14:16 +02:00
|
|
|
me.beforeBuildLabels();
|
|
|
|
me.buildLabels();
|
|
|
|
me.afterBuildLabels();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Fit
|
2016-06-04 20:14:16 +02:00
|
|
|
me.beforeFit();
|
|
|
|
me.fit();
|
|
|
|
me.afterFit();
|
2016-02-14 23:06:00 +01:00
|
|
|
//
|
2016-06-04 20:14:16 +02:00
|
|
|
me.afterUpdate();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
return me.minSize;
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
2016-05-05 01:28:22 +02:00
|
|
|
afterUpdate: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
beforeSetDimensions: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
setDimensions: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-02-14 23:06:00 +01:00
|
|
|
// Set the unconstrained dimension before label rotation
|
2016-06-04 20:14:16 +02:00
|
|
|
if (me.isHorizontal()) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// Reset position before calculating rotation
|
2016-06-04 20:14:16 +02:00
|
|
|
me.width = me.maxWidth;
|
|
|
|
me.left = 0;
|
|
|
|
me.right = me.width;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-04 20:14:16 +02:00
|
|
|
me.height = me.maxHeight;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Reset position before calculating rotation
|
2016-06-04 20:14:16 +02:00
|
|
|
me.top = 0;
|
|
|
|
me.bottom = me.height;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset padding
|
2016-06-04 20:14:16 +02:00
|
|
|
me.paddingLeft = 0;
|
|
|
|
me.paddingTop = 0;
|
|
|
|
me.paddingRight = 0;
|
|
|
|
me.paddingBottom = 0;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Reset minSize
|
2016-06-04 20:14:16 +02:00
|
|
|
me.minSize = {
|
2016-02-14 23:06:00 +01:00
|
|
|
width: 0,
|
|
|
|
height: 0
|
|
|
|
};
|
|
|
|
},
|
2016-05-05 01:28:22 +02:00
|
|
|
afterSetDimensions: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
beforeBuildLabels: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
buildLabels: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
me.legendItems = me.options.labels.generateLabels.call(me, me.chart);
|
2016-09-14 20:12:23 +02:00
|
|
|
if (me.options.reverse) {
|
2016-06-04 20:14:16 +02:00
|
|
|
me.legendItems.reverse();
|
2016-02-17 20:42:09 +01:00
|
|
|
}
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
2016-05-05 01:28:22 +02:00
|
|
|
afterBuildLabels: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
beforeFit: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
fit: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var opts = me.options;
|
2016-05-05 01:28:22 +02:00
|
|
|
var labelOpts = opts.labels;
|
|
|
|
var display = opts.display;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var ctx = me.ctx;
|
2016-05-05 01:28:22 +02:00
|
|
|
|
|
|
|
var globalDefault = Chart.defaults.global,
|
|
|
|
itemOrDefault = helpers.getValueOrDefault,
|
|
|
|
fontSize = itemOrDefault(labelOpts.fontSize, globalDefault.defaultFontSize),
|
|
|
|
fontStyle = itemOrDefault(labelOpts.fontStyle, globalDefault.defaultFontStyle),
|
|
|
|
fontFamily = itemOrDefault(labelOpts.fontFamily, globalDefault.defaultFontFamily),
|
|
|
|
labelFont = helpers.fontString(fontSize, fontStyle, fontFamily);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Reset hit boxes
|
2016-06-04 20:14:16 +02:00
|
|
|
var hitboxes = me.legendHitBoxes = [];
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var minSize = me.minSize;
|
|
|
|
var isHorizontal = me.isHorizontal();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (isHorizontal) {
|
2016-06-04 20:14:16 +02:00
|
|
|
minSize.width = me.maxWidth; // fill all the width
|
2016-05-05 01:28:22 +02:00
|
|
|
minSize.height = display ? 10 : 0;
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-05-05 01:28:22 +02:00
|
|
|
minSize.width = display ? 10 : 0;
|
2016-06-04 20:14:16 +02:00
|
|
|
minSize.height = me.maxHeight; // fill all the height
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increase sizes here
|
2016-05-05 01:28:22 +02:00
|
|
|
if (display) {
|
2016-06-07 03:41:40 +02:00
|
|
|
ctx.font = labelFont;
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (isHorizontal) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// Labels
|
|
|
|
|
|
|
|
// Width of each line of legend boxes. Labels wrap onto multiple lines when there are too many to fit on one
|
2016-06-04 20:14:16 +02:00
|
|
|
var lineWidths = me.lineWidths = [0];
|
|
|
|
var totalHeight = me.legendItems.length ? fontSize + (labelOpts.padding) : 0;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-09-14 20:05:19 +02:00
|
|
|
ctx.textAlign = 'left';
|
2016-02-14 23:06:00 +01:00
|
|
|
ctx.textBaseline = 'top';
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
helpers.each(me.legendItems, function(legendItem, i) {
|
2016-07-06 04:08:29 +02:00
|
|
|
var boxWidth = labelOpts.usePointStyle ?
|
|
|
|
fontSize * Math.sqrt(2) :
|
|
|
|
labelOpts.boxWidth;
|
|
|
|
|
|
|
|
var width = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
|
2016-06-04 20:14:16 +02:00
|
|
|
if (lineWidths[lineWidths.length - 1] + width + labelOpts.padding >= me.width) {
|
2016-05-05 01:28:22 +02:00
|
|
|
totalHeight += fontSize + (labelOpts.padding);
|
2016-06-04 20:14:16 +02:00
|
|
|
lineWidths[lineWidths.length] = me.left;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the hitbox width and height here. Final position will be updated in `draw`
|
2016-05-05 01:28:22 +02:00
|
|
|
hitboxes[i] = {
|
2016-02-14 23:06:00 +01:00
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: width,
|
2016-02-28 19:41:17 +01:00
|
|
|
height: fontSize
|
2016-02-14 23:06:00 +01:00
|
|
|
};
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
lineWidths[lineWidths.length - 1] += width + labelOpts.padding;
|
2016-06-07 03:41:40 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
minSize.height += totalHeight;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
} else {
|
2016-06-07 03:41:40 +02:00
|
|
|
var vPadding = labelOpts.padding;
|
|
|
|
var columnWidths = me.columnWidths = [];
|
|
|
|
var totalWidth = labelOpts.padding;
|
|
|
|
var currentColWidth = 0;
|
|
|
|
var currentColHeight = 0;
|
|
|
|
var itemHeight = fontSize + vPadding;
|
|
|
|
|
|
|
|
helpers.each(me.legendItems, function(legendItem, i) {
|
2016-07-06 04:08:29 +02:00
|
|
|
// If usePointStyle is set, multiple boxWidth by 2 since it represents
|
|
|
|
// the radius and not truly the width
|
|
|
|
var boxWidth = labelOpts.usePointStyle ? 2 * labelOpts.boxWidth : labelOpts.boxWidth;
|
|
|
|
|
|
|
|
var itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
|
2016-06-07 03:41:40 +02:00
|
|
|
|
|
|
|
// If too tall, go to new column
|
|
|
|
if (currentColHeight + itemHeight > minSize.height) {
|
|
|
|
totalWidth += currentColWidth + labelOpts.padding;
|
|
|
|
columnWidths.push(currentColWidth); // previous column width
|
|
|
|
|
|
|
|
currentColWidth = 0;
|
|
|
|
currentColHeight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get max width
|
|
|
|
currentColWidth = Math.max(currentColWidth, itemWidth);
|
|
|
|
currentColHeight += itemHeight;
|
|
|
|
|
|
|
|
// Store the hitbox width and height here. Final position will be updated in `draw`
|
|
|
|
hitboxes[i] = {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: itemWidth,
|
|
|
|
height: fontSize
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
totalWidth += currentColWidth;
|
|
|
|
columnWidths.push(currentColWidth);
|
|
|
|
minSize.width += totalWidth;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
me.width = minSize.width;
|
|
|
|
me.height = minSize.height;
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
2016-05-05 01:28:22 +02:00
|
|
|
afterFit: noop,
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Shared Methods
|
|
|
|
isHorizontal: function() {
|
2016-09-14 20:05:19 +02:00
|
|
|
return this.options.position === 'top' || this.options.position === 'bottom';
|
2016-02-14 23:06:00 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Actualy draw the legend on the canvas
|
|
|
|
draw: function() {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
|
|
|
var opts = me.options;
|
2016-05-05 01:28:22 +02:00
|
|
|
var labelOpts = opts.labels;
|
2016-07-06 04:08:29 +02:00
|
|
|
var globalDefault = Chart.defaults.global,
|
|
|
|
lineDefault = globalDefault.elements.line,
|
|
|
|
legendWidth = me.width,
|
|
|
|
lineWidths = me.lineWidths;
|
2016-05-05 01:28:22 +02:00
|
|
|
|
|
|
|
if (opts.display) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var ctx = me.ctx,
|
2016-06-07 03:41:40 +02:00
|
|
|
cursor,
|
2016-05-05 01:28:22 +02:00
|
|
|
itemOrDefault = helpers.getValueOrDefault,
|
|
|
|
fontColor = itemOrDefault(labelOpts.fontColor, globalDefault.defaultFontColor),
|
|
|
|
fontSize = itemOrDefault(labelOpts.fontSize, globalDefault.defaultFontSize),
|
|
|
|
fontStyle = itemOrDefault(labelOpts.fontStyle, globalDefault.defaultFontStyle),
|
|
|
|
fontFamily = itemOrDefault(labelOpts.fontFamily, globalDefault.defaultFontFamily),
|
|
|
|
labelFont = helpers.fontString(fontSize, fontStyle, fontFamily);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
// Canvas setup
|
2016-09-14 20:05:19 +02:00
|
|
|
ctx.textAlign = 'left';
|
2016-06-07 04:14:42 +02:00
|
|
|
ctx.textBaseline = 'top';
|
|
|
|
ctx.lineWidth = 0.5;
|
|
|
|
ctx.strokeStyle = fontColor; // for strikethrough effect
|
|
|
|
ctx.fillStyle = fontColor; // render in correct colour
|
|
|
|
ctx.font = labelFont;
|
|
|
|
|
|
|
|
var boxWidth = labelOpts.boxWidth,
|
|
|
|
hitboxes = me.legendHitBoxes;
|
|
|
|
|
|
|
|
// current position
|
|
|
|
var drawLegendBox = function(x, y, legendItem) {
|
2016-07-06 04:08:29 +02:00
|
|
|
if (isNaN(boxWidth) || boxWidth <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
// Set the ctx for the box
|
|
|
|
ctx.save();
|
|
|
|
|
|
|
|
ctx.fillStyle = itemOrDefault(legendItem.fillStyle, globalDefault.defaultColor);
|
|
|
|
ctx.lineCap = itemOrDefault(legendItem.lineCap, lineDefault.borderCapStyle);
|
|
|
|
ctx.lineDashOffset = itemOrDefault(legendItem.lineDashOffset, lineDefault.borderDashOffset);
|
|
|
|
ctx.lineJoin = itemOrDefault(legendItem.lineJoin, lineDefault.borderJoinStyle);
|
|
|
|
ctx.lineWidth = itemOrDefault(legendItem.lineWidth, lineDefault.borderWidth);
|
|
|
|
ctx.strokeStyle = itemOrDefault(legendItem.strokeStyle, globalDefault.defaultColor);
|
2016-09-01 08:28:10 +02:00
|
|
|
var isLineWidthZero = (itemOrDefault(legendItem.lineWidth, lineDefault.borderWidth) === 0);
|
2016-06-07 04:14:42 +02:00
|
|
|
|
|
|
|
if (ctx.setLineDash) {
|
|
|
|
// IE 9 and 10 do not support line dash
|
|
|
|
ctx.setLineDash(itemOrDefault(legendItem.lineDash, lineDefault.borderDash));
|
|
|
|
}
|
|
|
|
|
2016-07-06 04:08:29 +02:00
|
|
|
if (opts.labels && opts.labels.usePointStyle) {
|
|
|
|
// Recalulate x and y for drawPoint() because its expecting
|
|
|
|
// x and y to be center of figure (instead of top left)
|
|
|
|
var radius = fontSize * Math.SQRT2 / 2;
|
|
|
|
var offSet = radius / Math.SQRT2;
|
|
|
|
var centerX = x + offSet;
|
|
|
|
var centerY = y + offSet;
|
|
|
|
|
|
|
|
// Draw pointStyle as legend symbol
|
|
|
|
Chart.canvasHelpers.drawPoint(ctx, legendItem.pointStyle, radius, centerX, centerY);
|
2016-09-03 17:49:58 +02:00
|
|
|
} else {
|
2016-07-06 04:08:29 +02:00
|
|
|
// Draw box as legend symbol
|
2016-09-03 17:49:58 +02:00
|
|
|
if (!isLineWidthZero) {
|
2016-09-03 10:53:32 +02:00
|
|
|
ctx.strokeRect(x, y, boxWidth, fontSize);
|
2016-09-01 08:18:42 +02:00
|
|
|
}
|
2016-07-06 04:08:29 +02:00
|
|
|
ctx.fillRect(x, y, boxWidth, fontSize);
|
|
|
|
}
|
2016-06-07 04:14:42 +02:00
|
|
|
|
|
|
|
ctx.restore();
|
|
|
|
};
|
|
|
|
var fillText = function(x, y, legendItem, textWidth) {
|
|
|
|
ctx.fillText(legendItem.text, boxWidth + (fontSize / 2) + x, y);
|
|
|
|
|
|
|
|
if (legendItem.hidden) {
|
|
|
|
// Strikethrough the text if hidden
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.lineWidth = 2;
|
|
|
|
ctx.moveTo(boxWidth + (fontSize / 2) + x, y + (fontSize / 2));
|
|
|
|
ctx.lineTo(boxWidth + (fontSize / 2) + x + textWidth, y + (fontSize / 2));
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
// Horizontal
|
2016-06-07 04:14:42 +02:00
|
|
|
var isHorizontal = me.isHorizontal();
|
|
|
|
if (isHorizontal) {
|
2016-06-07 03:41:40 +02:00
|
|
|
cursor = {
|
|
|
|
x: me.left + ((legendWidth - lineWidths[0]) / 2),
|
|
|
|
y: me.top + labelOpts.padding,
|
|
|
|
line: 0
|
|
|
|
};
|
2016-02-14 23:06:00 +01:00
|
|
|
} else {
|
2016-06-07 03:41:40 +02:00
|
|
|
cursor = {
|
|
|
|
x: me.left + labelOpts.padding,
|
2016-07-06 04:08:29 +02:00
|
|
|
y: me.top + labelOpts.padding,
|
2016-06-07 03:41:40 +02:00
|
|
|
line: 0
|
|
|
|
};
|
2016-06-07 04:14:42 +02:00
|
|
|
}
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
var itemHeight = fontSize + labelOpts.padding;
|
|
|
|
helpers.each(me.legendItems, function(legendItem, i) {
|
|
|
|
var textWidth = ctx.measureText(legendItem.text).width,
|
2016-07-06 04:08:29 +02:00
|
|
|
width = labelOpts.usePointStyle ?
|
|
|
|
fontSize + (fontSize / 2) + textWidth :
|
|
|
|
boxWidth + (fontSize / 2) + textWidth,
|
2016-06-07 04:14:42 +02:00
|
|
|
x = cursor.x,
|
|
|
|
y = cursor.y;
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
if (isHorizontal) {
|
|
|
|
if (x + width >= legendWidth) {
|
2016-07-06 04:08:29 +02:00
|
|
|
y = cursor.y += itemHeight;
|
2016-06-07 04:14:42 +02:00
|
|
|
cursor.line++;
|
|
|
|
x = cursor.x = me.left + ((legendWidth - lineWidths[cursor.line]) / 2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (y + itemHeight > me.bottom) {
|
2016-06-07 03:41:40 +02:00
|
|
|
x = cursor.x = x + me.columnWidths[cursor.line] + labelOpts.padding;
|
|
|
|
y = cursor.y = me.top;
|
|
|
|
cursor.line++;
|
|
|
|
}
|
2016-06-07 04:14:42 +02:00
|
|
|
}
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
drawLegendBox(x, y, legendItem);
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
hitboxes[i].left = x;
|
|
|
|
hitboxes[i].top = y;
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
// Fill the actual label
|
|
|
|
fillText(x, y, legendItem, textWidth);
|
2016-06-07 03:41:40 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
if (isHorizontal) {
|
|
|
|
cursor.x += width + (labelOpts.padding);
|
|
|
|
} else {
|
|
|
|
cursor.y += itemHeight;
|
|
|
|
}
|
2016-06-18 11:00:11 +02:00
|
|
|
|
2016-06-07 04:14:42 +02:00
|
|
|
});
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Handle an event
|
|
|
|
handleEvent: function(e) {
|
2016-06-04 20:14:16 +02:00
|
|
|
var me = this;
|
2016-09-07 16:59:33 +02:00
|
|
|
var opts = me.options;
|
|
|
|
var type = e.type === 'mouseup' ? 'click' : e.type;
|
|
|
|
|
|
|
|
if (type === 'mousemove') {
|
|
|
|
if (!opts.onHover) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (type === 'click') {
|
|
|
|
if (!opts.onClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
var position = helpers.getRelativePosition(e, me.chart.chart),
|
2016-05-05 01:28:22 +02:00
|
|
|
x = position.x,
|
2016-09-07 16:59:33 +02:00
|
|
|
y = position.y;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-06-04 20:14:16 +02:00
|
|
|
if (x >= me.left && x <= me.right && y >= me.top && y <= me.bottom) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// See if we are touching one of the dataset boxes
|
2016-06-04 20:14:16 +02:00
|
|
|
var lh = me.legendHitBoxes;
|
2016-05-05 01:28:22 +02:00
|
|
|
for (var i = 0; i < lh.length; ++i) {
|
|
|
|
var hitBox = lh[i];
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (x >= hitBox.left && x <= hitBox.left + hitBox.width && y >= hitBox.top && y <= hitBox.top + hitBox.height) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// Touching an element
|
2016-09-07 16:59:33 +02:00
|
|
|
if (type === 'click') {
|
2016-06-04 20:14:16 +02:00
|
|
|
opts.onClick.call(me, e, me.legendItems[i]);
|
2016-09-07 16:59:33 +02:00
|
|
|
break;
|
|
|
|
} else if (type === 'mousemove') {
|
|
|
|
opts.onHover.call(me, e, me.legendItems[i]);
|
|
|
|
break;
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-26 03:23:46 +02:00
|
|
|
// Register the legend plugin
|
2016-06-10 22:26:35 +02:00
|
|
|
Chart.plugins.register({
|
2016-05-26 03:23:46 +02:00
|
|
|
beforeInit: function(chartInstance) {
|
|
|
|
var opts = chartInstance.options;
|
|
|
|
var legendOpts = opts.legend;
|
|
|
|
|
|
|
|
if (legendOpts) {
|
|
|
|
chartInstance.legend = new Chart.Legend({
|
|
|
|
ctx: chartInstance.chart.ctx,
|
|
|
|
options: legendOpts,
|
|
|
|
chart: chartInstance
|
|
|
|
});
|
|
|
|
|
|
|
|
Chart.layoutService.addBox(chartInstance, chartInstance.legend);
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 03:28:02 +02:00
|
|
|
});
|
2016-02-17 20:42:09 +01:00
|
|
|
};
|