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

Method _draw

src/plugins/plugin.legend.js:283–438  ·  src/plugins/plugin.legend.js::Legend._draw

* @private

()

Source from the content-addressed store, hash-verified

281 * @private
282 */
283 _draw() {
284 const {options: opts, columnSizes, lineWidths, ctx} = this;
285 const {align, labels: labelOpts} = opts;
286 const defaultColor = defaults.color;
287 const rtlHelper = getRtlAdapter(opts.rtl, this.left, this.width);
288 const labelFont = toFont(labelOpts.font);
289 const {padding} = labelOpts;
290 const fontSize = labelFont.size;
291 const halfFontSize = fontSize / 2;
292 let cursor;
293
294 this.drawTitle();
295
296 class="cm">// Canvas setup
297 ctx.textAlign = rtlHelper.textAlign(class="st">'left');
298 ctx.textBaseline = class="st">'middle';
299 ctx.lineWidth = 0.5;
300 ctx.font = labelFont.string;
301
302 const {boxWidth, boxHeight, itemHeight} = getBoxSize(labelOpts, fontSize);
303
304 class="cm">// current position
305 const drawLegendBox = function(x, y, legendItem) {
306 if (isNaN(boxWidth) || boxWidth <= 0 || isNaN(boxHeight) || boxHeight < 0) {
307 return;
308 }
309
310 class="cm">// 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, class="st">'butt');
316 ctx.lineDashOffset = valueOrDefault(legendItem.lineDashOffset, 0);
317 ctx.lineJoin = valueOrDefault(legendItem.lineJoin, class="st">'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 class="cm">// Recalculate x and y for drawPoint() because its expecting
325 class="cm">// 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 class="cm">// Draw pointStyle as legend symbol
336 drawPointLegend(ctx, drawOptions, centerX, centerY, labelOpts.pointStyleWidth && boxWidth);
337 } else {
338 class="cm">// Draw box as legend symbol
339 class="cm">// Adjust position when boxHeight < fontSize (want it centered)
340 const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0);

Callers 1

drawMethod · 0.95

Calls 14

drawTitleMethod · 0.95
isHorizontalMethod · 0.95
_computeTitleHeightMethod · 0.95
getRtlAdapterFunction · 0.85
toFontFunction · 0.85
getBoxSizeFunction · 0.85
_alignStartEndFunction · 0.85
overrideTextDirectionFunction · 0.85
_textXFunction · 0.85
restoreTextDirectionFunction · 0.85
textAlignMethod · 0.80

Tested by

no test coverage detected