2016-02-12 04:30:53 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
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,
|
|
|
|
fillStyle: dataset.backgroundColor,
|
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,
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
|
|
|
// Update Lifecycle - Probably don't want to ever extend or overwrite this function ;)
|
|
|
|
this.beforeUpdate();
|
|
|
|
|
|
|
|
// Absorb the master measurements
|
|
|
|
this.maxWidth = maxWidth;
|
|
|
|
this.maxHeight = maxHeight;
|
|
|
|
this.margins = margins;
|
|
|
|
|
|
|
|
// Dimensions
|
|
|
|
this.beforeSetDimensions();
|
|
|
|
this.setDimensions();
|
|
|
|
this.afterSetDimensions();
|
|
|
|
// Labels
|
|
|
|
this.beforeBuildLabels();
|
|
|
|
this.buildLabels();
|
|
|
|
this.afterBuildLabels();
|
|
|
|
|
|
|
|
// Fit
|
|
|
|
this.beforeFit();
|
|
|
|
this.fit();
|
|
|
|
this.afterFit();
|
|
|
|
//
|
|
|
|
this.afterUpdate();
|
|
|
|
|
|
|
|
return this.minSize;
|
|
|
|
},
|
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() {
|
|
|
|
// Set the unconstrained dimension before label rotation
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
// Reset position before calculating rotation
|
|
|
|
this.width = this.maxWidth;
|
|
|
|
this.left = 0;
|
|
|
|
this.right = this.width;
|
|
|
|
} else {
|
|
|
|
this.height = this.maxHeight;
|
|
|
|
|
|
|
|
// Reset position before calculating rotation
|
|
|
|
this.top = 0;
|
|
|
|
this.bottom = this.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset padding
|
|
|
|
this.paddingLeft = 0;
|
|
|
|
this.paddingTop = 0;
|
|
|
|
this.paddingRight = 0;
|
|
|
|
this.paddingBottom = 0;
|
|
|
|
|
|
|
|
// Reset minSize
|
|
|
|
this.minSize = {
|
|
|
|
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-04-21 17:11:52 +02:00
|
|
|
this.legendItems = this.options.labels.generateLabels.call(this, this.chart);
|
2016-02-17 20:42:09 +01:00
|
|
|
if(this.options.reverse){
|
|
|
|
this.legendItems.reverse();
|
|
|
|
}
|
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-05-05 01:28:22 +02:00
|
|
|
var opts = this.options;
|
|
|
|
var labelOpts = opts.labels;
|
|
|
|
var display = opts.display;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
var ctx = this.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-05-05 01:28:22 +02:00
|
|
|
var hitboxes = this.legendHitBoxes = [];
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
var minSize = this.minSize;
|
|
|
|
var isHorizontal = this.isHorizontal();
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (isHorizontal) {
|
|
|
|
minSize.width = this.maxWidth; // fill all the width
|
|
|
|
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;
|
|
|
|
minSize.height = this.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) {
|
|
|
|
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-05-05 01:28:22 +02:00
|
|
|
var lineWidths = this.lineWidths = [0];
|
|
|
|
var totalHeight = this.legendItems.length ? fontSize + (labelOpts.padding) : 0;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
ctx.textAlign = "left";
|
|
|
|
ctx.textBaseline = 'top';
|
|
|
|
ctx.font = labelFont;
|
|
|
|
|
|
|
|
helpers.each(this.legendItems, function(legendItem, i) {
|
2016-05-05 01:28:22 +02:00
|
|
|
var width = labelOpts.boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
|
|
|
|
if (lineWidths[lineWidths.length - 1] + width + labelOpts.padding >= this.width) {
|
|
|
|
totalHeight += fontSize + (labelOpts.padding);
|
|
|
|
lineWidths[lineWidths.length] = this.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-02-14 23:06:00 +01:00
|
|
|
}, this);
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
minSize.height += totalHeight;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO vertical
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
this.width = minSize.width;
|
|
|
|
this.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() {
|
|
|
|
return this.options.position === "top" || this.options.position === "bottom";
|
|
|
|
},
|
|
|
|
|
|
|
|
// Actualy draw the legend on the canvas
|
|
|
|
draw: function() {
|
2016-05-05 01:28:22 +02:00
|
|
|
var opts = this.options;
|
|
|
|
var labelOpts = opts.labels;
|
|
|
|
var globalDefault = Chart.defaults.global,
|
|
|
|
lineDefault = globalDefault.elements.line,
|
|
|
|
legendWidth = this.width,
|
|
|
|
lineWidths = this.lineWidths;
|
|
|
|
|
|
|
|
if (opts.display) {
|
|
|
|
var ctx = this.ctx,
|
|
|
|
cursor = {
|
|
|
|
x: this.left + ((legendWidth - lineWidths[0]) / 2),
|
|
|
|
y: this.top + labelOpts.padding,
|
|
|
|
line: 0
|
|
|
|
},
|
|
|
|
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
|
|
|
|
|
|
|
// Horizontal
|
|
|
|
if (this.isHorizontal()) {
|
|
|
|
// Labels
|
|
|
|
ctx.textAlign = "left";
|
|
|
|
ctx.textBaseline = 'top';
|
|
|
|
ctx.lineWidth = 0.5;
|
2016-02-28 19:41:17 +01:00
|
|
|
ctx.strokeStyle = fontColor; // for strikethrough effect
|
|
|
|
ctx.fillStyle = fontColor; // render in correct colour
|
2016-02-14 23:06:00 +01:00
|
|
|
ctx.font = labelFont;
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
var boxWidth = labelOpts.boxWidth,
|
|
|
|
hitboxes = this.legendHitBoxes;
|
|
|
|
|
2016-02-14 23:06:00 +01:00
|
|
|
helpers.each(this.legendItems, function(legendItem, i) {
|
2016-05-05 01:28:22 +02:00
|
|
|
var textWidth = ctx.measureText(legendItem.text).width,
|
|
|
|
width = boxWidth + (fontSize / 2) + textWidth,
|
|
|
|
x = cursor.x,
|
|
|
|
y = cursor.y;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (x + width >= legendWidth) {
|
2016-05-05 03:06:19 +02:00
|
|
|
y = cursor.y += fontSize + (labelOpts.padding);
|
2016-02-14 23:06:00 +01:00
|
|
|
cursor.line++;
|
2016-05-05 03:06:19 +02:00
|
|
|
x = cursor.x = this.left + ((legendWidth - lineWidths[cursor.line]) / 2);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the ctx for the box
|
|
|
|
ctx.save();
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
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-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
if (ctx.setLineDash) {
|
|
|
|
// IE 9 and 10 do not support line dash
|
2016-05-05 01:28:22 +02:00
|
|
|
ctx.setLineDash(itemOrDefault(legendItem.lineDash, lineDefault.borderDash));
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the box
|
2016-05-05 01:28:22 +02:00
|
|
|
ctx.strokeRect(x, y, boxWidth, fontSize);
|
|
|
|
ctx.fillRect(x, y, boxWidth, fontSize);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
ctx.restore();
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
hitboxes[i].left = x;
|
|
|
|
hitboxes[i].top = y;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
// Fill the actual label
|
2016-05-05 01:28:22 +02:00
|
|
|
ctx.fillText(legendItem.text, boxWidth + (fontSize / 2) + x, y);
|
2016-02-14 23:06:00 +01:00
|
|
|
|
|
|
|
if (legendItem.hidden) {
|
|
|
|
// Strikethrough the text if hidden
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.lineWidth = 2;
|
2016-05-05 01:28:22 +02:00
|
|
|
ctx.moveTo(boxWidth + (fontSize / 2) + x, y + (fontSize / 2));
|
|
|
|
ctx.lineTo(boxWidth + (fontSize / 2) + x + textWidth, y + (fontSize / 2));
|
2016-02-14 23:06:00 +01:00
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
cursor.x += width + (labelOpts.padding);
|
2016-02-14 23:06:00 +01:00
|
|
|
}, this);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Handle an event
|
|
|
|
handleEvent: function(e) {
|
2016-05-05 01:28:22 +02:00
|
|
|
var position = helpers.getRelativePosition(e, this.chart.chart),
|
|
|
|
x = position.x,
|
|
|
|
y = position.y,
|
|
|
|
opts = this.options;
|
2016-02-14 23:06:00 +01:00
|
|
|
|
2016-05-05 01:28:22 +02:00
|
|
|
if (x >= this.left && x <= this.right && y >= this.top && y <= this.bottom) {
|
2016-02-14 23:06:00 +01:00
|
|
|
// See if we are touching one of the dataset boxes
|
2016-05-05 01:28:22 +02:00
|
|
|
var lh = this.legendHitBoxes;
|
|
|
|
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-05-05 01:28:22 +02:00
|
|
|
if (opts.onClick) {
|
|
|
|
opts.onClick.call(this, e, this.legendItems[i]);
|
2016-02-14 23:06:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-02-17 20:42:09 +01:00
|
|
|
};
|