MCPcopy
hub / github.com/chartjs/Chart.js / drawLegendBox

Method drawLegendBox

src/plugins/plugin.legend.js:305–365  ·  view source on GitHub ↗
(x, y, legendItem)

Source from the content-addressed store, hash-verified

303
304 // current position
305 const drawLegendBox = function(x, y, legendItem) {
306 if (isNaN(boxWidth) || boxWidth <= 0 || isNaN(boxHeight) || boxHeight < 0) {
307 return;
308 }
309
310 // Set the ctx for the box
311 ctx.save();
312
313 const lineWidth = valueOrDefault(legendItem.lineWidth, 1);
314 ctx.fillStyle = valueOrDefault(legendItem.fillStyle, defaultColor);
315 ctx.lineCap = valueOrDefault(legendItem.lineCap, 'butt');
316 ctx.lineDashOffset = valueOrDefault(legendItem.lineDashOffset, 0);
317 ctx.lineJoin = valueOrDefault(legendItem.lineJoin, 'miter');
318 ctx.lineWidth = lineWidth;
319 ctx.strokeStyle = valueOrDefault(legendItem.strokeStyle, defaultColor);
320
321 ctx.setLineDash(valueOrDefault(legendItem.lineDash, []));
322
323 if (labelOpts.usePointStyle) {
324 // Recalculate x and y for drawPoint() because its expecting
325 // x and y to be center of figure (instead of top left)
326 const drawOptions = {
327 radius: boxHeight * Math.SQRT2 / 2,
328 pointStyle: legendItem.pointStyle,
329 rotation: legendItem.rotation,
330 borderWidth: lineWidth
331 };
332 const centerX = rtlHelper.xPlus(x, boxWidth / 2);
333 const centerY = y + halfFontSize;
334
335 // Draw pointStyle as legend symbol
336 drawPointLegend(ctx, drawOptions, centerX, centerY, labelOpts.pointStyleWidth && boxWidth);
337 } else {
338 // Draw box as legend symbol
339 // Adjust position when boxHeight < fontSize (want it centered)
340 const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0);
341 const xBoxLeft = rtlHelper.leftForLtr(x, boxWidth);
342 const borderRadius = toTRBLCorners(legendItem.borderRadius);
343
344 ctx.beginPath();
345
346 if (Object.values(borderRadius).some(v => v !== 0)) {
347 addRoundedRectPath(ctx, {
348 x: xBoxLeft,
349 y: yBoxTop,
350 w: boxWidth,
351 h: boxHeight,
352 radius: borderRadius,
353 });
354 } else {
355 ctx.rect(xBoxLeft, yBoxTop, boxWidth, boxHeight);
356 }
357
358 ctx.fill();
359 if (lineWidth !== 0) {
360 ctx.stroke();
361 }
362 }

Callers

nothing calls this directly

Calls 6

valueOrDefaultFunction · 0.85
drawPointLegendFunction · 0.85
toTRBLCornersFunction · 0.85
addRoundedRectPathFunction · 0.85
xPlusMethod · 0.80
leftForLtrMethod · 0.80

Tested by

no test coverage detected