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

Method calculateLabelRotation

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

Source from the content-addressed store, hash-verified

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

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