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

Method drawTitle

src/plugins/plugin.legend.js:443–488  ·  view source on GitHub ↗

* @protected

()

Source from the content-addressed store, hash-verified

441 * @protected
442 */
443 drawTitle() {
444 const opts = this.options;
445 const titleOpts = opts.title;
446 const titleFont = toFont(titleOpts.font);
447 const titlePadding = toPadding(titleOpts.padding);
448
449 if (!titleOpts.display) {
450 return;
451 }
452
453 const rtlHelper = getRtlAdapter(opts.rtl, this.left, this.width);
454 const ctx = this.ctx;
455 const position = titleOpts.position;
456 const halfFontSize = titleFont.size / 2;
457 const topPaddingPlusHalfFontSize = titlePadding.top + halfFontSize;
458 let y;
459
460 // These defaults are used when the legend is vertical.
461 // When horizontal, they are computed below.
462 let left = this.left;
463 let maxWidth = this.width;
464
465 if (this.isHorizontal()) {
466 // Move left / right so that the title is above the legend lines
467 maxWidth = Math.max(...this.lineWidths);
468 y = this.top + topPaddingPlusHalfFontSize;
469 left = _alignStartEnd(opts.align, left, this.right - maxWidth);
470 } else {
471 // Move down so that the title is above the legend stack in every alignment
472 const maxHeight = this.columnSizes.reduce((acc, size) => Math.max(acc, size.height), 0);
473 y = topPaddingPlusHalfFontSize + _alignStartEnd(opts.align, this.top, this.bottom - maxHeight - opts.labels.padding - this._computeTitleHeight());
474 }
475
476 // Now that we know the left edge of the inner legend box, compute the correct
477 // X coordinate from the title alignment
478 const x = _alignStartEnd(position, left, left + maxWidth);
479
480 // Canvas setup
481 ctx.textAlign = rtlHelper.textAlign(_toLeftRightCenter(position));
482 ctx.textBaseline = 'middle';
483 ctx.strokeStyle = titleOpts.color;
484 ctx.fillStyle = titleOpts.color;
485 ctx.font = titleFont.string;
486
487 renderText(ctx, titleOpts.text, x, y, titleFont);
488 }
489
490 /**
491 * @private

Callers 1

_drawMethod · 0.95

Calls 9

isHorizontalMethod · 0.95
_computeTitleHeightMethod · 0.95
toFontFunction · 0.85
toPaddingFunction · 0.85
getRtlAdapterFunction · 0.85
_alignStartEndFunction · 0.85
_toLeftRightCenterFunction · 0.85
renderTextFunction · 0.85
textAlignMethod · 0.80

Tested by

no test coverage detected