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

Method _computeLabelSizes

src/core/core.scale.js:824–875  ·  view source on GitHub ↗

* Returns {width, height, offset} objects for the first, last, widest, highest tick * labels where offset indicates the anchor point offset from the top in pixels. * @return {{ first: object, last: object, widest: object, highest: object, widths: Array, heights: array }} * @private

(ticks, length, maxTicksLimit)

Source from the content-addressed store, hash-verified

822 * @private
823 */
824 _computeLabelSizes(ticks, length, maxTicksLimit) {
825 const {ctx, _longestTextCache: caches} = this;
826 const widths = [];
827 const heights = [];
828 const increment = Math.floor(length / getTicksLimit(length, maxTicksLimit));
829 let widestLabelSize = 0;
830 let highestLabelSize = 0;
831 let i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel;
832
833 for (i = 0; i < length; i += increment) {
834 label = ticks[i].label;
835 tickFont = this._resolveTickFontOptions(i);
836 ctx.font = fontString = tickFont.string;
837 cache = caches[fontString] = caches[fontString] || {data: {}, gc: []};
838 lineHeight = tickFont.lineHeight;
839 width = height = 0;
840 // Undefined labels and arrays should not be measured
841 if (!isNullOrUndef(label) && !isArray(label)) {
842 width = _measureText(ctx, cache.data, cache.gc, width, label);
843 height = lineHeight;
844 } else if (isArray(label)) {
845 // if it is an array let's measure each element
846 for (j = 0, jlen = label.length; j < jlen; ++j) {
847 nestedLabel = /** @type {string} */ (label[j]);
848 // Undefined labels and arrays should not be measured
849 if (!isNullOrUndef(nestedLabel) && !isArray(nestedLabel)) {
850 width = _measureText(ctx, cache.data, cache.gc, width, nestedLabel);
851 height += lineHeight;
852 }
853 }
854 }
855 widths.push(width);
856 heights.push(height);
857 widestLabelSize = Math.max(width, widestLabelSize);
858 highestLabelSize = Math.max(height, highestLabelSize);
859 }
860 garbageCollect(caches, length);
861
862 const widest = widths.indexOf(widestLabelSize);
863 const highest = heights.indexOf(highestLabelSize);
864
865 const valueAt = (idx) => ({width: widths[idx] || 0, height: heights[idx] || 0});
866
867 return {
868 first: valueAt(0),
869 last: valueAt(length - 1),
870 widest: valueAt(widest),
871 highest: valueAt(highest),
872 widths,
873 heights,
874 };
875 }
876
877 /**
878 * Used to get the label to display in the tooltip for the given value

Callers 1

_getLabelSizesMethod · 0.95

Calls 6

getTicksLimitFunction · 0.85
isNullOrUndefFunction · 0.85
isArrayFunction · 0.85
_measureTextFunction · 0.85
garbageCollectFunction · 0.85

Tested by

no test coverage detected