MCPcopy Create free account
hub / github.com/chartjs/Chart.js / fit

Method fit

src/core/core.scale.js:639–692  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

637 call(this.options.beforeFit, [this]);
638 }
639 fit() {
640 // Reset
641 const minSize = {
642 width: 0,
643 height: 0
644 };
645
646 const {chart, options: {ticks: tickOpts, title: titleOpts, grid: gridOpts}} = this;
647 const display = this._isVisible();
648 const isHorizontal = this.isHorizontal();
649
650 if (display) {
651 const titleHeight = getTitleHeight(titleOpts, chart.options.font);
652 if (isHorizontal) {
653 minSize.width = this.maxWidth;
654 minSize.height = getTickMarkLength(gridOpts) + titleHeight;
655 } else {
656 minSize.height = this.maxHeight; // fill all the height
657 minSize.width = getTickMarkLength(gridOpts) + titleHeight;
658 }
659
660 // Don't bother fitting the ticks if we are not showing the labels
661 if (tickOpts.display && this.ticks.length) {
662 const {first, last, widest, highest} = this._getLabelSizes();
663 const tickPadding = tickOpts.padding * 2;
664 const angleRadians = toRadians(this.labelRotation);
665 const cos = Math.cos(angleRadians);
666 const sin = Math.sin(angleRadians);
667
668 if (isHorizontal) {
669 // A horizontal axis is more constrained by the height.
670 const labelHeight = tickOpts.mirror ? 0 : sin * widest.width + cos * highest.height;
671 minSize.height = Math.min(this.maxHeight, minSize.height + labelHeight + tickPadding);
672 } else {
673 // A vertical axis is more constrained by the width. Labels are the
674 // dominant factor here, so get that length first and account for padding
675 const labelWidth = tickOpts.mirror ? 0 : cos * widest.width + sin * highest.height;
676
677 minSize.width = Math.min(this.maxWidth, minSize.width + labelWidth + tickPadding);
678 }
679 this._calculatePadding(first, last, sin, cos);
680 }
681 }
682
683 this._handleMargins();
684
685 if (isHorizontal) {
686 this.width = this._length = chart.width - this._margins.left - this._margins.right;
687 this.height = minSize.height;
688 } else {
689 this.width = minSize.width;
690 this.height = this._length = chart.height - this._margins.top - this._margins.bottom;
691 }
692 }
693
694 _calculatePadding(first, last, sin, cos) {
695 const {ticks: {align, padding}, position} = this.options;

Callers 1

updateMethod · 0.95

Calls 8

_isVisibleMethod · 0.95
isHorizontalMethod · 0.95
_getLabelSizesMethod · 0.95
_calculatePaddingMethod · 0.95
_handleMarginsMethod · 0.95
getTitleHeightFunction · 0.85
getTickMarkLengthFunction · 0.85
toRadiansFunction · 0.85

Tested by

no test coverage detected