MCPcopy Index your code
hub / github.com/microsoft/SandDance / placeMarkLabel

Function placeMarkLabel

docs/app/js/sanddance-app.js:138998–139040  ·  view source on GitHub ↗
($, bitmaps, anchors, offsets)

Source from the content-addressed store, hash-verified

138996 "top"
138997];
138998function placeMarkLabel($, bitmaps, anchors, offsets) {
138999 const width = $.width, height = $.height, bm0 = bitmaps[0], bm1 = bitmaps[1], n = offsets.length;
139000 return function(d) {
139001 const boundary = d.boundary, textHeight = d.datum.fontSize; // can not be placed if the mark is not visible in the graph bound
139002 if (boundary[2] < 0 || boundary[5] < 0 || boundary[0] > width || boundary[3] > height) return false;
139003 let textWidth = 0, dx, dy, isInside, sizeFactor, insideFactor, x1, x2, y1, y2, xc, yc, _x1, _x2, _y1, _y2; // for each anchor and offset
139004 for(let i7 = 0; i7 < n; ++i7){
139005 dx = (anchors[i7] & 0x3) - 1;
139006 dy = (anchors[i7] >>> 0x2 & 0x3) - 1;
139007 isInside = dx === 0 && dy === 0 || offsets[i7] < 0;
139008 sizeFactor = dx && dy ? Math.SQRT1_2 : 1;
139009 insideFactor = offsets[i7] < 0 ? -1 : 1;
139010 x1 = boundary[1 + dx] + offsets[i7] * dx * sizeFactor;
139011 yc = boundary[4 + dy] + insideFactor * textHeight * dy / 2 + offsets[i7] * dy * sizeFactor;
139012 y1 = yc - textHeight / 2;
139013 y2 = yc + textHeight / 2;
139014 _x1 = $(x1);
139015 _y1 = $(y1);
139016 _y2 = $(y2);
139017 if (!textWidth) {
139018 // to avoid finding width of text label,
139019 if (!test(_x1, _x1, _y1, _y2, bm0, bm1, x1, x1, y1, y2, boundary, isInside)) continue;
139020 else // Otherwise, find the label width
139021 textWidth = (0, _vegaScenegraph.textMetrics).width(d.datum, d.datum.text);
139022 }
139023 xc = x1 + insideFactor * textWidth * dx / 2;
139024 x1 = xc - textWidth / 2;
139025 x2 = xc + textWidth / 2;
139026 _x1 = $(x1);
139027 _x2 = $(x2);
139028 if (test(_x1, _x2, _y1, _y2, bm0, bm1, x1, x2, y1, y2, boundary, isInside)) {
139029 // place label if the position is placeable
139030 d.x = !dx ? xc : dx * insideFactor < 0 ? x2 : x1;
139031 d.y = !dy ? yc : dy * insideFactor < 0 ? y2 : y1;
139032 d.align = Aligns[dx * insideFactor + 1];
139033 d.baseline = Baselines[dy * insideFactor + 1];
139034 bm0.setRange(_x1, _y1, _x2, _y2);
139035 return true;
139036 }
139037 }
139038 return false;
139039 };
139040} // Test if a label with the given dimensions can be added without overlap
139041function test(_x1, _x2, _y1, _y2, bm0, bm1, x1, x2, y1, y2, boundary, isInside) {
139042 return !(bm0.outOfBounds(_x1, _y1, _x2, _y2) || (isInside && bm1 ? bm1.getRange(_x1, _y1, _x2, _y2) || !isInMarkBound(x1, y1, x2, y2, boundary) : bm0.getRange(_x1, _y1, _x2, _y2)));
139043}

Callers 1

labelLayoutFunction · 0.70

Calls 2

$Function · 0.70
testFunction · 0.70

Tested by

no test coverage detected