@easyv/charts 1.9.3 → 1.9.4

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.
@@ -65,6 +65,7 @@ var pieLegendFormatter = exports.pieLegendFormatter = function pieLegendFormatte
65
65
  if (showName) columns.push("".concat(props.nameMaxWidth, "px"));
66
66
  if (showValue) columns.push("".concat(props.valueMaxWidth + valueGap, "px"));
67
67
  if (showPercent) columns.push("".concat(props.percentMaxWidth + percentGap, "px"));
68
+ columns[columns.length - 1] = "auto";
68
69
  return /*#__PURE__*/React.createElement(React.Fragment, null, icon && /*#__PURE__*/React.createElement("span", {
69
70
  style: _objectSpread(_objectSpread({}, icon), {}, {
70
71
  marginRight: icon.marginRight,
@@ -96,6 +97,7 @@ var pieLegendFormatter = exports.pieLegendFormatter = function pieLegendFormatte
96
97
  alignItems: 'center',
97
98
  justifyContent: valueAlign == "left" ? "flex-start" : valueAlign == "right" ? "flex-end" : 'center',
98
99
  textAlign: valueAlign === "left" ? "left" : valueAlign === "right" ? "right" : "center"
100
+ //------------
99
101
  })
100
102
  }, /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement(_components.SplitText, {
101
103
  value: data.y,
@@ -4,8 +4,9 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
+ exports.SvgBackground = void 0;
7
8
  exports.band = band;
8
- exports.sortPie = exports.seriesYOrZ = exports.resetStacks = exports.reduceConfig = exports.mathR = exports.isValidHttpUrl = exports.identity = exports.getTranslate3d = exports.getTranslate2d = exports.getTicksOfAxis = exports.getTickCoord = exports.getStacks = exports.getSeriesInfo = exports.getMultiColorStr = exports.getMousePos = exports.getMargin = exports.getIcon = exports.getGridCoord = exports.getFontStyle = exports.getDomPath = exports.getDataWithPercent = exports.getCurrentStack = exports.getColorList = exports.getChildren = exports.getBreakWord = exports.getBandwidth = exports.getBandSeriesStepAndWidth = exports.getBandBackground = exports.formatFont = exports.filterChildren = exports.dateFormat = exports.dataYOrZ = void 0;
9
+ exports.sortPie = exports.seriesYOrZ = exports.resetStacks = exports.renderCanvasBackground = exports.reduceConfig = exports.mathR = exports.isValidHttpUrl = exports.identity = exports.getTranslate3d = exports.getTranslate2d = exports.getTicksOfAxis = exports.getTickCoord = exports.getStacks = exports.getSeriesInfo = exports.getMultiColorStr = exports.getMousePos = exports.getMargin = exports.getIcon = exports.getGridCoord = exports.getFontStyle = exports.getDomPath = exports.getDataWithPercent = exports.getCurrentStack = exports.getColorList = exports.getChildren = exports.getBreakWord = exports.getBandwidth = exports.getBandSeriesStepAndWidth = exports.getBandBackground = exports.formatFont = exports.filterChildren = exports.dateFormat = exports.dataYOrZ = void 0;
9
10
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
11
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
11
12
  var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
@@ -27,7 +28,7 @@ var defaultLineIcon = {
27
28
  height: 2,
28
29
  background: defaultBackground
29
30
  };
30
- var SvgBackground = function SvgBackground(_ref) {
31
+ var renderCanvasBackground = exports.renderCanvasBackground = function renderCanvasBackground(_ref) {
31
32
  var _ref$fill = _ref.fill,
32
33
  type = _ref$fill.type,
33
34
  pure = _ref$fill.pure,
@@ -46,6 +47,83 @@ var SvgBackground = function SvgBackground(_ref) {
46
47
  stroke = _ref$pattern$boderCol === void 0 ? 'transparent' : _ref$pattern$boderCol,
47
48
  _ref$pattern$boderWid = _ref$pattern.boderWidth,
48
49
  boderWidth = _ref$pattern$boderWid === void 0 ? 0 : _ref$pattern$boderWid;
50
+ // 处理宽高(支持百分比/数字)
51
+ var canvasWidth = width === '100%' ? 200 : Number(width) || 200; // 百分比默认200,可根据实际场景调整
52
+ var canvasHeight = height === '100%' ? 200 : Number(height) || 200;
53
+
54
+ // 创建离屏 Canvas
55
+ var canvas = document.createElement('canvas');
56
+ var ctx = canvas.getContext('2d');
57
+ canvas.width = canvasWidth;
58
+ canvas.height = canvasHeight;
59
+
60
+ // 清空画布
61
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);
62
+
63
+ // 1. 解析 SVG Path 为 Canvas Path2D
64
+ var path2D = new Path2D(path);
65
+
66
+ // 2. 处理填充(纯色/渐变)
67
+ if (type === 'pure') {
68
+ ctx.fillStyle = pure;
69
+ } else {
70
+ // 线性渐变(对应原 SVG linearGradient)
71
+ var centerX = canvasWidth / 2;
72
+ var centerY = canvasHeight / 2;
73
+ var rad = (angle + 180) * Math.PI / 180; // 旋转角度转弧度
74
+ var gradientLength = Math.hypot(canvasWidth, canvasHeight); // 渐变长度取对角线
75
+
76
+ // 计算渐变起点/终点(匹配 SVG rotate 效果)
77
+ var startX = centerX - Math.sin(rad) * gradientLength;
78
+ var startY = centerY - Math.cos(rad) * gradientLength;
79
+ var endX = centerX + Math.sin(rad) * gradientLength;
80
+ var endY = centerY + Math.cos(rad) * gradientLength;
81
+
82
+ // 创建渐变
83
+ var gradient = ctx.createLinearGradient(startX, startY, endX, endY);
84
+ stops.forEach(function (_ref2) {
85
+ var offset = _ref2.offset,
86
+ color = _ref2.color;
87
+ gradient.addColorStop(offset / 100, color);
88
+ gradient.opacity = opacity; // 透明度
89
+ });
90
+ ctx.fillStyle = gradient;
91
+ }
92
+
93
+ // 3. 处理描边
94
+ ctx.strokeStyle = stroke;
95
+ ctx.lineWidth = boderWidth;
96
+
97
+ // 4. 绘制路径
98
+ ctx.fill(path2D);
99
+ if (boderWidth > 0) {
100
+ ctx.stroke(path2D);
101
+ }
102
+
103
+ // 5. 转换为 DataURL(替代 SVG DataURL)
104
+ return canvas.toDataURL('image/png');
105
+ };
106
+
107
+ // 原 SvgBackground 组件保留(兼容旧逻辑,也可删除)
108
+ var SvgBackground = exports.SvgBackground = function SvgBackground(_ref3) {
109
+ var _ref3$fill = _ref3.fill,
110
+ type = _ref3$fill.type,
111
+ pure = _ref3$fill.pure,
112
+ _ref3$fill$linear = _ref3$fill.linear,
113
+ angle = _ref3$fill$linear.angle,
114
+ opacity = _ref3$fill$linear.opacity,
115
+ stops = _ref3$fill$linear.stops,
116
+ _ref3$pattern = _ref3.pattern,
117
+ _ref3$pattern$path = _ref3$pattern.path,
118
+ path = _ref3$pattern$path === void 0 ? '' : _ref3$pattern$path,
119
+ _ref3$pattern$width = _ref3$pattern.width,
120
+ width = _ref3$pattern$width === void 0 ? '100%' : _ref3$pattern$width,
121
+ _ref3$pattern$height = _ref3$pattern.height,
122
+ height = _ref3$pattern$height === void 0 ? '100%' : _ref3$pattern$height,
123
+ _ref3$pattern$boderCo = _ref3$pattern.boderColor,
124
+ stroke = _ref3$pattern$boderCo === void 0 ? 'transparent' : _ref3$pattern$boderCo,
125
+ _ref3$pattern$boderWi = _ref3$pattern.boderWidth,
126
+ boderWidth = _ref3$pattern$boderWi === void 0 ? 0 : _ref3$pattern$boderWi;
49
127
  return /*#__PURE__*/React.createElement("svg", {
50
128
  preserveAspectRatio: "none",
51
129
  xmlns: "http://www.w3.org/2000/svg",
@@ -58,9 +136,9 @@ var SvgBackground = function SvgBackground(_ref) {
58
136
  x2: "0%",
59
137
  y2: "100%",
60
138
  gradientTransform: 'rotate(' + (angle + 180) + ', 0.5, 0.5)'
61
- }, stops.map(function (_ref2, index) {
62
- var offset = _ref2.offset,
63
- color = _ref2.color;
139
+ }, stops.map(function (_ref4, index) {
140
+ var offset = _ref4.offset,
141
+ color = _ref4.color;
64
142
  return /*#__PURE__*/React.createElement("stop", {
65
143
  key: index,
66
144
  offset: offset + '%',
@@ -74,13 +152,15 @@ var SvgBackground = function SvgBackground(_ref) {
74
152
  strokeWidth: boderWidth
75
153
  }));
76
154
  };
77
- var getColorList = exports.getColorList = function getColorList(_ref3) {
78
- var type = _ref3.type,
79
- pure = _ref3.pure,
80
- _ref3$linear = _ref3.linear,
81
- stops = _ref3$linear.stops,
82
- angle = _ref3$linear.angle,
83
- opacity = _ref3$linear.opacity;
155
+
156
+ // ===================== 原有方法(仅改造 getBandBackground) =====================
157
+ var getColorList = exports.getColorList = function getColorList(_ref5) {
158
+ var type = _ref5.type,
159
+ pure = _ref5.pure,
160
+ _ref5$linear = _ref5.linear,
161
+ stops = _ref5$linear.stops,
162
+ angle = _ref5$linear.angle,
163
+ opacity = _ref5$linear.opacity;
84
164
  if (type == 'pure') {
85
165
  return [{
86
166
  color: pure,
@@ -90,9 +170,9 @@ var getColorList = exports.getColorList = function getColorList(_ref3) {
90
170
  offset: 0
91
171
  }];
92
172
  }
93
- return stops.map(function (_ref4) {
94
- var color = _ref4.color,
95
- offset = _ref4.offset;
173
+ return stops.map(function (_ref6) {
174
+ var color = _ref6.color,
175
+ offset = _ref6.offset;
96
176
  return {
97
177
  color: color,
98
178
  offset: offset / 100
@@ -114,7 +194,7 @@ var getMultiColorStr = exports.getMultiColorStr = function getMultiColorStr(colo
114
194
  offset = _stops_$i.offset;
115
195
  result += "".concat(color, " ").concat(offset, "%,");
116
196
  }
117
- return result.slice(0, -1) + ")";
197
+ return result.slice(0, -1) + ')';
118
198
  }
119
199
  };
120
200
  var getIcon = exports.getIcon = function getIcon(type, icon) {
@@ -201,15 +281,15 @@ var getTicksOfAxis = exports.getTicksOfAxis = function getTicksOfAxis(domain, ti
201
281
  }
202
282
  return ticksArr;
203
283
  };
204
- var getTickCoord = exports.getTickCoord = function getTickCoord(_ref5) {
205
- var orientation = _ref5.orientation,
206
- coordinate = _ref5.coordinate,
207
- _ref5$tickSize = _ref5.tickSize,
208
- tickSize = _ref5$tickSize === void 0 ? 6 : _ref5$tickSize,
209
- _ref5$x = _ref5.x,
210
- x = _ref5$x === void 0 ? 0 : _ref5$x,
211
- _ref5$y = _ref5.y,
212
- y = _ref5$y === void 0 ? 0 : _ref5$y;
284
+ var getTickCoord = exports.getTickCoord = function getTickCoord(_ref7) {
285
+ var orientation = _ref7.orientation,
286
+ coordinate = _ref7.coordinate,
287
+ _ref7$tickSize = _ref7.tickSize,
288
+ tickSize = _ref7$tickSize === void 0 ? 6 : _ref7$tickSize,
289
+ _ref7$x = _ref7.x,
290
+ x = _ref7$x === void 0 ? 0 : _ref7$x,
291
+ _ref7$y = _ref7.y,
292
+ y = _ref7$y === void 0 ? 0 : _ref7$y;
213
293
  var x1, x2, y1, y2;
214
294
  switch (orientation) {
215
295
  case 'top':
@@ -240,10 +320,10 @@ var getTickCoord = exports.getTickCoord = function getTickCoord(_ref5) {
240
320
  y2: y2
241
321
  };
242
322
  };
243
- var getGridCoord = exports.getGridCoord = function getGridCoord(_ref6) {
244
- var orientation = _ref6.orientation,
245
- coordinate = _ref6.coordinate,
246
- end = _ref6.end;
323
+ var getGridCoord = exports.getGridCoord = function getGridCoord(_ref8) {
324
+ var orientation = _ref8.orientation,
325
+ coordinate = _ref8.coordinate,
326
+ end = _ref8.end;
247
327
  var x1, x2, y1, y2;
248
328
  switch (orientation) {
249
329
  case 'top':
@@ -288,14 +368,14 @@ var getMousePos = exports.getMousePos = function getMousePos(evt, dom) {
288
368
  h: rect.height
289
369
  };
290
370
  };
291
- var getFontStyle = exports.getFontStyle = function getFontStyle(_ref7, type) {
292
- var color = _ref7.color,
293
- bold = _ref7.bold,
294
- italic = _ref7.italic,
295
- fontSize = _ref7.fontSize,
296
- fontFamily = _ref7.fontFamily,
297
- letterSpacing = _ref7.letterSpacing,
298
- lineHeight = _ref7.lineHeight;
371
+ var getFontStyle = exports.getFontStyle = function getFontStyle(_ref9, type) {
372
+ var color = _ref9.color,
373
+ bold = _ref9.bold,
374
+ italic = _ref9.italic,
375
+ fontSize = _ref9.fontSize,
376
+ fontFamily = _ref9.fontFamily,
377
+ letterSpacing = _ref9.letterSpacing,
378
+ lineHeight = _ref9.lineHeight;
299
379
  if (type == 'svg') {
300
380
  return {
301
381
  fontSize: fontSize,
@@ -317,12 +397,12 @@ var getFontStyle = exports.getFontStyle = function getFontStyle(_ref7, type) {
317
397
  lineHeight: lineHeight + "px" //不加px默认是倍数关系
318
398
  };
319
399
  };
320
- var formatFont = exports.formatFont = function formatFont(_ref8, type) {
321
- var color = _ref8.color,
322
- fill = _ref8.fill,
323
- bold = _ref8.bold,
324
- italic = _ref8.italic,
325
- rest = (0, _objectWithoutProperties2["default"])(_ref8, _excluded);
400
+ var formatFont = exports.formatFont = function formatFont(_ref10, type) {
401
+ var color = _ref10.color,
402
+ fill = _ref10.fill,
403
+ bold = _ref10.bold,
404
+ italic = _ref10.italic,
405
+ rest = (0, _objectWithoutProperties2["default"])(_ref10, _excluded);
326
406
  if (type == 'svg') {
327
407
  return _objectSpread({
328
408
  fill: fill || color,
@@ -336,27 +416,27 @@ var formatFont = exports.formatFont = function formatFont(_ref8, type) {
336
416
  fontStyle: typeof italic == "string" ? italic : italic ? 'italic' : 'normal'
337
417
  }, rest);
338
418
  };
339
- var getMargin = exports.getMargin = function getMargin(_ref9) {
340
- var marginTop = _ref9.marginTop,
341
- marginRight = _ref9.marginRight,
342
- marginBottom = _ref9.marginBottom,
343
- marginLeft = _ref9.marginLeft;
419
+ var getMargin = exports.getMargin = function getMargin(_ref11) {
420
+ var marginTop = _ref11.marginTop,
421
+ marginRight = _ref11.marginRight,
422
+ marginBottom = _ref11.marginBottom,
423
+ marginLeft = _ref11.marginLeft;
344
424
  return marginTop + 'px ' + marginRight + 'px ' + marginBottom + 'px ' + marginLeft + 'px';
345
425
  };
346
- var getTranslate3d = exports.getTranslate3d = function getTranslate3d(_ref10) {
347
- var _ref10$x = _ref10.x,
348
- x = _ref10$x === void 0 ? 0 : _ref10$x,
349
- _ref10$y = _ref10.y,
350
- y = _ref10$y === void 0 ? 0 : _ref10$y,
351
- _ref10$z = _ref10.z,
352
- z = _ref10$z === void 0 ? 0 : _ref10$z;
426
+ var getTranslate3d = exports.getTranslate3d = function getTranslate3d(_ref12) {
427
+ var _ref12$x = _ref12.x,
428
+ x = _ref12$x === void 0 ? 0 : _ref12$x,
429
+ _ref12$y = _ref12.y,
430
+ y = _ref12$y === void 0 ? 0 : _ref12$y,
431
+ _ref12$z = _ref12.z,
432
+ z = _ref12$z === void 0 ? 0 : _ref12$z;
353
433
  return 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
354
434
  };
355
- var getTranslate2d = exports.getTranslate2d = function getTranslate2d(_ref11) {
356
- var _ref11$x = _ref11.x,
357
- x = _ref11$x === void 0 ? 0 : _ref11$x,
358
- _ref11$y = _ref11.y,
359
- y = _ref11$y === void 0 ? 0 : _ref11$y;
435
+ var getTranslate2d = exports.getTranslate2d = function getTranslate2d(_ref13) {
436
+ var _ref13$x = _ref13.x,
437
+ x = _ref13$x === void 0 ? 0 : _ref13$x,
438
+ _ref13$y = _ref13.y,
439
+ y = _ref13$y === void 0 ? 0 : _ref13$y;
360
440
  return 'translate(' + x + ', ' + y + ')';
361
441
  };
362
442
  function band() {
@@ -370,9 +450,6 @@ function band() {
370
450
  round = false,
371
451
  paddingInner = 0,
372
452
  paddingOuter = 0,
373
- // seriesPaddingInner = 0,
374
- // seriesPaddingOuter = 0,
375
- // seriesLength = 0,
376
453
  align = 0.5;
377
454
  delete scale.unknown;
378
455
  function rescale() {
@@ -394,12 +471,12 @@ function band() {
394
471
  return arguments.length ? (domain(_), rescale()) : domain();
395
472
  };
396
473
  scale.range = function (_) {
397
- var _ref12;
398
- return arguments.length ? (_ref12 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref12[0], r1 = _ref12[1], r0 = +r0, r1 = +r1, rescale()) : [r0, r1];
474
+ var _ref14;
475
+ return arguments.length ? (_ref14 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref14[0], r1 = _ref14[1], r0 = +r0, r1 = +r1, rescale()) : [r0, r1];
399
476
  };
400
477
  scale.rangeRound = function (_) {
401
- var _ref13;
402
- return _ref13 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref13[0], r1 = _ref13[1], r0 = +r0, r1 = +r1, round = true, rescale();
478
+ var _ref15;
479
+ return _ref15 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref15[0], r1 = _ref15[1], r0 = +r0, r1 = +r1, round = true, rescale();
403
480
  };
404
481
  scale.bandwidth = function () {
405
482
  return bandwidth;
@@ -448,14 +525,14 @@ function initRange(domain, range) {
448
525
  }
449
526
  var getStacks = exports.getStacks = function getStacks(series) {
450
527
  var tmp = [];
451
- series.forEach(function (_ref14, name) {
452
- var type = _ref14.type,
453
- stack = _ref14.stack,
454
- yOrZ = _ref14.yOrZ;
455
- var current = tmp.find(function (_ref15) {
456
- var _type = _ref15.type,
457
- _stack = _ref15.stack,
458
- _yOrZ = _ref15.yOrZ;
528
+ series.forEach(function (_ref16, name) {
529
+ var type = _ref16.type,
530
+ stack = _ref16.stack,
531
+ yOrZ = _ref16.yOrZ;
532
+ var current = tmp.find(function (_ref17) {
533
+ var _type = _ref17.type,
534
+ _stack = _ref17.stack,
535
+ _yOrZ = _ref17.yOrZ;
459
536
  return _type == type && stack && _stack == stack && yOrZ == _yOrZ;
460
537
  });
461
538
  if (!current) {
@@ -485,9 +562,9 @@ var getStacks = exports.getStacks = function getStacks(series) {
485
562
  });
486
563
  return tmp;
487
564
  };
488
- var dataYOrZ = exports.dataYOrZ = function dataYOrZ(data, _ref16) {
489
- var seriesY = _ref16.y,
490
- seriesZ = _ref16.z;
565
+ var dataYOrZ = exports.dataYOrZ = function dataYOrZ(data, _ref18) {
566
+ var seriesY = _ref18.y,
567
+ seriesZ = _ref18.z;
491
568
  var tmp = {
492
569
  y: [],
493
570
  z: []
@@ -526,11 +603,11 @@ var resetStacks = exports.resetStacks = function resetStacks(stacks) {
526
603
  });
527
604
  };
528
605
  var getCurrentStack = exports.getCurrentStack = function getCurrentStack(stack, stackMap) {
529
- return stackMap.find(function (_ref17) {
530
- var _stack = _ref17.stack,
531
- _type = _ref17.type,
532
- _yOrZ = _ref17.yOrZ,
533
- _s = _ref17.s;
606
+ return stackMap.find(function (_ref19) {
607
+ var _stack = _ref19.stack,
608
+ _type = _ref19.type,
609
+ _yOrZ = _ref19.yOrZ,
610
+ _s = _ref19.s;
534
611
  return _type == stack.type && _stack == stack.stack && _yOrZ == stack.yOrZ && _s.includes(stack.name);
535
612
  });
536
613
  };
@@ -562,6 +639,8 @@ var reverseGradientStops = function reverseGradientStops(gradient) {
562
639
  })
563
640
  });
564
641
  };
642
+
643
+ // ===================== 核心改动:改造 getBandBackground 方法 =====================
565
644
  var getBandBackground = exports.getBandBackground = function getBandBackground(pattern, fill, y) {
566
645
  if (!(pattern && pattern.path)) {
567
646
  return (0, _utils.getColor)(y < 0 ? reverseGradientStops(fill) : fill); //小于0颜色翻转
@@ -570,20 +649,24 @@ var getBandBackground = exports.getBandBackground = function getBandBackground(p
570
649
  var _pattern$backgroundSi = pattern.backgroundSize,
571
650
  backgroundSize = _pattern$backgroundSi === void 0 ? '100% 100%' : _pattern$backgroundSi,
572
651
  _pattern = (0, _objectWithoutProperties2["default"])(pattern, _excluded2);
573
- return 'center top / ' + backgroundSize + ' url("data:image/svg+xml,' + encodeURIComponent((0, _server.renderToStaticMarkup)(/*#__PURE__*/React.createElement(SvgBackground, {
652
+ // 替换原 SVG DataURL Canvas DataURL
653
+ var canvasDataUrl = renderCanvasBackground({
574
654
  fill: fill,
575
655
  pattern: _pattern
576
- }))) + '")';
577
- } catch (e) {}
656
+ });
657
+ return 'center top / ' + backgroundSize + ' url("' + canvasDataUrl + '")';
658
+ } catch (e) {
659
+ console.error('Canvas 绘制背景失败:', e);
660
+ }
578
661
  return "";
579
662
  };
580
663
  var getBandwidth = exports.getBandwidth = function getBandwidth(step, paddingOuter) {
581
664
  return step * (1 - paddingOuter);
582
665
  };
583
- var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function getBandSeriesStepAndWidth(_ref18) {
584
- var width = _ref18.width,
585
- paddingInner = _ref18.paddingInner,
586
- bandLength = _ref18.bandLength;
666
+ var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function getBandSeriesStepAndWidth(_ref20) {
667
+ var width = _ref20.width,
668
+ paddingInner = _ref20.paddingInner,
669
+ bandLength = _ref20.bandLength;
587
670
  var seriesStep = width / (bandLength == 0 ? 1 : bandLength);
588
671
  var seriesWidth = seriesStep * (1 - paddingInner);
589
672
  return {
@@ -591,14 +674,14 @@ var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function get
591
674
  seriesWidth: seriesWidth
592
675
  };
593
676
  };
594
- var getSeriesInfo = exports.getSeriesInfo = function getSeriesInfo(_ref19) {
595
- var step = _ref19.step,
596
- _ref19$bandLength = _ref19.bandLength,
597
- bandLength = _ref19$bandLength === void 0 ? 1 : _ref19$bandLength,
598
- _ref19$paddingInner = _ref19.paddingInner,
599
- paddingInner = _ref19$paddingInner === void 0 ? 0 : _ref19$paddingInner,
600
- _ref19$paddingOuter = _ref19.paddingOuter,
601
- paddingOuter = _ref19$paddingOuter === void 0 ? 0 : _ref19$paddingOuter;
677
+ var getSeriesInfo = exports.getSeriesInfo = function getSeriesInfo(_ref21) {
678
+ var step = _ref21.step,
679
+ _ref21$bandLength = _ref21.bandLength,
680
+ bandLength = _ref21$bandLength === void 0 ? 1 : _ref21$bandLength,
681
+ _ref21$paddingInner = _ref21.paddingInner,
682
+ paddingInner = _ref21$paddingInner === void 0 ? 0 : _ref21$paddingInner,
683
+ _ref21$paddingOuter = _ref21.paddingOuter,
684
+ paddingOuter = _ref21$paddingOuter === void 0 ? 0 : _ref21$paddingOuter;
602
685
  if (bandLength == 0) return {
603
686
  seriesWidth: step,
604
687
  seriesStep: step,
@@ -705,54 +788,29 @@ var sortPie = exports.sortPie = function sortPie(data, order) {
705
788
  });
706
789
  switch (order) {
707
790
  case '':
708
- _data.sort(function (_ref20, _ref21) {
709
- var a = _ref20.index;
710
- var b = _ref21.index;
791
+ _data.sort(function (_ref22, _ref23) {
792
+ var a = _ref22.index;
793
+ var b = _ref23.index;
711
794
  return (0, _d3v.ascending)(a, b);
712
795
  });
713
796
  break;
714
797
  case 'desc':
715
- _data.sort(function (_ref22, _ref23) {
716
- var a = _ref22.value;
717
- var b = _ref23.value;
798
+ _data.sort(function (_ref24, _ref25) {
799
+ var a = _ref24.value;
800
+ var b = _ref25.value;
718
801
  return (0, _d3v.descending)(a, b);
719
802
  });
720
803
  break;
721
804
  case 'asc':
722
- _data.sort(function (_ref24, _ref25) {
723
- var a = _ref24.value;
724
- var b = _ref25.value;
805
+ _data.sort(function (_ref26, _ref27) {
806
+ var a = _ref26.value;
807
+ var b = _ref27.value;
725
808
  return (0, _d3v.ascending)(a, b);
726
809
  });
727
810
  break;
728
811
  }
729
812
  return _data;
730
813
  };
731
-
732
- // const getDataWithPercent = (data = [], precision = 0, type) => {
733
- // const digits = Math.pow(10, precision);
734
- // const targetSeats = digits * 100;
735
-
736
- // const total = sum(data, (d) => d.value);
737
-
738
- // const votesPerQuota = data.map((d, index) => ({
739
- // ...d,
740
- // vote: Math.round((d.value / total) * digits * 100),
741
- // index,
742
- // }));
743
- // const currentSum = sum(votesPerQuota, (d) => d.vote);
744
- // const remainder = targetSeats - currentSum;
745
- // console.log(type+":",votesPerQuota, toFixed);
746
- // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
747
-
748
- // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
749
- // ...data,
750
- // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
751
- // }));
752
-
753
- // return tmp;
754
- // };
755
-
756
814
  var getDataWithPercent = exports.getDataWithPercent = function getDataWithPercent() {
757
815
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
758
816
  var precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/charts",
3
- "version": "1.9.3",
3
+ "version": "1.9.4",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -49,11 +49,11 @@ export const pieLegendFormatter = (
49
49
  const textMarginLeft = 5;
50
50
 
51
51
  // 1. 计算每列宽度(加上间距)
52
- const columns = [];
52
+ let columns = [];
53
53
  if (showName) columns.push(`${props.nameMaxWidth}px`);
54
54
  if (showValue) columns.push(`${props.valueMaxWidth + valueGap}px`);
55
55
  if (showPercent) columns.push(`${props.percentMaxWidth + percentGap}px`);
56
-
56
+ columns[columns.length-1]="auto"
57
57
  return (
58
58
  <>
59
59
  {icon &&<span style={{ ...icon, marginRight: icon.marginRight, transform:`translate(${nameX}px, ${nameY}px)` }} />}
@@ -84,6 +84,7 @@ export const pieLegendFormatter = (
84
84
  alignItems: 'center',
85
85
  justifyContent: valueAlign=="left"?"flex-start":valueAlign=="right"?"flex-end":'center',
86
86
  textAlign: valueAlign === "left" ? "left" : valueAlign === "right" ? "right" : "center",
87
+ //------------
87
88
  }}
88
89
  >
89
90
  <span><SplitText value={data.y} config={splitConfig} /></span>
@@ -18,6 +18,76 @@ const defaultLineIcon = {
18
18
  height: 2,
19
19
  background: defaultBackground,
20
20
  };
21
+ const renderCanvasBackground = ({
22
+ fill: {
23
+ type,
24
+ pure,
25
+ linear: { angle, opacity, stops },
26
+ },
27
+ pattern: {
28
+ path = '',
29
+ width = '100%',
30
+ height = '100%',
31
+ boderColor: stroke = 'transparent',
32
+ boderWidth = 0,
33
+ },
34
+ }) => {
35
+ // 处理宽高(支持百分比/数字)
36
+ const canvasWidth = width === '100%' ? 200 : Number(width) || 200; // 百分比默认200,可根据实际场景调整
37
+ const canvasHeight = height === '100%' ? 200 : Number(height) || 200;
38
+
39
+ // 创建离屏 Canvas
40
+ const canvas = document.createElement('canvas');
41
+ const ctx = canvas.getContext('2d');
42
+ canvas.width = canvasWidth;
43
+ canvas.height = canvasHeight;
44
+
45
+ // 清空画布
46
+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);
47
+
48
+ // 1. 解析 SVG Path 为 Canvas Path2D
49
+ const path2D = new Path2D(path);
50
+
51
+ // 2. 处理填充(纯色/渐变)
52
+ if (type === 'pure') {
53
+ ctx.fillStyle = pure;
54
+ } else {
55
+ // 线性渐变(对应原 SVG linearGradient)
56
+ const centerX = canvasWidth / 2;
57
+ const centerY = canvasHeight / 2;
58
+ const rad = (angle + 180) * Math.PI / 180; // 旋转角度转弧度
59
+ const gradientLength = Math.hypot(canvasWidth, canvasHeight); // 渐变长度取对角线
60
+
61
+ // 计算渐变起点/终点(匹配 SVG rotate 效果)
62
+ const startX = centerX - Math.sin(rad) * gradientLength;
63
+ const startY = centerY - Math.cos(rad) * gradientLength;
64
+ const endX = centerX + Math.sin(rad) * gradientLength;
65
+ const endY = centerY + Math.cos(rad) * gradientLength;
66
+
67
+ // 创建渐变
68
+ const gradient = ctx.createLinearGradient(startX, startY, endX, endY);
69
+ stops.forEach(({ offset, color }) => {
70
+ gradient.addColorStop(offset / 100, color);
71
+ gradient.opacity = opacity; // 透明度
72
+ });
73
+ ctx.fillStyle = gradient;
74
+ }
75
+
76
+ // 3. 处理描边
77
+ ctx.strokeStyle = stroke;
78
+ ctx.lineWidth = boderWidth;
79
+
80
+ // 4. 绘制路径
81
+ ctx.fill(path2D);
82
+ if (boderWidth > 0) {
83
+ ctx.stroke(path2D);
84
+ }
85
+
86
+ // 5. 转换为 DataURL(替代 SVG DataURL)
87
+ return canvas.toDataURL('image/png');
88
+ };
89
+
90
+ // 原 SvgBackground 组件保留(兼容旧逻辑,也可删除)
21
91
  const SvgBackground = ({
22
92
  fill: {
23
93
  type,
@@ -67,6 +137,8 @@ const SvgBackground = ({
67
137
  </svg>
68
138
  );
69
139
  };
140
+
141
+ // ===================== 原有方法(仅改造 getBandBackground) =====================
70
142
  const getColorList = ({ type, pure, linear: { stops, angle, opacity } }) => {
71
143
  if (type == 'pure') {
72
144
  return [
@@ -76,20 +148,22 @@ const getColorList = ({ type, pure, linear: { stops, angle, opacity } }) => {
76
148
  }
77
149
  return stops.map(({ color, offset }) => ({ color, offset: offset / 100 }));
78
150
  };
79
- const getMultiColorStr = (colors)=>{
80
- if(colors){
151
+
152
+ const getMultiColorStr = (colors) => {
153
+ if (colors) {
81
154
  const { stops, angle } = colors;
82
155
  let result = 'linear-gradient(' + angle + 'deg, ';
83
156
  const stops_ = JSON.parse(JSON.stringify(stops));
84
- stops_.sort((a,b)=>a.offset-b.offset);
85
- for(let i=0;i<stops_.length;i++){
157
+ stops_.sort((a, b) => a.offset - b.offset);
158
+ for (let i = 0; i < stops_.length; i++) {
86
159
  const { color, offset } = stops_[i];
87
160
  result += `${color} ${offset}%,`;
88
161
  }
89
- return result.slice(0,-1)+")";
162
+ return result.slice(0, -1) + ')';
90
163
  }
91
- }
92
- const getIcon = (type, icon, lineType="solid") => {
164
+ };
165
+
166
+ const getIcon = (type, icon, lineType = "solid") => {
93
167
  switch (type) {
94
168
  case 'area':
95
169
  case 'line':
@@ -99,7 +173,7 @@ const getIcon = (type, icon, lineType="solid") => {
99
173
  ...defaultLineIcon,
100
174
  ...icon,
101
175
  minWidth: icon.width,
102
- background:lineType=="solid"?color:`linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
176
+ background: lineType == "solid" ? color : `linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
103
177
  }
104
178
  : defaultLineIcon;
105
179
  default:
@@ -150,6 +224,7 @@ const dateFormat = (date, fmt) => {
150
224
  );
151
225
  return fmt;
152
226
  };
227
+
153
228
  const getBreakWord = (str, breakNumber) => {
154
229
  const re = new RegExp('([^]){1,' + breakNumber + '}', 'g');
155
230
  return str.match(re);
@@ -164,27 +239,27 @@ const getTicksOfAxis = (domain, ticksCount, showLast) => {
164
239
  if (showLast) {
165
240
  let count = ticksCount, gap = 0;
166
241
  step = (len - 1) / (count - 1);
167
- const maxGap = Math.max(count-2,len-count);
242
+ const maxGap = Math.max(count - 2, len - count);
168
243
  //循环计算出最接近count值且能让step为整数的值
169
- if(!Number.isInteger(step)){
170
- while(gap<=maxGap){
244
+ if (!Number.isInteger(step)) {
245
+ while (gap <= maxGap) {
171
246
  gap++;
172
- const left = (len-1)/(count-gap-1), right = (len-1)/(count+gap-1);
173
- if(Number.isInteger(left)){
247
+ const left = (len - 1) / (count - gap - 1), right = (len - 1) / (count + gap - 1);
248
+ if (Number.isInteger(left)) {
174
249
  step = left;
175
250
  break;
176
251
  }
177
- if(Number.isInteger(right)){
252
+ if (Number.isInteger(right)) {
178
253
  step = right;
179
254
  break;
180
255
  }
181
256
  }
182
257
  }
183
- if(!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
258
+ if (!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
184
259
  ticksArr = domain.filter(function (d, i) {
185
260
  return i % step === 0;
186
261
  });
187
- }else{
262
+ } else {
188
263
  ticksArr = domain.filter(function (d, i) {
189
264
  return i % (step + 1) === 0;
190
265
  });
@@ -224,6 +299,7 @@ const getTickCoord = ({
224
299
  }
225
300
  return { x1, x2, y1, y2 };
226
301
  };
302
+
227
303
  const getGridCoord = ({ orientation, coordinate, end }) => {
228
304
  let x1, x2, y1, y2;
229
305
  switch (orientation) {
@@ -265,7 +341,7 @@ const getMousePos = (evt, dom) => {
265
341
  };
266
342
 
267
343
  const getFontStyle = (
268
- { color, bold, italic, fontSize, fontFamily, letterSpacing,lineHeight },
344
+ { color, bold, italic, fontSize, fontFamily, letterSpacing, lineHeight },
269
345
  type
270
346
  ) => {
271
347
  if (type == 'svg') {
@@ -276,7 +352,7 @@ const getFontStyle = (
276
352
  fill: color,
277
353
  fontWeight: bold ? 'bold' : 'normal',
278
354
  fontStyle: italic ? 'italic' : 'normal',
279
- lineHeight:lineHeight+"px"
355
+ lineHeight: lineHeight + "px"
280
356
  };
281
357
  }
282
358
  return {
@@ -286,26 +362,26 @@ const getFontStyle = (
286
362
  color,
287
363
  fontWeight: bold ? 'bold' : 'normal',
288
364
  fontStyle: italic ? 'italic' : 'normal',
289
- lineHeight:lineHeight+"px"//不加px默认是倍数关系
365
+ lineHeight: lineHeight + "px"//不加px默认是倍数关系
290
366
  };
291
367
  };
292
- const formatFont=({ color, fill, bold, italic, ...rest },
293
- type)=>{
368
+
369
+ const formatFont = ({ color, fill, bold, italic, ...rest }, type) => {
294
370
  if (type == 'svg') {
295
371
  return {
296
372
  fill: fill || color,
297
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
298
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
373
+ fontWeight: typeof bold == "string" ? bold : (bold ? 'bold' : 'normal'),
374
+ fontStyle: typeof italic == "string" ? italic : (italic ? 'italic' : 'normal'),
299
375
  ...rest
300
376
  };
301
377
  }
302
378
  return {
303
- color:color|| fill,
304
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
305
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
379
+ color: color || fill,
380
+ fontWeight: typeof bold == "string" ? bold : (bold ? 'bold' : 'normal'),
381
+ fontStyle: typeof italic == "string" ? italic : (italic ? 'italic' : 'normal'),
306
382
  ...rest
307
383
  };
308
- }
384
+ };
309
385
 
310
386
  const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
311
387
  marginTop +
@@ -316,9 +392,12 @@ const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
316
392
  'px ' +
317
393
  marginLeft +
318
394
  'px';
395
+
319
396
  const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
320
397
  'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
398
+
321
399
  const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
400
+
322
401
  function band() {
323
402
  var scale = ordinal().unknown(undefined),
324
403
  domain = scale.domain,
@@ -330,9 +409,6 @@ function band() {
330
409
  round = false,
331
410
  paddingInner = 0,
332
411
  paddingOuter = 0,
333
- // seriesPaddingInner = 0,
334
- // seriesPaddingOuter = 0,
335
- // seriesLength = 0,
336
412
  align = 0.5;
337
413
 
338
414
  delete scale.unknown;
@@ -515,49 +591,53 @@ const getCurrentStack = (stack, stackMap) =>
515
591
  _yOrZ == stack.yOrZ &&
516
592
  _s.includes(stack.name)
517
593
  );
518
- const reverseGradientStops=(gradient)=>{
519
- if (gradient.type !== 'linear' || !gradient.linear?.stops) {
520
- return gradient;
521
- }
522
- const stops = gradient.linear.stops;
523
- //先按 offset 排序(升序),确保输入是规范的
524
- const sortedStops = [...stops].sort((a, b) => a.offset - b.offset);
525
-
526
- // 反转每个 stop 的 offset: newOffset = 100 - oldOffset
527
- // 颜色保持不变,只是位置镜像翻转
528
- const reversedStops = sortedStops.map(stop => ({
529
- ...stop,
530
- offset: 100 - stop.offset
531
- }));
532
- //再按新的 offset 升序排序,确保输出结构正确
533
- const finalStops = reversedStops.sort((a, b) => a.offset - b.offset);
534
-
535
- return {
536
- ...gradient,
537
- linear: {
538
- ...gradient.linear,
539
- stops: finalStops
540
- }
541
- };
594
+
595
+ const reverseGradientStops = (gradient) => {
596
+ if (gradient.type !== 'linear' || !gradient.linear?.stops) {
597
+ return gradient;
542
598
  }
543
- const getBandBackground = (pattern, fill,y) => {
544
- if (!(pattern && pattern.path)){
545
- return getColor(y<0?reverseGradientStops(fill):fill);//小于0颜色翻转
599
+ const stops = gradient.linear.stops;
600
+ //先按 offset 排序(升序),确保输入是规范的
601
+ const sortedStops = [...stops].sort((a, b) => a.offset - b.offset);
602
+
603
+ // 反转每个 stop 的 offset: newOffset = 100 - oldOffset
604
+ // 颜色保持不变,只是位置镜像翻转
605
+ const reversedStops = sortedStops.map(stop => ({
606
+ ...stop,
607
+ offset: 100 - stop.offset
608
+ }));
609
+ //再按新的 offset 升序排序,确保输出结构正确
610
+ const finalStops = reversedStops.sort((a, b) => a.offset - b.offset);
611
+
612
+ return {
613
+ ...gradient,
614
+ linear: {
615
+ ...gradient.linear,
616
+ stops: finalStops
617
+ }
618
+ };
619
+ };
620
+
621
+ // ===================== 核心改动:改造 getBandBackground 方法 =====================
622
+ const getBandBackground = (pattern, fill, y) => {
623
+ if (!(pattern && pattern.path)) {
624
+ return getColor(y < 0 ? reverseGradientStops(fill) : fill);//小于0颜色翻转
546
625
  }
547
- try{
626
+ try {
548
627
  const { backgroundSize = '100% 100%', ..._pattern } = pattern;
628
+ // 替换原 SVG DataURL 为 Canvas DataURL
629
+ const canvasDataUrl = renderCanvasBackground({ fill, pattern: _pattern });
549
630
  return (
550
631
  'center top / ' +
551
632
  backgroundSize +
552
- ' url("data:image/svg+xml,' +
553
- encodeURIComponent(
554
- renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
555
- ) +
556
- '")'
633
+ ' url("' + canvasDataUrl + '")'
557
634
  );
558
- }catch(e){}
635
+ } catch (e) {
636
+ console.error('Canvas 绘制背景失败:', e);
637
+ }
559
638
  return "";
560
639
  };
640
+
561
641
  const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
562
642
 
563
643
  const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
@@ -708,37 +788,13 @@ const sortPie = (data, order) => {
708
788
  return _data;
709
789
  };
710
790
 
711
- // const getDataWithPercent = (data = [], precision = 0, type) => {
712
- // const digits = Math.pow(10, precision);
713
- // const targetSeats = digits * 100;
714
-
715
- // const total = sum(data, (d) => d.value);
716
-
717
- // const votesPerQuota = data.map((d, index) => ({
718
- // ...d,
719
- // vote: Math.round((d.value / total) * digits * 100),
720
- // index,
721
- // }));
722
- // const currentSum = sum(votesPerQuota, (d) => d.vote);
723
- // const remainder = targetSeats - currentSum;
724
- // console.log(type+":",votesPerQuota, toFixed);
725
- // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
726
-
727
- // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
728
- // ...data,
729
- // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
730
- // }));
731
-
732
- // return tmp;
733
- // };
734
-
735
791
  const getDataWithPercent = (data = [], precision = 0) => {
736
792
  let objData = [];
737
793
  function getPercentWithPrecision(valueList, idx, precision) {
738
794
  if (!valueList[idx]) {
739
795
  return 0;
740
796
  }
741
- const sum = valueList.reduce( function (acc, val) {
797
+ const sum = valueList.reduce(function (acc, val) {
742
798
  return acc + val.value;
743
799
  }, 0);
744
800
  if (sum === 0) {
@@ -752,7 +808,7 @@ const getDataWithPercent = (data = [], precision = 0) => {
752
808
  const seats = votesPerQuota.map(function (votes) {
753
809
  return Math.floor(votes);
754
810
  });
755
- let currentSum = seats.reduce( function (acc, val) {
811
+ let currentSum = seats.reduce(function (acc, val) {
756
812
  return acc + val;
757
813
  }, 0);
758
814
  const remainder = votesPerQuota.map(function (votes, idx) {
@@ -800,10 +856,10 @@ const reduceConfig = (config = []) => {
800
856
  };
801
857
 
802
858
  //限制value的值在min和max之间
803
- const mathR=(value, range)=>{
804
- const [min,max] = range;
805
- return Math.max(min,Math.min(value,max));
806
- }
859
+ const mathR = (value, range) => {
860
+ const [min, max] = range;
861
+ return Math.max(min, Math.min(value, max));
862
+ };
807
863
 
808
864
  export {
809
865
  dateFormat,
@@ -838,5 +894,7 @@ export {
838
894
  getDataWithPercent,
839
895
  reduceConfig,
840
896
  getSeriesInfo,
841
- mathR
842
- };
897
+ mathR,
898
+ renderCanvasBackground, // 导出 Canvas 绘制方法(可选)
899
+ SvgBackground // 保留原 SVG 组件(兼容旧逻辑,可选删除)
900
+ };