onesight-charts 1.4.5 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -63,8 +63,12 @@ function StandardCustomLine(props) {
63
63
  yAxisScale = true,
64
64
  // Y轴是否不从0开始,根据数据范围自动调整
65
65
  legendDropdownRight = -98,
66
- showAllLegends = false
66
+ showAllLegends = false,
67
67
  // 是否显示所有图例
68
+ legendNameMaxWidth = 120,
69
+ // legend 名称最大宽度(像素值),用于文本截断
70
+ tooltipNameMaxWidth = 140
71
+ // tooltip 名称最大宽度(像素值),用于文本截断
68
72
  } = props;
69
73
  const chartRef = (0, import_react.useRef)(null);
70
74
  const prevDepsRef = (0, import_react.useRef)(null);
@@ -288,8 +292,10 @@ function StandardCustomLine(props) {
288
292
  // 传入映射,使 tooltip 显示原始 name
289
293
  xAxis,
290
294
  // 传入原始 xAxis 数据
291
- tooltipYOffset
295
+ tooltipYOffset,
292
296
  // y 方向偏移量
297
+ tooltipNameMaxWidth
298
+ // tooltip 名称最大宽度
293
299
  });
294
300
  const nameToIndexMap = {};
295
301
  legendData.forEach((item, index) => {
@@ -360,8 +366,16 @@ function StandardCustomLine(props) {
360
366
  const index = nameToIndexMap[name];
361
367
  const iconKey = platformIcon && index !== void 0 ? `icon${index}` : "";
362
368
  const iconPrefix = iconKey ? `{${iconKey}|} ` : "";
363
- return legendValue ? `${iconPrefix}{labelName|${showName}}
364
- {labelMark|${value}}` : `${iconPrefix}{labelName|${showName}}`;
369
+ const ellipsisName = (0, import_chartUtils.canvasEllipsisMiddle)(
370
+ showName,
371
+ legendNameMaxWidth,
372
+ {
373
+ fontSize: 12,
374
+ fontWeight: 500
375
+ }
376
+ );
377
+ return legendValue ? `${iconPrefix}{labelName|${ellipsisName}}
378
+ {labelMark|${value}}` : `${iconPrefix}{labelName|${ellipsisName}}`;
365
379
  },
366
380
  textStyle: {
367
381
  fontWeight: 500,
@@ -60,8 +60,10 @@ var createCustomTooltip = ({
60
60
  // 新增:uniqueKey 到原始 name 的映射
61
61
  xAxis,
62
62
  // 新增:原始的 xAxis 数据
63
- tooltipYOffset = -4
63
+ tooltipYOffset = -4,
64
64
  // y 方向偏移量,默认 14px
65
+ tooltipNameMaxWidth = 140
66
+ // tooltip 名称最大宽度(像素值),用于文本截断
65
67
  }) => ({
66
68
  className: `onesight-line-tooltip ${tooltipClassName}`,
67
69
  trigger: "axis",
@@ -158,7 +160,14 @@ var createCustomTooltip = ({
158
160
  <div class='l'>
159
161
  <span class='l-color' style="background-color:${item.color}"></span>
160
162
  <span>
161
- ${item.seriesName}
163
+ ${(0, import_chartUtils.canvasEllipsisMiddle)(
164
+ item.seriesName,
165
+ tooltipNameMaxWidth,
166
+ {
167
+ fontSize: 14,
168
+ fontWeight: 400
169
+ }
170
+ )}
162
171
  </span>
163
172
  </div>
164
173
  <div class='r'>
@@ -63,8 +63,12 @@ function StandardLine(props) {
63
63
  yAxisScale = true,
64
64
  // Y轴是否不从0开始,根据数据范围自动调整
65
65
  legendDropdownRight = -98,
66
- showAllLegends = false
66
+ showAllLegends = false,
67
67
  // 是否显示所有图例
68
+ legendNameMaxWidth = 120,
69
+ // legend 名称最大宽度(像素值),用于文本截断
70
+ tooltipNameMaxWidth = 140
71
+ // tooltip 名称最大宽度(像素值),用于文本截断
68
72
  } = props;
69
73
  const chartRef = (0, import_react.useRef)(null);
70
74
  const legendMoreBtnRef = (0, import_react.useRef)(null);
@@ -249,8 +253,10 @@ function StandardLine(props) {
249
253
  isTotalShow,
250
254
  xAxis,
251
255
  // 传入原始 xAxis 数据
252
- tooltipYOffset
256
+ tooltipYOffset,
253
257
  // y 方向偏移量
258
+ tooltipNameMaxWidth
259
+ // tooltip 名称最大宽度
254
260
  });
255
261
  let option = {
256
262
  legend: {
@@ -271,15 +277,12 @@ function StandardLine(props) {
271
277
  } else if (sumData) {
272
278
  value = (0, import_chartUtils.numberFormatMust)(sumData);
273
279
  }
274
- let showName = name;
275
- if (dealData.length >= 4) {
276
- const strLen = 15;
277
- if (showName.length > strLen) {
278
- showName = showName.substring(0, strLen) + "...";
279
- }
280
- }
281
- return legendValue ? `{labelName|${showName}}
282
- {labelMark|${value}}` : `{labelName|${showName}}`;
280
+ const ellipsisName = (0, import_chartUtils.canvasEllipsisMiddle)(name, legendNameMaxWidth, {
281
+ fontSize: 12,
282
+ fontWeight: 500
283
+ });
284
+ return legendValue ? `{labelName|${ellipsisName}}
285
+ {labelMark|${value}}` : `{labelName|${ellipsisName}}`;
283
286
  },
284
287
  textStyle: {
285
288
  fontWeight: 500,
@@ -58,8 +58,10 @@ var createCustomTooltip = ({
58
58
  isTotalShow,
59
59
  xAxis,
60
60
  // 新增:原始的 xAxis 数据
61
- tooltipYOffset = -4
61
+ tooltipYOffset = -4,
62
62
  // y 方向偏移量,默认 14px
63
+ tooltipNameMaxWidth = 140
64
+ // tooltip 名称最大宽度(像素值),用于文本截断
63
65
  }) => ({
64
66
  className: `onesight-line-tooltip ${tooltipClassName}`,
65
67
  trigger: "axis",
@@ -152,7 +154,14 @@ var createCustomTooltip = ({
152
154
  <div class='l'>
153
155
  <span class='l-color' style="background-color:${item.color}"></span>
154
156
  <span>
155
- ${item.seriesName}
157
+ ${(0, import_chartUtils.canvasEllipsisMiddle)(
158
+ item.seriesName,
159
+ tooltipNameMaxWidth,
160
+ {
161
+ fontSize: 14,
162
+ fontWeight: 400
163
+ }
164
+ )}
156
165
  </span>
157
166
  </div>
158
167
  <div class='r'>
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  var chartUtils_exports = {};
21
21
  __export(chartUtils_exports, {
22
22
  calculateSeriesTotal: () => calculateSeriesTotal,
23
+ canvasEllipsisMiddle: () => canvasEllipsisMiddle,
23
24
  changeDataTypeEn: () => changeDataTypeEn,
24
25
  computeFloat: () => computeFloat,
25
26
  computeFloatNull: () => computeFloatNull,
@@ -2239,9 +2240,79 @@ var mapEchartsDict = {
2239
2240
  function numberFormat(num, fixedNum = 1) {
2240
2241
  return numberFormatMust(num, fixedNum);
2241
2242
  }
2243
+ function canvasGetTextWidth(str, style = {}) {
2244
+ if (!str)
2245
+ return 0;
2246
+ const { fontSize = 12, fontWeight = 500, fontFamily = "sans-serif" } = style;
2247
+ const canvas = document.createElement("canvas");
2248
+ const ctx = canvas.getContext("2d");
2249
+ ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
2250
+ return Math.ceil(ctx.measureText(str).width);
2251
+ }
2252
+ function canvasEllipsisMiddle(str, maxWidth, style = {}) {
2253
+ if (!str)
2254
+ return "";
2255
+ if (canvasGetTextWidth(str, style) <= maxWidth)
2256
+ return str;
2257
+ const ellipsis = "...";
2258
+ let left = 1;
2259
+ let testWidth = 0;
2260
+ let arr = [];
2261
+ while (testWidth < maxWidth) {
2262
+ const leftPart = str.slice(0, left);
2263
+ const rightPart = str.slice(-left);
2264
+ const testStr = leftPart + ellipsis + rightPart;
2265
+ testWidth = canvasGetTextWidth(testStr, style);
2266
+ arr.push({
2267
+ left,
2268
+ testWidth
2269
+ });
2270
+ left++;
2271
+ }
2272
+ let T = 0;
2273
+ for (let i = arr.length - 1; i >= 0; i--) {
2274
+ if (arr[i].testWidth < maxWidth) {
2275
+ T = arr[i].left;
2276
+ break;
2277
+ }
2278
+ }
2279
+ if (T) {
2280
+ return `${str.slice(0, T)}${ellipsis}${str.slice(-T)}`;
2281
+ } else {
2282
+ return "";
2283
+ }
2284
+ }
2285
+ var TextMeasure = (() => {
2286
+ const span = document.createElement("span");
2287
+ span.style.position = "fixed";
2288
+ span.style.visibility = "hidden";
2289
+ span.style.whiteSpace = "nowrap";
2290
+ span.style.pointerEvents = "none";
2291
+ span.style.left = "-9999px";
2292
+ document.body.appendChild(span);
2293
+ function getWidth(str, style = {}) {
2294
+ if (!str)
2295
+ return 0;
2296
+ const {
2297
+ fontSize = 14,
2298
+ fontWeight = "normal",
2299
+ // fontFamily = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'",
2300
+ fontFamily = '"PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"',
2301
+ letterSpacing = 0
2302
+ } = style;
2303
+ span.style.fontSize = fontSize + "px";
2304
+ span.style.fontWeight = fontWeight;
2305
+ span.style.letterSpacing = letterSpacing + "px";
2306
+ span.style.fontFamily = fontFamily;
2307
+ span.textContent = str;
2308
+ return Math.ceil(span.offsetWidth) + 2;
2309
+ }
2310
+ return { getWidth };
2311
+ })();
2242
2312
  // Annotate the CommonJS export names for ESM import in node:
2243
2313
  0 && (module.exports = {
2244
2314
  calculateSeriesTotal,
2315
+ canvasEllipsisMiddle,
2245
2316
  changeDataTypeEn,
2246
2317
  computeFloat,
2247
2318
  computeFloatNull,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onesight-charts",
3
- "version": "1.4.5",
3
+ "version": "1.4.6",
4
4
  "private": false,
5
5
  "description": "OneSight前端图表公共组件库",
6
6
  "license": "MIT",