@lobehub/charts
Version:
React modern charts components built on recharts
110 lines (104 loc) • 4.8 kB
JavaScript
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { defaultValueFormatter, isOnSeverSide } from "./index";
export var getTextWidth = function getTextWidth(text) {
if (isOnSeverSide) return 0;
// 创建临时 span 元素
var span = document.createElement('span');
// 设置样式使其不可见
span.style.fontSize = '12px';
span.style.position = 'absolute';
span.style.visibility = 'hidden';
span.style.whiteSpace = 'nowrap';
// 设置文本内容
span.textContent = text;
// 添加到 body
document.body.append(span);
// 获取宽度
var width = span.getBoundingClientRect().width;
// 移除临时元素
span.remove();
return width;
};
export var getMaxLabelLength = function getMaxLabelLength(_ref) {
var data = _ref.data,
_ref$valueFormatter = _ref.valueFormatter,
valueFormatter = _ref$valueFormatter === void 0 ? defaultValueFormatter : _ref$valueFormatter,
index = _ref.index,
_ref$layout = _ref.layout,
layout = _ref$layout === void 0 ? 'horizontal' : _ref$layout,
_ref$margin = _ref.margin,
margin = _ref$margin === void 0 ? 24 : _ref$margin,
isScatterChart = _ref.isScatterChart;
var maxLength = 0;
var maxLabel = '';
if (isScatterChart) {
var _iterator = _createForOfIteratorHelper(data),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var item = _step.value;
var formattedValue = valueFormatter(item[index]);
if (formattedValue.length > maxLength) {
maxLength = formattedValue.length;
maxLabel = formattedValue;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} else if (layout === 'vertical') {
var _iterator2 = _createForOfIteratorHelper(data),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _item = _step2.value;
if (_item[index].length > maxLength) {
maxLength = _item[index].length;
maxLabel = _item[index];
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
} else {
var _iterator3 = _createForOfIteratorHelper(data),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _item2 = _step3.value;
var categories = Object.keys(_item2).filter(function (key) {
return key !== index;
});
var _iterator4 = _createForOfIteratorHelper(categories),
_step4;
try {
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
var category = _step4.value;
var value = _item2[category];
if (value === undefined) continue; // Skip null or undefined values
var _formattedValue = valueFormatter(value);
if (_formattedValue.length > maxLength) {
maxLength = _formattedValue.length;
maxLabel = _formattedValue;
}
}
} catch (err) {
_iterator4.e(err);
} finally {
_iterator4.f();
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}
return getTextWidth(maxLabel) + margin;
};