{"version":3,"file":"getMaxLabelLength.mjs","names":[],"sources":["../../src/utils/getMaxLabelLength.ts"],"sourcesContent":["import { ValueFormatter } from '@/types/charts';\nimport { defaultValueFormatter, isOnSeverSide } from '@/utils/index';\n\nexport const getTextWidth = (text: string) => {\n  if (isOnSeverSide) return 0;\n\n  // 创建临时 span 元素\n  const span = document.createElement('span');\n\n  // 设置样式使其不可见\n  span.style.fontSize = '12px';\n  span.style.position = 'absolute';\n  span.style.visibility = 'hidden';\n  span.style.whiteSpace = 'nowrap';\n\n  // 设置文本内容\n  span.textContent = text;\n\n  // 添加到 body\n  document.body.append(span);\n\n  // 获取宽度\n  const width = span.getBoundingClientRect().width;\n\n  // 移除临时元素\n  span.remove();\n\n  return width;\n};\n\nexport const getMaxLabelLength = ({\n  data,\n  valueFormatter = defaultValueFormatter,\n  index,\n  layout = 'horizontal',\n  margin = 24,\n  isScatterChart,\n}: {\n  data: Array<Record<string, any>>;\n  index: string;\n  isScatterChart?: boolean;\n  layout?: 'vertical' | 'horizontal';\n  margin?: number;\n  valueFormatter?: ValueFormatter;\n}): number => {\n  let maxLength = 0;\n  let maxLabel = '';\n\n  if (isScatterChart) {\n    for (const item of data) {\n      const formattedValue = valueFormatter(item[index]);\n      if (formattedValue.length > maxLength) {\n        maxLength = formattedValue.length;\n        maxLabel = formattedValue;\n      }\n    }\n  } else if (layout === 'vertical') {\n    for (const item of data) {\n      if (item[index].length > maxLength) {\n        maxLength = item[index].length;\n        maxLabel = item[index];\n      }\n    }\n  } else {\n    for (const item of data) {\n      const categories = Object.keys(item).filter((key) => key !== index);\n      for (const category of categories) {\n        const value = item[category];\n        if (value === undefined) continue; // Skip null or undefined values\n        const formattedValue = valueFormatter(value);\n        if (formattedValue.length > maxLength) {\n          maxLength = formattedValue.length;\n          maxLabel = formattedValue;\n        }\n      }\n    }\n  }\n\n  return getTextWidth(maxLabel) + margin;\n};\n"],"mappings":";;AAGA,MAAa,gBAAgB,SAAiB;CAC5C,IAAI,eAAe,OAAO;CAG1B,MAAM,OAAO,SAAS,cAAc,MAAM;CAG1C,KAAK,MAAM,WAAW;CACtB,KAAK,MAAM,WAAW;CACtB,KAAK,MAAM,aAAa;CACxB,KAAK,MAAM,aAAa;CAGxB,KAAK,cAAc;CAGnB,SAAS,KAAK,OAAO,IAAI;CAGzB,MAAM,QAAQ,KAAK,sBAAsB,CAAC,CAAC;CAG3C,KAAK,OAAO;CAEZ,OAAO;AACT;AAEA,MAAa,qBAAqB,EAChC,MACA,iBAAiB,uBACjB,OACA,SAAS,cACT,SAAS,IACT,qBAQY;CACZ,IAAI,YAAY;CAChB,IAAI,WAAW;CAEf,IAAI,gBACF,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,iBAAiB,eAAe,KAAK,MAAM;EACjD,IAAI,eAAe,SAAS,WAAW;GACrC,YAAY,eAAe;GAC3B,WAAW;EACb;CACF;MACK,IAAI,WAAW,YACf;OAAA,MAAM,QAAQ,MACjB,IAAI,KAAK,MAAM,CAAC,SAAS,WAAW;GAClC,YAAY,KAAK,MAAM,CAAC;GACxB,WAAW,KAAK;EAClB;QAGF,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,aAAa,OAAO,KAAK,IAAI,CAAC,CAAC,QAAQ,QAAQ,QAAQ,KAAK;EAClE,KAAK,MAAM,YAAY,YAAY;GACjC,MAAM,QAAQ,KAAK;GACnB,IAAI,UAAU,KAAA,GAAW;GACzB,MAAM,iBAAiB,eAAe,KAAK;GAC3C,IAAI,eAAe,SAAS,WAAW;IACrC,YAAY,eAAe;IAC3B,WAAW;GACb;EACF;CACF;CAGF,OAAO,aAAa,QAAQ,IAAI;AAClC"}