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

Method fit

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

Source from the content-addressed store, hash-verified

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