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

Method calculateLabelRotation

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

Source from the content-addressed store, hash-verified

589 call(this.options.beforeCalculateLabelRotation, [this]);
590 }
591 calculateLabelRotation() {
592 const options = this.options;
593 const tickOpts = options.ticks;
594 const numTicks = getTicksLimit(this.ticks.length, options.ticks.maxTicksLimit);
595 const minRotation = tickOpts.minRotation || 0;
596 const maxRotation = tickOpts.maxRotation;
597 let labelRotation = minRotation;
598 let tickWidth, maxHeight, maxLabelDiagonal;
599
600 if (!this._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !this.isHorizontal()) {
601 this.labelRotation = minRotation;
602 return;
603 }
604
605 const labelSizes = this._getLabelSizes();
606 const maxLabelWidth = labelSizes.widest.width;
607 const maxLabelHeight = labelSizes.highest.height;
608
609 // Estimate the width of each grid based on the canvas width, the maximum
610 // label width and the number of tick intervals
611 const maxWidth = _limitValue(this.chart.width - maxLabelWidth, 0, this.maxWidth);
612 tickWidth = options.offset ? this.maxWidth / numTicks : maxWidth / (numTicks - 1);
613
614 // Allow 3 pixels x2 padding either side for label readability
615 if (maxLabelWidth + 6 > tickWidth) {
616 tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1));
617 maxHeight = this.maxHeight - getTickMarkLength(options.grid)
618 - tickOpts.padding - getTitleHeight(options.title, this.chart.options.font);
619 maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
620 labelRotation = toDegrees(Math.min(
621 Math.asin(_limitValue((labelSizes.highest.height + 6) / tickWidth, -1, 1)),
622 Math.asin(_limitValue(maxHeight / maxLabelDiagonal, -1, 1)) - Math.asin(_limitValue(maxLabelHeight / maxLabelDiagonal, -1, 1))
623 ));
624 labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation));
625 }
626
627 this.labelRotation = labelRotation;
628 }
629 afterCalculateLabelRotation() {
630 call(this.options.afterCalculateLabelRotation, [this]);
631 }

Callers 1

updateMethod · 0.95

Calls 8

_isVisibleMethod · 0.95
isHorizontalMethod · 0.95
_getLabelSizesMethod · 0.95
toDegreesFunction · 0.90
getTicksLimitFunction · 0.85
_limitValueFunction · 0.85
getTickMarkLengthFunction · 0.85
getTitleHeightFunction · 0.85

Tested by

no test coverage detected