MCPcopy Index your code
hub / github.com/plotly/plotly.js / scaleTextForRoundedBar

Function scaleTextForRoundedBar

src/traces/bar/plot.js:859–921  ·  view source on GitHub ↗
(x0, x1, y0, y1, t, r, overhead, isHorizontal, hasB)

Source from the content-addressed store, hash-verified

857}
858
859function scaleTextForRoundedBar(x0, x1, y0, y1, t, r, overhead, isHorizontal, hasB) {
860 var barWidth = Math.max(0, Math.abs(x1 - x0) - 2 * TEXTPAD);
861 var barHeight = Math.max(0, Math.abs(y1 - y0) - 2 * TEXTPAD);
862 var R = r - TEXTPAD;
863 var clippedR = overhead ? R - Math.sqrt(R * R - (R - overhead) * (R - overhead)) : R;
864 var rX = hasB ? R * 2 : isHorizontal ? R - overhead : 2 * clippedR;
865 var rY = hasB ? R * 2 : isHorizontal ? 2 * clippedR : R - overhead;
866 var a, b, c;
867 var scale, pad;
868
869 if (t.y / t.x >= barHeight / (barWidth - rX)) {
870 // Case 1 (Tall text)
871 scale = barHeight / t.y;
872 } else if (t.y / t.x <= (barHeight - rY) / barWidth) {
873 // Case 2 (Wide text)
874 scale = barWidth / t.x;
875 } else if (!hasB && isHorizontal) {
876 // Case 3a (Quadratic case, two side corners are rounded)
877 a = t.x * t.x + (t.y * t.y) / 4;
878 b = -2 * t.x * (barWidth - R) - t.y * (barHeight / 2 - R);
879 c = (barWidth - R) * (barWidth - R) + (barHeight / 2 - R) * (barHeight / 2 - R) - R * R;
880
881 scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
882 } else if (!hasB) {
883 // Case 3b (Quadratic case, two top/bottom corners are rounded)
884 a = (t.x * t.x) / 4 + t.y * t.y;
885 b = -t.x * (barWidth / 2 - R) - 2 * t.y * (barHeight - R);
886 c = (barWidth / 2 - R) * (barWidth / 2 - R) + (barHeight - R) * (barHeight - R) - R * R;
887
888 scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
889 } else {
890 // Case 4 (Quadratic case, all four corners are rounded)
891 a = (t.x * t.x + t.y * t.y) / 4;
892 b = -t.x * (barWidth / 2 - R) - t.y * (barHeight / 2 - R);
893 c = (barWidth / 2 - R) * (barWidth / 2 - R) + (barHeight / 2 - R) * (barHeight / 2 - R) - R * R;
894 scale = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
895 }
896
897 // Scale should not be larger than 1
898 scale = Math.min(1, scale);
899
900 if (isHorizontal) {
901 pad = Math.max(
902 0,
903 R -
904 Math.sqrt(
905 Math.max(0, R * R - (R - (barHeight - t.y * scale) / 2) * (R - (barHeight - t.y * scale) / 2))
906 ) -
907 overhead
908 );
909 } else {
910 pad = Math.max(
911 0,
912 R -
913 Math.sqrt(
914 Math.max(0, R * R - (R - (barWidth - t.x * scale) / 2) * (R - (barWidth - t.x * scale) / 2))
915 ) -
916 overhead

Callers 1

toMoveInsideBarFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…