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

Method _calculatePadding

src/core/core.scale.js:694–742  ·  view source on GitHub ↗
(first, last, sin, cos)

Source from the content-addressed store, hash-verified

692 }
693
694 _calculatePadding(first, last, sin, cos) {
695 const {ticks: {align, padding}, position} = this.options;
696 const isRotated = this.labelRotation !== 0;
697 const labelsBelowTicks = position !== 'top' && this.axis === 'x';
698
699 if (this.isHorizontal()) {
700 const offsetLeft = this.getPixelForTick(0) - this.left;
701 const offsetRight = this.right - this.getPixelForTick(this.ticks.length - 1);
702 let paddingLeft = 0;
703 let paddingRight = 0;
704
705 // Ensure that our ticks are always inside the canvas. When rotated, ticks are right aligned
706 // which means that the right padding is dominated by the font height
707 if (isRotated) {
708 if (labelsBelowTicks) {
709 paddingLeft = cos * first.width;
710 paddingRight = sin * last.height;
711 } else {
712 paddingLeft = sin * first.height;
713 paddingRight = cos * last.width;
714 }
715 } else if (align === 'start') {
716 paddingRight = last.width;
717 } else if (align === 'end') {
718 paddingLeft = first.width;
719 } else if (align !== 'inner') {
720 paddingLeft = first.width / 2;
721 paddingRight = last.width / 2;
722 }
723
724 // Adjust padding taking into account changes in offsets
725 this.paddingLeft = Math.max((paddingLeft - offsetLeft + padding) * this.width / (this.width - offsetLeft), 0);
726 this.paddingRight = Math.max((paddingRight - offsetRight + padding) * this.width / (this.width - offsetRight), 0);
727 } else {
728 let paddingTop = last.height / 2;
729 let paddingBottom = first.height / 2;
730
731 if (align === 'start') {
732 paddingTop = 0;
733 paddingBottom = first.height;
734 } else if (align === 'end') {
735 paddingTop = last.height;
736 paddingBottom = 0;
737 }
738
739 this.paddingTop = paddingTop + padding;
740 this.paddingBottom = paddingBottom + padding;
741 }
742 }
743
744 /**
745 * Handle margins and padding interactions

Callers 1

fitMethod · 0.95

Calls 2

isHorizontalMethod · 0.95
getPixelForTickMethod · 0.95

Tested by

no test coverage detected