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

Method _computeLabelSizes

src/core/core.scale.js:827–878  ·  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

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