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

Method drawBody

src/plugins/plugin.tooltip.js:929–991  ·  view source on GitHub ↗
(pt, ctx, options)

Source from the content-addressed store, hash-verified

927 }
928
929 drawBody(pt, ctx, options) {
930 const {body} = this;
931 const {bodySpacing, bodyAlign, displayColors, boxHeight, boxWidth, boxPadding} = options;
932 const bodyFont = toFont(options.bodyFont);
933 let bodyLineHeight = bodyFont.lineHeight;
934 let xLinePadding = 0;
935
936 const rtlHelper = getRtlAdapter(options.rtl, this.x, this.width);
937
938 const fillLineOfText = function(line) {
939 ctx.fillText(line, rtlHelper.x(pt.x + xLinePadding), pt.y + bodyLineHeight / 2);
940 pt.y += bodyLineHeight + bodySpacing;
941 };
942
943 const bodyAlignForCalculation = rtlHelper.textAlign(bodyAlign);
944 let bodyItem, textColor, lines, i, j, ilen, jlen;
945
946 ctx.textAlign = bodyAlign;
947 ctx.textBaseline = 'middle';
948 ctx.font = bodyFont.string;
949
950 pt.x = getAlignedX(this, bodyAlignForCalculation, options);
951
952 // Before body lines
953 ctx.fillStyle = options.bodyColor;
954 each(this.beforeBody, fillLineOfText);
955
956 xLinePadding = displayColors && bodyAlignForCalculation !== 'right'
957 ? bodyAlign === 'center' ? (boxWidth / 2 + boxPadding) : (boxWidth + 2 + boxPadding)
958 : 0;
959
960 // Draw body lines now
961 for (i = 0, ilen = body.length; i < ilen; ++i) {
962 bodyItem = body[i];
963 textColor = this.labelTextColors[i];
964
965 ctx.fillStyle = textColor;
966 each(bodyItem.before, fillLineOfText);
967
968 lines = bodyItem.lines;
969 // Draw Legend-like boxes if needed
970 if (displayColors && lines.length) {
971 this._drawColorBox(ctx, pt, i, rtlHelper, options);
972 bodyLineHeight = Math.max(bodyFont.lineHeight, boxHeight);
973 }
974
975 for (j = 0, jlen = lines.length; j < jlen; ++j) {
976 fillLineOfText(lines[j]);
977 // Reset for any lines that don't include colorbox
978 bodyLineHeight = bodyFont.lineHeight;
979 }
980
981 each(bodyItem.after, fillLineOfText);
982 }
983
984 // Reset back to 0 for after body
985 xLinePadding = 0;
986 bodyLineHeight = bodyFont.lineHeight;

Callers 1

drawMethod · 0.95

Calls 6

_drawColorBoxMethod · 0.95
toFontFunction · 0.85
getRtlAdapterFunction · 0.85
getAlignedXFunction · 0.85
eachFunction · 0.85
textAlignMethod · 0.80

Tested by

no test coverage detected