@easyv/charts 1.9.3 → 1.9.5

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.
@@ -53,6 +53,7 @@ var Chart = /*#__PURE__*/(0, _react.memo)(function (_ref2) {
53
53
  emit = _ref2.emit,
54
54
  emitEvent = _ref2.emitEvent,
55
55
  props = (0, _objectWithoutProperties2["default"])(_ref2, _excluded);
56
+ console.log("888");
56
57
  var width = props.width,
57
58
  height = props.height;
58
59
  var isIOS = (0, _react.useRef)(/iPad|iPhone|iPod|iOS/i.test(navigator.userAgent) || /Mac OS X/i.test(navigator.userAgent) && !/Chrome/i.test(navigator.userAgent));
@@ -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,
@@ -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,81 @@ 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
+ var SvgBackground = exports.SvgBackground = function SvgBackground(_ref3) {
107
+ var _ref3$fill = _ref3.fill,
108
+ type = _ref3$fill.type,
109
+ pure = _ref3$fill.pure,
110
+ _ref3$fill$linear = _ref3$fill.linear,
111
+ angle = _ref3$fill$linear.angle,
112
+ opacity = _ref3$fill$linear.opacity,
113
+ stops = _ref3$fill$linear.stops,
114
+ _ref3$pattern = _ref3.pattern,
115
+ _ref3$pattern$path = _ref3$pattern.path,
116
+ path = _ref3$pattern$path === void 0 ? '' : _ref3$pattern$path,
117
+ _ref3$pattern$width = _ref3$pattern.width,
118
+ width = _ref3$pattern$width === void 0 ? '100%' : _ref3$pattern$width,
119
+ _ref3$pattern$height = _ref3$pattern.height,
120
+ height = _ref3$pattern$height === void 0 ? '100%' : _ref3$pattern$height,
121
+ _ref3$pattern$boderCo = _ref3$pattern.boderColor,
122
+ stroke = _ref3$pattern$boderCo === void 0 ? 'transparent' : _ref3$pattern$boderCo,
123
+ _ref3$pattern$boderWi = _ref3$pattern.boderWidth,
124
+ boderWidth = _ref3$pattern$boderWi === void 0 ? 0 : _ref3$pattern$boderWi;
49
125
  return /*#__PURE__*/React.createElement("svg", {
50
126
  preserveAspectRatio: "none",
51
127
  xmlns: "http://www.w3.org/2000/svg",
@@ -58,9 +134,9 @@ var SvgBackground = function SvgBackground(_ref) {
58
134
  x2: "0%",
59
135
  y2: "100%",
60
136
  gradientTransform: 'rotate(' + (angle + 180) + ', 0.5, 0.5)'
61
- }, stops.map(function (_ref2, index) {
62
- var offset = _ref2.offset,
63
- color = _ref2.color;
137
+ }, stops.map(function (_ref4, index) {
138
+ var offset = _ref4.offset,
139
+ color = _ref4.color;
64
140
  return /*#__PURE__*/React.createElement("stop", {
65
141
  key: index,
66
142
  offset: offset + '%',
@@ -74,13 +150,13 @@ var SvgBackground = function SvgBackground(_ref) {
74
150
  strokeWidth: boderWidth
75
151
  }));
76
152
  };
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;
153
+ var getColorList = exports.getColorList = function getColorList(_ref5) {
154
+ var type = _ref5.type,
155
+ pure = _ref5.pure,
156
+ _ref5$linear = _ref5.linear,
157
+ stops = _ref5$linear.stops,
158
+ angle = _ref5$linear.angle,
159
+ opacity = _ref5$linear.opacity;
84
160
  if (type == 'pure') {
85
161
  return [{
86
162
  color: pure,
@@ -90,9 +166,9 @@ var getColorList = exports.getColorList = function getColorList(_ref3) {
90
166
  offset: 0
91
167
  }];
92
168
  }
93
- return stops.map(function (_ref4) {
94
- var color = _ref4.color,
95
- offset = _ref4.offset;
169
+ return stops.map(function (_ref6) {
170
+ var color = _ref6.color,
171
+ offset = _ref6.offset;
96
172
  return {
97
173
  color: color,
98
174
  offset: offset / 100
@@ -114,7 +190,7 @@ var getMultiColorStr = exports.getMultiColorStr = function getMultiColorStr(colo
114
190
  offset = _stops_$i.offset;
115
191
  result += "".concat(color, " ").concat(offset, "%,");
116
192
  }
117
- return result.slice(0, -1) + ")";
193
+ return result.slice(0, -1) + ')';
118
194
  }
119
195
  };
120
196
  var getIcon = exports.getIcon = function getIcon(type, icon) {
@@ -201,15 +277,15 @@ var getTicksOfAxis = exports.getTicksOfAxis = function getTicksOfAxis(domain, ti
201
277
  }
202
278
  return ticksArr;
203
279
  };
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;
280
+ var getTickCoord = exports.getTickCoord = function getTickCoord(_ref7) {
281
+ var orientation = _ref7.orientation,
282
+ coordinate = _ref7.coordinate,
283
+ _ref7$tickSize = _ref7.tickSize,
284
+ tickSize = _ref7$tickSize === void 0 ? 6 : _ref7$tickSize,
285
+ _ref7$x = _ref7.x,
286
+ x = _ref7$x === void 0 ? 0 : _ref7$x,
287
+ _ref7$y = _ref7.y,
288
+ y = _ref7$y === void 0 ? 0 : _ref7$y;
213
289
  var x1, x2, y1, y2;
214
290
  switch (orientation) {
215
291
  case 'top':
@@ -240,10 +316,10 @@ var getTickCoord = exports.getTickCoord = function getTickCoord(_ref5) {
240
316
  y2: y2
241
317
  };
242
318
  };
243
- var getGridCoord = exports.getGridCoord = function getGridCoord(_ref6) {
244
- var orientation = _ref6.orientation,
245
- coordinate = _ref6.coordinate,
246
- end = _ref6.end;
319
+ var getGridCoord = exports.getGridCoord = function getGridCoord(_ref8) {
320
+ var orientation = _ref8.orientation,
321
+ coordinate = _ref8.coordinate,
322
+ end = _ref8.end;
247
323
  var x1, x2, y1, y2;
248
324
  switch (orientation) {
249
325
  case 'top':
@@ -288,14 +364,14 @@ var getMousePos = exports.getMousePos = function getMousePos(evt, dom) {
288
364
  h: rect.height
289
365
  };
290
366
  };
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;
367
+ var getFontStyle = exports.getFontStyle = function getFontStyle(_ref9, type) {
368
+ var color = _ref9.color,
369
+ bold = _ref9.bold,
370
+ italic = _ref9.italic,
371
+ fontSize = _ref9.fontSize,
372
+ fontFamily = _ref9.fontFamily,
373
+ letterSpacing = _ref9.letterSpacing,
374
+ lineHeight = _ref9.lineHeight;
299
375
  if (type == 'svg') {
300
376
  return {
301
377
  fontSize: fontSize,
@@ -317,12 +393,12 @@ var getFontStyle = exports.getFontStyle = function getFontStyle(_ref7, type) {
317
393
  lineHeight: lineHeight + "px" //不加px默认是倍数关系
318
394
  };
319
395
  };
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);
396
+ var formatFont = exports.formatFont = function formatFont(_ref10, type) {
397
+ var color = _ref10.color,
398
+ fill = _ref10.fill,
399
+ bold = _ref10.bold,
400
+ italic = _ref10.italic,
401
+ rest = (0, _objectWithoutProperties2["default"])(_ref10, _excluded);
326
402
  if (type == 'svg') {
327
403
  return _objectSpread({
328
404
  fill: fill || color,
@@ -336,27 +412,27 @@ var formatFont = exports.formatFont = function formatFont(_ref8, type) {
336
412
  fontStyle: typeof italic == "string" ? italic : italic ? 'italic' : 'normal'
337
413
  }, rest);
338
414
  };
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;
415
+ var getMargin = exports.getMargin = function getMargin(_ref11) {
416
+ var marginTop = _ref11.marginTop,
417
+ marginRight = _ref11.marginRight,
418
+ marginBottom = _ref11.marginBottom,
419
+ marginLeft = _ref11.marginLeft;
344
420
  return marginTop + 'px ' + marginRight + 'px ' + marginBottom + 'px ' + marginLeft + 'px';
345
421
  };
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;
422
+ var getTranslate3d = exports.getTranslate3d = function getTranslate3d(_ref12) {
423
+ var _ref12$x = _ref12.x,
424
+ x = _ref12$x === void 0 ? 0 : _ref12$x,
425
+ _ref12$y = _ref12.y,
426
+ y = _ref12$y === void 0 ? 0 : _ref12$y,
427
+ _ref12$z = _ref12.z,
428
+ z = _ref12$z === void 0 ? 0 : _ref12$z;
353
429
  return 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
354
430
  };
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;
431
+ var getTranslate2d = exports.getTranslate2d = function getTranslate2d(_ref13) {
432
+ var _ref13$x = _ref13.x,
433
+ x = _ref13$x === void 0 ? 0 : _ref13$x,
434
+ _ref13$y = _ref13.y,
435
+ y = _ref13$y === void 0 ? 0 : _ref13$y;
360
436
  return 'translate(' + x + ', ' + y + ')';
361
437
  };
362
438
  function band() {
@@ -370,9 +446,6 @@ function band() {
370
446
  round = false,
371
447
  paddingInner = 0,
372
448
  paddingOuter = 0,
373
- // seriesPaddingInner = 0,
374
- // seriesPaddingOuter = 0,
375
- // seriesLength = 0,
376
449
  align = 0.5;
377
450
  delete scale.unknown;
378
451
  function rescale() {
@@ -394,12 +467,12 @@ function band() {
394
467
  return arguments.length ? (domain(_), rescale()) : domain();
395
468
  };
396
469
  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];
470
+ var _ref14;
471
+ return arguments.length ? (_ref14 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref14[0], r1 = _ref14[1], r0 = +r0, r1 = +r1, rescale()) : [r0, r1];
399
472
  };
400
473
  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();
474
+ var _ref15;
475
+ return _ref15 = (0, _slicedToArray2["default"])(_, 2), r0 = _ref15[0], r1 = _ref15[1], r0 = +r0, r1 = +r1, round = true, rescale();
403
476
  };
404
477
  scale.bandwidth = function () {
405
478
  return bandwidth;
@@ -448,14 +521,14 @@ function initRange(domain, range) {
448
521
  }
449
522
  var getStacks = exports.getStacks = function getStacks(series) {
450
523
  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;
524
+ series.forEach(function (_ref16, name) {
525
+ var type = _ref16.type,
526
+ stack = _ref16.stack,
527
+ yOrZ = _ref16.yOrZ;
528
+ var current = tmp.find(function (_ref17) {
529
+ var _type = _ref17.type,
530
+ _stack = _ref17.stack,
531
+ _yOrZ = _ref17.yOrZ;
459
532
  return _type == type && stack && _stack == stack && yOrZ == _yOrZ;
460
533
  });
461
534
  if (!current) {
@@ -485,9 +558,9 @@ var getStacks = exports.getStacks = function getStacks(series) {
485
558
  });
486
559
  return tmp;
487
560
  };
488
- var dataYOrZ = exports.dataYOrZ = function dataYOrZ(data, _ref16) {
489
- var seriesY = _ref16.y,
490
- seriesZ = _ref16.z;
561
+ var dataYOrZ = exports.dataYOrZ = function dataYOrZ(data, _ref18) {
562
+ var seriesY = _ref18.y,
563
+ seriesZ = _ref18.z;
491
564
  var tmp = {
492
565
  y: [],
493
566
  z: []
@@ -526,11 +599,11 @@ var resetStacks = exports.resetStacks = function resetStacks(stacks) {
526
599
  });
527
600
  };
528
601
  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;
602
+ return stackMap.find(function (_ref19) {
603
+ var _stack = _ref19.stack,
604
+ _type = _ref19.type,
605
+ _yOrZ = _ref19.yOrZ,
606
+ _s = _ref19.s;
534
607
  return _type == stack.type && _stack == stack.stack && _yOrZ == stack.yOrZ && _s.includes(stack.name);
535
608
  });
536
609
  };
@@ -570,20 +643,24 @@ var getBandBackground = exports.getBandBackground = function getBandBackground(p
570
643
  var _pattern$backgroundSi = pattern.backgroundSize,
571
644
  backgroundSize = _pattern$backgroundSi === void 0 ? '100% 100%' : _pattern$backgroundSi,
572
645
  _pattern = (0, _objectWithoutProperties2["default"])(pattern, _excluded2);
573
- return 'center top / ' + backgroundSize + ' url("data:image/svg+xml,' + encodeURIComponent((0, _server.renderToStaticMarkup)(/*#__PURE__*/React.createElement(SvgBackground, {
646
+ // 替换原 SVG DataURL Canvas DataURL
647
+ var canvasDataUrl = renderCanvasBackground({
574
648
  fill: fill,
575
649
  pattern: _pattern
576
- }))) + '")';
577
- } catch (e) {}
650
+ });
651
+ return 'center top / ' + backgroundSize + ' url("' + canvasDataUrl + '")';
652
+ } catch (e) {
653
+ console.error('Canvas 绘制背景失败:', e);
654
+ }
578
655
  return "";
579
656
  };
580
657
  var getBandwidth = exports.getBandwidth = function getBandwidth(step, paddingOuter) {
581
658
  return step * (1 - paddingOuter);
582
659
  };
583
- var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function getBandSeriesStepAndWidth(_ref18) {
584
- var width = _ref18.width,
585
- paddingInner = _ref18.paddingInner,
586
- bandLength = _ref18.bandLength;
660
+ var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function getBandSeriesStepAndWidth(_ref20) {
661
+ var width = _ref20.width,
662
+ paddingInner = _ref20.paddingInner,
663
+ bandLength = _ref20.bandLength;
587
664
  var seriesStep = width / (bandLength == 0 ? 1 : bandLength);
588
665
  var seriesWidth = seriesStep * (1 - paddingInner);
589
666
  return {
@@ -591,14 +668,14 @@ var getBandSeriesStepAndWidth = exports.getBandSeriesStepAndWidth = function get
591
668
  seriesWidth: seriesWidth
592
669
  };
593
670
  };
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;
671
+ var getSeriesInfo = exports.getSeriesInfo = function getSeriesInfo(_ref21) {
672
+ var step = _ref21.step,
673
+ _ref21$bandLength = _ref21.bandLength,
674
+ bandLength = _ref21$bandLength === void 0 ? 1 : _ref21$bandLength,
675
+ _ref21$paddingInner = _ref21.paddingInner,
676
+ paddingInner = _ref21$paddingInner === void 0 ? 0 : _ref21$paddingInner,
677
+ _ref21$paddingOuter = _ref21.paddingOuter,
678
+ paddingOuter = _ref21$paddingOuter === void 0 ? 0 : _ref21$paddingOuter;
602
679
  if (bandLength == 0) return {
603
680
  seriesWidth: step,
604
681
  seriesStep: step,
@@ -705,54 +782,29 @@ var sortPie = exports.sortPie = function sortPie(data, order) {
705
782
  });
706
783
  switch (order) {
707
784
  case '':
708
- _data.sort(function (_ref20, _ref21) {
709
- var a = _ref20.index;
710
- var b = _ref21.index;
785
+ _data.sort(function (_ref22, _ref23) {
786
+ var a = _ref22.index;
787
+ var b = _ref23.index;
711
788
  return (0, _d3v.ascending)(a, b);
712
789
  });
713
790
  break;
714
791
  case 'desc':
715
- _data.sort(function (_ref22, _ref23) {
716
- var a = _ref22.value;
717
- var b = _ref23.value;
792
+ _data.sort(function (_ref24, _ref25) {
793
+ var a = _ref24.value;
794
+ var b = _ref25.value;
718
795
  return (0, _d3v.descending)(a, b);
719
796
  });
720
797
  break;
721
798
  case 'asc':
722
- _data.sort(function (_ref24, _ref25) {
723
- var a = _ref24.value;
724
- var b = _ref25.value;
799
+ _data.sort(function (_ref26, _ref27) {
800
+ var a = _ref26.value;
801
+ var b = _ref27.value;
725
802
  return (0, _d3v.ascending)(a, b);
726
803
  });
727
804
  break;
728
805
  }
729
806
  return _data;
730
807
  };
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
808
  var getDataWithPercent = exports.getDataWithPercent = function getDataWithPercent() {
757
809
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
758
810
  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.5",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -44,6 +44,8 @@ const Chart = memo(
44
44
  emitEvent,
45
45
  ...props
46
46
  }) => {
47
+ console.log("888");
48
+
47
49
  const { width, height } = props;
48
50
  const isIOS = useRef(
49
51
  /iPad|iPhone|iPod|iOS/i.test(navigator.userAgent) ||
@@ -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)` }} />}
@@ -18,6 +18,74 @@ 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
+ };
21
89
  const SvgBackground = ({
22
90
  fill: {
23
91
  type,
@@ -76,20 +144,21 @@ const getColorList = ({ type, pure, linear: { stops, angle, opacity } }) => {
76
144
  }
77
145
  return stops.map(({ color, offset }) => ({ color, offset: offset / 100 }));
78
146
  };
79
- const getMultiColorStr = (colors)=>{
80
- if(colors){
147
+ const getMultiColorStr = (colors) => {
148
+ if (colors) {
81
149
  const { stops, angle } = colors;
82
150
  let result = 'linear-gradient(' + angle + 'deg, ';
83
151
  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++){
152
+ stops_.sort((a, b) => a.offset - b.offset);
153
+ for (let i = 0; i < stops_.length; i++) {
86
154
  const { color, offset } = stops_[i];
87
155
  result += `${color} ${offset}%,`;
88
156
  }
89
- return result.slice(0,-1)+")";
157
+ return result.slice(0, -1) + ')';
90
158
  }
91
- }
92
- const getIcon = (type, icon, lineType="solid") => {
159
+ };
160
+
161
+ const getIcon = (type, icon, lineType = "solid") => {
93
162
  switch (type) {
94
163
  case 'area':
95
164
  case 'line':
@@ -99,7 +168,7 @@ const getIcon = (type, icon, lineType="solid") => {
99
168
  ...defaultLineIcon,
100
169
  ...icon,
101
170
  minWidth: icon.width,
102
- background:lineType=="solid"?color:`linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
171
+ background: lineType == "solid" ? color : `linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
103
172
  }
104
173
  : defaultLineIcon;
105
174
  default:
@@ -150,6 +219,7 @@ const dateFormat = (date, fmt) => {
150
219
  );
151
220
  return fmt;
152
221
  };
222
+
153
223
  const getBreakWord = (str, breakNumber) => {
154
224
  const re = new RegExp('([^]){1,' + breakNumber + '}', 'g');
155
225
  return str.match(re);
@@ -164,27 +234,27 @@ const getTicksOfAxis = (domain, ticksCount, showLast) => {
164
234
  if (showLast) {
165
235
  let count = ticksCount, gap = 0;
166
236
  step = (len - 1) / (count - 1);
167
- const maxGap = Math.max(count-2,len-count);
237
+ const maxGap = Math.max(count - 2, len - count);
168
238
  //循环计算出最接近count值且能让step为整数的值
169
- if(!Number.isInteger(step)){
170
- while(gap<=maxGap){
239
+ if (!Number.isInteger(step)) {
240
+ while (gap <= maxGap) {
171
241
  gap++;
172
- const left = (len-1)/(count-gap-1), right = (len-1)/(count+gap-1);
173
- if(Number.isInteger(left)){
242
+ const left = (len - 1) / (count - gap - 1), right = (len - 1) / (count + gap - 1);
243
+ if (Number.isInteger(left)) {
174
244
  step = left;
175
245
  break;
176
246
  }
177
- if(Number.isInteger(right)){
247
+ if (Number.isInteger(right)) {
178
248
  step = right;
179
249
  break;
180
250
  }
181
251
  }
182
252
  }
183
- if(!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
253
+ if (!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
184
254
  ticksArr = domain.filter(function (d, i) {
185
255
  return i % step === 0;
186
256
  });
187
- }else{
257
+ } else {
188
258
  ticksArr = domain.filter(function (d, i) {
189
259
  return i % (step + 1) === 0;
190
260
  });
@@ -265,7 +335,7 @@ const getMousePos = (evt, dom) => {
265
335
  };
266
336
 
267
337
  const getFontStyle = (
268
- { color, bold, italic, fontSize, fontFamily, letterSpacing,lineHeight },
338
+ { color, bold, italic, fontSize, fontFamily, letterSpacing, lineHeight },
269
339
  type
270
340
  ) => {
271
341
  if (type == 'svg') {
@@ -276,7 +346,7 @@ const getFontStyle = (
276
346
  fill: color,
277
347
  fontWeight: bold ? 'bold' : 'normal',
278
348
  fontStyle: italic ? 'italic' : 'normal',
279
- lineHeight:lineHeight+"px"
349
+ lineHeight: lineHeight + "px"
280
350
  };
281
351
  }
282
352
  return {
@@ -286,26 +356,26 @@ const getFontStyle = (
286
356
  color,
287
357
  fontWeight: bold ? 'bold' : 'normal',
288
358
  fontStyle: italic ? 'italic' : 'normal',
289
- lineHeight:lineHeight+"px"//不加px默认是倍数关系
359
+ lineHeight: lineHeight + "px"//不加px默认是倍数关系
290
360
  };
291
361
  };
292
- const formatFont=({ color, fill, bold, italic, ...rest },
293
- type)=>{
362
+
363
+ const formatFont = ({ color, fill, bold, italic, ...rest }, type) => {
294
364
  if (type == 'svg') {
295
365
  return {
296
366
  fill: fill || color,
297
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
298
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
367
+ fontWeight: typeof bold == "string" ? bold : (bold ? 'bold' : 'normal'),
368
+ fontStyle: typeof italic == "string" ? italic : (italic ? 'italic' : 'normal'),
299
369
  ...rest
300
370
  };
301
371
  }
302
372
  return {
303
- color:color|| fill,
304
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
305
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
373
+ color: color || fill,
374
+ fontWeight: typeof bold == "string" ? bold : (bold ? 'bold' : 'normal'),
375
+ fontStyle: typeof italic == "string" ? italic : (italic ? 'italic' : 'normal'),
306
376
  ...rest
307
377
  };
308
- }
378
+ };
309
379
 
310
380
  const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
311
381
  marginTop +
@@ -316,9 +386,12 @@ const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
316
386
  'px ' +
317
387
  marginLeft +
318
388
  'px';
389
+
319
390
  const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
320
391
  'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
392
+
321
393
  const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
394
+
322
395
  function band() {
323
396
  var scale = ordinal().unknown(undefined),
324
397
  domain = scale.domain,
@@ -330,9 +403,6 @@ function band() {
330
403
  round = false,
331
404
  paddingInner = 0,
332
405
  paddingOuter = 0,
333
- // seriesPaddingInner = 0,
334
- // seriesPaddingOuter = 0,
335
- // seriesLength = 0,
336
406
  align = 0.5;
337
407
 
338
408
  delete scale.unknown;
@@ -515,49 +585,51 @@ const getCurrentStack = (stack, stackMap) =>
515
585
  _yOrZ == stack.yOrZ &&
516
586
  _s.includes(stack.name)
517
587
  );
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
- };
588
+
589
+ const reverseGradientStops = (gradient) => {
590
+ if (gradient.type !== 'linear' || !gradient.linear?.stops) {
591
+ return gradient;
542
592
  }
543
- const getBandBackground = (pattern, fill,y) => {
544
- if (!(pattern && pattern.path)){
545
- return getColor(y<0?reverseGradientStops(fill):fill);//小于0颜色翻转
593
+ const stops = gradient.linear.stops;
594
+ //先按 offset 排序(升序),确保输入是规范的
595
+ const sortedStops = [...stops].sort((a, b) => a.offset - b.offset);
596
+
597
+ // 反转每个 stop 的 offset: newOffset = 100 - oldOffset
598
+ // 颜色保持不变,只是位置镜像翻转
599
+ const reversedStops = sortedStops.map(stop => ({
600
+ ...stop,
601
+ offset: 100 - stop.offset
602
+ }));
603
+ //再按新的 offset 升序排序,确保输出结构正确
604
+ const finalStops = reversedStops.sort((a, b) => a.offset - b.offset);
605
+
606
+ return {
607
+ ...gradient,
608
+ linear: {
609
+ ...gradient.linear,
610
+ stops: finalStops
611
+ }
612
+ };
613
+ };
614
+ const getBandBackground = (pattern, fill, y) => {
615
+ if (!(pattern && pattern.path)) {
616
+ return getColor(y < 0 ? reverseGradientStops(fill) : fill);//小于0颜色翻转
546
617
  }
547
- try{
618
+ try {
548
619
  const { backgroundSize = '100% 100%', ..._pattern } = pattern;
620
+ // 替换原 SVG DataURL 为 Canvas DataURL
621
+ const canvasDataUrl = renderCanvasBackground({ fill, pattern: _pattern });
549
622
  return (
550
623
  'center top / ' +
551
624
  backgroundSize +
552
- ' url("data:image/svg+xml,' +
553
- encodeURIComponent(
554
- renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
555
- ) +
556
- '")'
625
+ ' url("' + canvasDataUrl + '")'
557
626
  );
558
- }catch(e){}
627
+ } catch (e) {
628
+ console.error('Canvas 绘制背景失败:', e);
629
+ }
559
630
  return "";
560
631
  };
632
+
561
633
  const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
562
634
 
563
635
  const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
@@ -708,37 +780,13 @@ const sortPie = (data, order) => {
708
780
  return _data;
709
781
  };
710
782
 
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
783
  const getDataWithPercent = (data = [], precision = 0) => {
736
784
  let objData = [];
737
785
  function getPercentWithPrecision(valueList, idx, precision) {
738
786
  if (!valueList[idx]) {
739
787
  return 0;
740
788
  }
741
- const sum = valueList.reduce( function (acc, val) {
789
+ const sum = valueList.reduce(function (acc, val) {
742
790
  return acc + val.value;
743
791
  }, 0);
744
792
  if (sum === 0) {
@@ -752,7 +800,7 @@ const getDataWithPercent = (data = [], precision = 0) => {
752
800
  const seats = votesPerQuota.map(function (votes) {
753
801
  return Math.floor(votes);
754
802
  });
755
- let currentSum = seats.reduce( function (acc, val) {
803
+ let currentSum = seats.reduce(function (acc, val) {
756
804
  return acc + val;
757
805
  }, 0);
758
806
  const remainder = votesPerQuota.map(function (votes, idx) {
@@ -800,10 +848,10 @@ const reduceConfig = (config = []) => {
800
848
  };
801
849
 
802
850
  //限制value的值在min和max之间
803
- const mathR=(value, range)=>{
804
- const [min,max] = range;
805
- return Math.max(min,Math.min(value,max));
806
- }
851
+ const mathR = (value, range) => {
852
+ const [min, max] = range;
853
+ return Math.max(min, Math.min(value, max));
854
+ };
807
855
 
808
856
  export {
809
857
  dateFormat,
@@ -838,5 +886,7 @@ export {
838
886
  getDataWithPercent,
839
887
  reduceConfig,
840
888
  getSeriesInfo,
841
- mathR
842
- };
889
+ mathR,
890
+ renderCanvasBackground, // 导出 Canvas 绘制方法
891
+ SvgBackground
892
+ };