}
};
+ /**
+ * Helper function to get the box width based on the usePointStyle option
+ * @param labelopts {Object} the label options on the legend
+ * @param fontSize {Number} the label font size
+ * @return {Number} width of the color box area
+ */
+ function getBoxWidth(labelOpts, fontSize) {
+ return labelOpts.usePointStyle ?
+ fontSize * Math.SQRT2 :
+ labelOpts.boxWidth;
+ }
+
Chart.Legend = Chart.Element.extend({
initialize: function(config) {
ctx.textBaseline = 'top';
helpers.each(me.legendItems, function(legendItem, i) {
- var boxWidth = labelOpts.usePointStyle ?
- fontSize * Math.sqrt(2) :
- labelOpts.boxWidth;
-
+ var boxWidth = getBoxWidth(labelOpts, fontSize);
var width = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
+
if (lineWidths[lineWidths.length - 1] + width + labelOpts.padding >= me.width) {
totalHeight += fontSize + (labelOpts.padding);
lineWidths[lineWidths.length] = me.left;
var itemHeight = fontSize + vPadding;
helpers.each(me.legendItems, function(legendItem, i) {
- // 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 boxWidth = getBoxWidth(labelOpts, fontSize);
var itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
// If too tall, go to new column
ctx.fillStyle = fontColor; // render in correct colour
ctx.font = labelFont;
- var boxWidth = labelOpts.boxWidth,
+ var boxWidth = getBoxWidth(labelOpts, fontSize),
hitboxes = me.legendHitBoxes;
// current position
var itemHeight = fontSize + labelOpts.padding;
helpers.each(me.legendItems, function(legendItem, i) {
var textWidth = ctx.measureText(legendItem.text).width,
- width = labelOpts.usePointStyle ?
- fontSize + (fontSize / 2) + textWidth :
- boxWidth + (fontSize / 2) + textWidth,
+ width = boxWidth + (fontSize / 2) + textWidth,
x = cursor.x,
y = cursor.y;