@easyv/charts 1.6.20 → 1.6.21

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.
Files changed (62) hide show
  1. package/.babelrc +8 -8
  2. package/CHANGELOG.md +18 -18
  3. package/commitlint.config.js +1 -1
  4. package/lib/components/Background.js +2 -2
  5. package/lib/components/Band.js +55 -31
  6. package/lib/components/Brush.js +2 -2
  7. package/lib/components/CartesianChart.js +3 -1
  8. package/lib/components/Chart.js +2 -3
  9. package/lib/components/ChartContainer.js +2 -2
  10. package/lib/components/ConicalGradient.js +21 -21
  11. package/lib/components/ExtentData.js +2 -2
  12. package/lib/components/Indicator.js +2 -2
  13. package/lib/components/Label.js +2 -2
  14. package/lib/components/Legend.js +2 -2
  15. package/lib/components/Lighter.js +2 -2
  16. package/lib/components/Line.js +2 -2
  17. package/lib/components/LinearGradient.js +2 -2
  18. package/lib/components/StereoBar.js +2 -2
  19. package/lib/css/index.module.css +42 -42
  20. package/lib/css/piechart.module.css +26 -26
  21. package/lib/hooks/useAnimateData.js +6 -6
  22. package/lib/hooks/useAxes.js +20 -16
  23. package/lib/hooks/useFilterData.js +5 -5
  24. package/lib/hooks/useStackData.js +5 -5
  25. package/lib/hooks/useTooltip.js +11 -11
  26. package/package.json +55 -55
  27. package/src/components/Background.tsx +61 -61
  28. package/src/components/Band.tsx +321 -302
  29. package/src/components/Brush.js +159 -159
  30. package/src/components/CartesianChart.js +1 -1
  31. package/src/components/Chart.js +153 -153
  32. package/src/components/ChartContainer.tsx +71 -71
  33. package/src/components/ConicalGradient.js +258 -258
  34. package/src/components/Control.jsx +241 -241
  35. package/src/components/ExtentData.js +18 -18
  36. package/src/components/Indicator.js +59 -59
  37. package/src/components/Label.js +262 -262
  38. package/src/components/Legend.js +189 -189
  39. package/src/components/Lighter.jsx +173 -173
  40. package/src/components/Line.js +153 -153
  41. package/src/components/LinearGradient.js +29 -29
  42. package/src/components/PieTooltip.jsx +160 -160
  43. package/src/components/StereoBar.tsx +307 -307
  44. package/src/components/index.js +59 -59
  45. package/src/context/index.js +2 -2
  46. package/src/css/index.module.css +42 -42
  47. package/src/css/piechart.module.css +26 -26
  48. package/src/element/ConicGradient.jsx +55 -55
  49. package/src/element/Line.tsx +33 -33
  50. package/src/element/index.ts +3 -3
  51. package/src/formatter/index.js +1 -1
  52. package/src/formatter/legend.js +114 -114
  53. package/src/hooks/index.js +20 -20
  54. package/src/hooks/useAnimateData.ts +68 -68
  55. package/src/hooks/useAxes.js +20 -16
  56. package/src/hooks/useFilterData.js +78 -78
  57. package/src/hooks/useStackData.js +102 -102
  58. package/src/hooks/useTooltip.ts +104 -104
  59. package/src/index.js +6 -6
  60. package/src/types/index.d.ts +68 -68
  61. package/src/utils/index.js +800 -800
  62. package/tsconfig.json +23 -23
@@ -1,800 +1,800 @@
1
- import { getColor } from '@easyv/utils';
2
- import { toFixed } from '@easyv/utils/lib/common/utils';
3
- import {
4
- scaleOrdinal as ordinal,
5
- range as sequence,
6
- ascending,
7
- descending,
8
- sum,
9
- } from 'd3v7';
10
- import { renderToStaticMarkup } from 'react-dom/server';
11
- import { toPath } from 'svg-points';
12
-
13
- const defaultBackground = '#000000';
14
- const defaultIcon = {
15
- background: defaultBackground,
16
- };
17
- const defaultLineIcon = {
18
- height: 2,
19
- background: defaultBackground,
20
- };
21
- const SvgBackground = ({
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
- return (
36
- <svg
37
- preserveAspectRatio='none'
38
- xmlns='http://www.w3.org/2000/svg'
39
- width={width}
40
- height={height}
41
- >
42
- <defs>
43
- <linearGradient
44
- id='linearGradient'
45
- x1='0%'
46
- y1='0%'
47
- x2='0%'
48
- y2='100%'
49
- gradientTransform={'rotate(' + (angle + 180) + ', 0.5, 0.5)'}
50
- >
51
- {stops.map(({ offset, color }, index) => (
52
- <stop
53
- key={index}
54
- offset={offset + '%'}
55
- stopColor={color}
56
- stopOpacity={opacity}
57
- />
58
- ))}
59
- </linearGradient>
60
- </defs>
61
- <path
62
- d={path}
63
- fill={type === 'pure' ? pure : 'url(#linearGradient)'}
64
- stroke={stroke}
65
- strokeWidth={boderWidth}
66
- />
67
- </svg>
68
- );
69
- };
70
- const getColorList = ({ type, pure, linear: { stops, angle, opacity } }) => {
71
- if (type == 'pure') {
72
- return [
73
- { color: pure, offset: 1 },
74
- { color: pure, offset: 0 },
75
- ];
76
- }
77
- return stops.map(({ color, offset }) => ({ color, offset: offset / 100 }));
78
- };
79
- const getIcon = (type, icon, lineType="solid") => {
80
- switch (type) {
81
- case 'area':
82
- case 'line':
83
- let color = icon.background;
84
- return icon
85
- ? {
86
- ...defaultLineIcon,
87
- ...icon,
88
- minWidth: icon.width,
89
- background:lineType=="solid"?color:`linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
90
- }
91
- : defaultLineIcon;
92
- default:
93
- return icon
94
- ? {
95
- ...defaultIcon,
96
- ...icon,
97
- minWidth: icon.width,
98
- }
99
- : defaultIcon;
100
- }
101
- };
102
-
103
- const dateFormat = (date, fmt) => {
104
- date = new Date(date);
105
- const o = {
106
- 'M+': date.getMonth() + 1, //月份
107
- 'D+': date.getDate(), //日
108
- 'H+': date.getHours(), //小时
109
- 'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
110
- 'm+': date.getMinutes(), //分
111
- 's+': date.getSeconds(), //秒
112
- S: date.getMilliseconds(), //毫秒
113
- X: '星期' + '日一二三四五六'.charAt(date.getDay()),
114
- W: new Array(
115
- 'Sunday',
116
- 'Monday',
117
- 'Tuesday',
118
- 'Wednesday',
119
- 'Thursday',
120
- 'Friday',
121
- 'Saturday'
122
- )[date.getDay()],
123
- w: new Array('Sun.', 'Mon.', ' Tues.', 'Wed.', ' Thur.', 'Fri.', 'Sat.')[
124
- date.getDay()
125
- ],
126
- };
127
- if (/(Y+)/.test(fmt))
128
- fmt = fmt.replace(
129
- RegExp.$1,
130
- (date.getFullYear() + '').substr(4 - RegExp.$1.length)
131
- );
132
- for (var k in o)
133
- if (new RegExp('(' + k + ')').test(fmt))
134
- fmt = fmt.replace(
135
- RegExp.$1,
136
- RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
137
- );
138
- return fmt;
139
- };
140
- const getBreakWord = (str, breakNumber) => {
141
- const re = new RegExp('([^]){1,' + breakNumber + '}', 'g');
142
- return str.match(re);
143
- };
144
-
145
- //x轴标签逻辑
146
- const getTicksOfAxis = (domain, ticksCount, showLast) => {
147
- let len = domain.length;
148
- if (ticksCount < 2 || ticksCount > len) return domain;
149
- let step = Math.floor((len - ticksCount) / (ticksCount - 1));
150
- let ticksArr = [];
151
- if (showLast) {
152
- let count = ticksCount, gap = 0;
153
- step = (len - 1) / (count - 1);
154
- const maxGap = Math.max(count-2,len-count);
155
- //循环计算出最接近count值且能让step为整数的值
156
- if(!Number.isInteger(step)){
157
- while(gap<=maxGap){
158
- gap++;
159
- const left = (len-1)/(count-gap-1), right = (len-1)/(count+gap-1);
160
- if(Number.isInteger(left)){
161
- step = left;
162
- break;
163
- }
164
- if(Number.isInteger(right)){
165
- step = right;
166
- break;
167
- }
168
- }
169
- }
170
- if(!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
171
- ticksArr = domain.filter(function (d, i) {
172
- return i % step === 0;
173
- });
174
- }else{
175
- ticksArr = domain.filter(function (d, i) {
176
- return i % (step + 1) === 0;
177
- });
178
- }
179
- return ticksArr;
180
- };
181
-
182
- const getTickCoord = ({
183
- orientation,
184
- coordinate,
185
- tickSize = 6,
186
- x = 0,
187
- y = 0,
188
- }) => {
189
- let x1, x2, y1, y2;
190
- switch (orientation) {
191
- case 'top':
192
- x1 = x2 = coordinate;
193
- y2 = y;
194
- y1 = y2 - tickSize;
195
- break;
196
- case 'left':
197
- y1 = y2 = coordinate;
198
- x2 = x;
199
- x1 = x2 - tickSize;
200
- break;
201
- case 'right':
202
- y1 = y2 = coordinate;
203
- x2 = x;
204
- x1 = x2 + tickSize;
205
- break;
206
- default:
207
- x1 = x2 = coordinate;
208
- y2 = y;
209
- y1 = y2 + tickSize;
210
- break;
211
- }
212
- return { x1, x2, y1, y2 };
213
- };
214
- const getGridCoord = ({ orientation, coordinate, end }) => {
215
- let x1, x2, y1, y2;
216
- switch (orientation) {
217
- case 'top':
218
- x1 = x2 = coordinate;
219
- y1 = 0;
220
- y2 = end;
221
- break;
222
- case 'bottom':
223
- x1 = x2 = coordinate;
224
- y1 = 0;
225
- y2 = end * -1;
226
- break;
227
- case 'left':
228
- y1 = y2 = coordinate;
229
- x1 = 0;
230
- x2 = end;
231
- break;
232
- case 'right':
233
- y1 = y2 = coordinate;
234
- x1 = 0;
235
- x2 = end * -1;
236
- break;
237
- }
238
- return { x1, x2, y1, y2 };
239
- };
240
-
241
- const identity = (d) => d;
242
-
243
- //获取鼠标指针坐标
244
- const getMousePos = (evt, dom) => {
245
- var rect = dom.getBoundingClientRect();
246
- return {
247
- x: evt.clientX - rect.left,
248
- y: evt.clientY - rect.top,
249
- w: rect.width,
250
- h: rect.height,
251
- };
252
- };
253
-
254
- const getFontStyle = (
255
- { color, bold, italic, fontSize, fontFamily, letterSpacing },
256
- type
257
- ) => {
258
- if (type == 'svg') {
259
- return {
260
- fontSize,
261
- fontFamily,
262
- letterSpacing,
263
- fill: color,
264
- fontWeight: bold ? 'bold' : 'normal',
265
- fontStyle: italic ? 'italic' : 'normal',
266
- };
267
- }
268
- return {
269
- fontSize,
270
- fontFamily,
271
- letterSpacing,
272
- color,
273
- fontWeight: bold ? 'bold' : 'normal',
274
- fontStyle: italic ? 'italic' : 'normal',
275
- };
276
- };
277
- const formatFont=({ color, fill, bold, italic, ...rest },
278
- type)=>{
279
- if (type == 'svg') {
280
- return {
281
- fill: fill || color,
282
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
283
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
284
- ...rest
285
- };
286
- }
287
- return {
288
- color:color|| fill,
289
- fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
290
- fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
291
- ...rest
292
- };
293
- }
294
-
295
- const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
296
- marginTop +
297
- 'px ' +
298
- marginRight +
299
- 'px ' +
300
- marginBottom +
301
- 'px ' +
302
- marginLeft +
303
- 'px';
304
- const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
305
- 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
306
- const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
307
- function band() {
308
- var scale = ordinal().unknown(undefined),
309
- domain = scale.domain,
310
- ordinalRange = scale.range,
311
- r0 = 0,
312
- r1 = 1,
313
- step,
314
- bandwidth,
315
- round = false,
316
- paddingInner = 0,
317
- paddingOuter = 0,
318
- // seriesPaddingInner = 0,
319
- // seriesPaddingOuter = 0,
320
- // seriesLength = 0,
321
- align = 0.5;
322
-
323
- delete scale.unknown;
324
-
325
- function rescale() {
326
- var n = domain().length,
327
- reverse = r1 < r0,
328
- start = reverse ? r1 : r0,
329
- stop = reverse ? r0 : r1;
330
- step = (stop - start) / Math.max(1, n - paddingOuter * 2);
331
- if (round) step = Math.floor(step);
332
- start += (stop - start - step * n) * align;
333
- bandwidth = step;
334
- if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
335
- var values = sequence(n).map(function (i) {
336
- return start + step * i + step / 2;
337
- });
338
- return ordinalRange(reverse ? values.reverse() : values);
339
- }
340
-
341
- scale.domain = function (_) {
342
- return arguments.length ? (domain(_), rescale()) : domain();
343
- };
344
-
345
- scale.range = function (_) {
346
- return arguments.length
347
- ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
348
- : [r0, r1];
349
- };
350
-
351
- scale.rangeRound = function (_) {
352
- return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
353
- };
354
-
355
- scale.bandwidth = function () {
356
- return bandwidth;
357
- };
358
-
359
- scale.step = function () {
360
- return step;
361
- };
362
-
363
- scale.seriesBandwidth = function () {
364
- return seriesBandwidth;
365
- };
366
-
367
- scale.seriesStep = function () {
368
- return seriesStep;
369
- };
370
-
371
- scale.round = function (_) {
372
- return arguments.length ? ((round = !!_), rescale()) : round;
373
- };
374
-
375
- scale.padding = function (_) {
376
- return arguments.length
377
- ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
378
- : paddingInner;
379
- };
380
-
381
- scale.paddingInner = function (_) {
382
- return arguments.length
383
- ? ((paddingInner = Math.min(1, _)), rescale())
384
- : paddingInner;
385
- };
386
-
387
- scale.paddingOuter = function (_) {
388
- return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
389
- };
390
-
391
- scale.align = function (_) {
392
- return arguments.length
393
- ? ((align = Math.max(0, Math.min(1, _))), rescale())
394
- : align;
395
- };
396
-
397
- scale.copy = function () {
398
- return band(domain(), [r0, r1])
399
- .round(round)
400
- .paddingInner(paddingInner)
401
- .paddingOuter(paddingOuter)
402
- .align(align);
403
- };
404
-
405
- return initRange.apply(rescale(), arguments);
406
- }
407
-
408
- function initRange(domain, range) {
409
- switch (arguments.length) {
410
- case 0:
411
- break;
412
- case 1:
413
- this.range(domain);
414
- break;
415
- default:
416
- this.range(range).domain(domain);
417
- break;
418
- }
419
- return this;
420
- }
421
-
422
- const getStacks = (series) => {
423
- const tmp = [];
424
- series.forEach(({ type, stack, yOrZ }, name) => {
425
- const current = tmp.find(
426
- ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
427
- _type == type && stack && _stack == stack && yOrZ == _yOrZ
428
- );
429
- if (!current) {
430
- const common = {
431
- type,
432
- stack,
433
- positive: 0,
434
- negative: 0,
435
- yOrZ,
436
- s: [name],
437
- };
438
- if (type === 'band') {
439
- const index = tmp.filter((item) => item.type === 'band').length;
440
- tmp.push({
441
- ...common,
442
- index,
443
- });
444
- } else {
445
- tmp.push({
446
- ...common,
447
- index: 0,
448
- });
449
- }
450
- } else {
451
- current.s.push(name);
452
- }
453
- });
454
- return tmp;
455
- };
456
-
457
- const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
458
- const tmp = {
459
- y: [],
460
- z: [],
461
- };
462
- for (let i = 0, j = data.length; i < j; i++) {
463
- const d = data[i];
464
- if (seriesY.get(d.s)) {
465
- tmp.y.push(d);
466
- continue;
467
- }
468
- if (seriesZ.get(d.s)) {
469
- tmp.z.push(d);
470
- }
471
- }
472
- return tmp;
473
- };
474
-
475
- const seriesYOrZ = (series) => {
476
- const y = new Map();
477
- const z = new Map();
478
- series.forEach((value, key) => {
479
- if (value.yOrZ === 'y') {
480
- y.set(key, value);
481
- } else {
482
- z.set(key, value);
483
- }
484
- });
485
- return { y, z };
486
- };
487
-
488
- const resetStacks = (stacks) => {
489
- stacks.forEach((stack) => {
490
- stack.positive = 0;
491
- stack.negative = 0;
492
- });
493
- };
494
-
495
- const getCurrentStack = (stack, stackMap) =>
496
- stackMap.find(
497
- ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
498
- _type == stack.type &&
499
- _stack == stack.stack &&
500
- _yOrZ == stack.yOrZ &&
501
- _s.includes(stack.name)
502
- );
503
-
504
- const getBandBackground = (pattern, fill) => {
505
- if (!(pattern && pattern.path)) return getColor(fill);
506
- try{
507
- const { backgroundSize = '100% 100%', ..._pattern } = pattern;
508
- return (
509
- 'center top / ' +
510
- backgroundSize +
511
- ' url("data:image/svg+xml,' +
512
- encodeURIComponent(
513
- renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
514
- ) +
515
- '")'
516
- );
517
- }catch(e){}
518
- return "";
519
- };
520
- const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
521
-
522
- const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
523
- const seriesStep = width / (bandLength == 0 ? 1 : bandLength);
524
- const seriesWidth = seriesStep * (1 - paddingInner);
525
- return {
526
- seriesStep,
527
- seriesWidth,
528
- };
529
- };
530
-
531
- const getSeriesInfo = ({
532
- step,
533
- bandLength = 1,
534
- paddingInner = 0,
535
- paddingOuter = 0,
536
- }) => {
537
- if (bandLength == 0)
538
- return {
539
- seriesWidth: step,
540
- seriesStep: step,
541
- seriesStart: 0,
542
- width: step,
543
- };
544
- const _step =
545
- step / (bandLength + paddingOuter * 2 + paddingInner * (bandLength - 1));
546
- return {
547
- seriesWidth: _step,
548
- seriesStep: (1 + paddingInner) * _step,
549
- seriesStart: paddingOuter * _step,
550
- width: step - paddingOuter * 2 * _step,
551
- };
552
- };
553
-
554
- const isValidHttpUrl = (string) => {
555
- let url;
556
-
557
- try {
558
- url = new URL(string);
559
- } catch (_) {
560
- return false;
561
- }
562
-
563
- return url.protocol === 'http:' || url.protocol === 'https:';
564
- };
565
-
566
- const getChildren = (svgStr) => {
567
- const wrapper = document.createElement('div');
568
- wrapper.innerHTML = svgStr;
569
- const { childNodes } = wrapper;
570
- const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
571
-
572
- if (!!svgDom) {
573
- return [...svgDom.childNodes];
574
- }
575
-
576
- return null;
577
- };
578
-
579
- const filterChildren = (children, tagNames) => {
580
- return children.reduce((prev, node) => {
581
- let { nodeName } = node;
582
-
583
- if (tagNames.indexOf(nodeName) > -1) {
584
- if (nodeName === 'g') {
585
- return filterChildren([...node.childNodes], tagNames);
586
- } else {
587
- prev.push(node);
588
- }
589
- }
590
-
591
- return prev;
592
- }, []);
593
- };
594
-
595
- const getDomPath = (node) => {
596
- switch (node.nodeName) {
597
- case 'circle':
598
- return toPath({
599
- type: 'circle',
600
- cx: +node.getAttribute('cx') || 0,
601
- cy: +node.getAttribute('cy') || 0,
602
- r: +node.getAttribute('r') || 0,
603
- });
604
-
605
- case 'ellipse':
606
- return toPath({
607
- type: 'ellipse',
608
- cx: +node.getAttribute('cx') || 0,
609
- cy: +node.getAttribute('cy') || 0,
610
- rx: +node.getAttribute('rx') || 0,
611
- ry: +node.getAttribute('ry') || 0,
612
- });
613
-
614
- case 'line':
615
- return toPath({
616
- type: 'line',
617
- x1: +node.getAttribute('x1') || 0,
618
- x2: +node.getAttribute('x2') || 0,
619
- y1: +node.getAttribute('y1') || 0,
620
- y2: +node.getAttribute('y2') || 0,
621
- });
622
-
623
- case 'path':
624
- return toPath({
625
- type: 'path',
626
- d: node.getAttribute('d') || '',
627
- });
628
-
629
- case 'polygon':
630
- return toPath({
631
- type: 'polyline',
632
- points: node.getAttribute('points') || '',
633
- });
634
-
635
- case 'polyline':
636
- return toPath({
637
- type: 'polyline',
638
- points: node.getAttribute('points') || '',
639
- });
640
-
641
- case 'rect':
642
- return toPath({
643
- type: 'rect',
644
- height: +node.getAttribute('height') || 0,
645
- width: +node.getAttribute('width') || 0,
646
- x: +node.getAttribute('x') || 0,
647
- y: +node.getAttribute('y') || 0,
648
- rx: +node.getAttribute('rx') || 0,
649
- ry: +node.getAttribute('ry') || 0,
650
- });
651
- }
652
- };
653
-
654
- const sortPie = (data, order) => {
655
- const _data = data.map((item) => ({ ...item }));
656
- switch (order) {
657
- case '':
658
- _data.sort(({ index: a }, { index: b }) => ascending(a, b));
659
- break;
660
- case 'desc':
661
- _data.sort(({ value: a }, { value: b }) => descending(a, b));
662
- break;
663
- case 'asc':
664
- _data.sort(({ value: a }, { value: b }) => ascending(a, b));
665
- break;
666
- }
667
- return _data;
668
- };
669
-
670
- // const getDataWithPercent = (data = [], precision = 0, type) => {
671
- // const digits = Math.pow(10, precision);
672
- // const targetSeats = digits * 100;
673
-
674
- // const total = sum(data, (d) => d.value);
675
-
676
- // const votesPerQuota = data.map((d, index) => ({
677
- // ...d,
678
- // vote: Math.round((d.value / total) * digits * 100),
679
- // index,
680
- // }));
681
- // const currentSum = sum(votesPerQuota, (d) => d.vote);
682
- // const remainder = targetSeats - currentSum;
683
- // console.log(type+":",votesPerQuota, toFixed);
684
- // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
685
-
686
- // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
687
- // ...data,
688
- // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
689
- // }));
690
-
691
- // return tmp;
692
- // };
693
-
694
- const getDataWithPercent = (data = [], precision = 0) => {
695
- let objData = [];
696
- function getPercentWithPrecision(valueList, idx, precision) {
697
- if (!valueList[idx]) {
698
- return 0;
699
- }
700
- const sum = valueList.reduce( function (acc, val) {
701
- return acc + val.value;
702
- }, 0);
703
- if (sum === 0) {
704
- return 0;
705
- }
706
- const digits = Math.pow(10, precision);
707
- const votesPerQuota = valueList.map(function (val) {
708
- return val.value / sum * digits * 100;
709
- });
710
- const targetSeats = digits * 100;
711
- const seats = votesPerQuota.map(function (votes) {
712
- return Math.floor(votes);
713
- });
714
- let currentSum = seats.reduce( function (acc, val) {
715
- return acc + val;
716
- }, 0);
717
- const remainder = votesPerQuota.map(function (votes, idx) {
718
- return votes - seats[idx];
719
- });
720
- while (currentSum < targetSeats) {
721
- let max = Number.NEGATIVE_INFINITY;
722
- let maxId = null;
723
- for (let i = 0, len = remainder.length; i < len; ++i) {
724
- if (remainder[i] > max) {
725
- max = remainder[i];
726
- maxId = i;
727
- }
728
- }
729
- ++seats[maxId];
730
- remainder[maxId] = 0;
731
- ++currentSum;
732
- }
733
- return seats[idx] / digits;
734
- }
735
- data.forEach((d, i) => {
736
- objData.push({
737
- ...d,
738
- percent: getPercentWithPrecision(data, i, precision)
739
- })
740
- });
741
- return objData
742
- };
743
-
744
- const excludeTypes = ['array', 'object', 'group', 'modal', 'colors'];
745
- const reduceConfig = (config = []) => {
746
- if (!Array.isArray(config)) {
747
- return config;
748
- }
749
- let output = {};
750
- for (let i = 0, len = config.length; i < len; i++) {
751
- let type = config[i]._type;
752
-
753
- output[config[i]._name] =
754
- type && !excludeTypes.includes(type)
755
- ? config[i]._value
756
- : reduceConfig(config[i]._value);
757
- }
758
- return output;
759
- };
760
-
761
- //限制value的值在min和max之间
762
- const mathR=(value, range)=>{
763
- const [min,max] = range;
764
- return Math.max(min,Math.min(value,max));
765
- }
766
-
767
- export {
768
- dateFormat,
769
- getBreakWord,
770
- getTicksOfAxis,
771
- getTickCoord,
772
- getGridCoord,
773
- identity,
774
- getMousePos,
775
- getFontStyle,
776
- formatFont,
777
- getMargin,
778
- getTranslate3d,
779
- getTranslate2d,
780
- band,
781
- getIcon,
782
- getColorList,
783
- getStacks,
784
- dataYOrZ,
785
- seriesYOrZ,
786
- resetStacks,
787
- getCurrentStack,
788
- getBandBackground,
789
- getBandwidth,
790
- getBandSeriesStepAndWidth,
791
- isValidHttpUrl,
792
- getChildren,
793
- filterChildren,
794
- getDomPath,
795
- sortPie,
796
- getDataWithPercent,
797
- reduceConfig,
798
- getSeriesInfo,
799
- mathR
800
- };
1
+ import { getColor } from '@easyv/utils';
2
+ import { toFixed } from '@easyv/utils/lib/common/utils';
3
+ import {
4
+ scaleOrdinal as ordinal,
5
+ range as sequence,
6
+ ascending,
7
+ descending,
8
+ sum,
9
+ } from 'd3v7';
10
+ import { renderToStaticMarkup } from 'react-dom/server';
11
+ import { toPath } from 'svg-points';
12
+
13
+ const defaultBackground = '#000000';
14
+ const defaultIcon = {
15
+ background: defaultBackground,
16
+ };
17
+ const defaultLineIcon = {
18
+ height: 2,
19
+ background: defaultBackground,
20
+ };
21
+ const SvgBackground = ({
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
+ return (
36
+ <svg
37
+ preserveAspectRatio='none'
38
+ xmlns='http://www.w3.org/2000/svg'
39
+ width={width}
40
+ height={height}
41
+ >
42
+ <defs>
43
+ <linearGradient
44
+ id='linearGradient'
45
+ x1='0%'
46
+ y1='0%'
47
+ x2='0%'
48
+ y2='100%'
49
+ gradientTransform={'rotate(' + (angle + 180) + ', 0.5, 0.5)'}
50
+ >
51
+ {stops.map(({ offset, color }, index) => (
52
+ <stop
53
+ key={index}
54
+ offset={offset + '%'}
55
+ stopColor={color}
56
+ stopOpacity={opacity}
57
+ />
58
+ ))}
59
+ </linearGradient>
60
+ </defs>
61
+ <path
62
+ d={path}
63
+ fill={type === 'pure' ? pure : 'url(#linearGradient)'}
64
+ stroke={stroke}
65
+ strokeWidth={boderWidth}
66
+ />
67
+ </svg>
68
+ );
69
+ };
70
+ const getColorList = ({ type, pure, linear: { stops, angle, opacity } }) => {
71
+ if (type == 'pure') {
72
+ return [
73
+ { color: pure, offset: 1 },
74
+ { color: pure, offset: 0 },
75
+ ];
76
+ }
77
+ return stops.map(({ color, offset }) => ({ color, offset: offset / 100 }));
78
+ };
79
+ const getIcon = (type, icon, lineType="solid") => {
80
+ switch (type) {
81
+ case 'area':
82
+ case 'line':
83
+ let color = icon.background;
84
+ return icon
85
+ ? {
86
+ ...defaultLineIcon,
87
+ ...icon,
88
+ minWidth: icon.width,
89
+ background:lineType=="solid"?color:`linear-gradient(90deg, ${color}, ${color} 66%, transparent 66%) 0 0/33% 100% repeat`
90
+ }
91
+ : defaultLineIcon;
92
+ default:
93
+ return icon
94
+ ? {
95
+ ...defaultIcon,
96
+ ...icon,
97
+ minWidth: icon.width,
98
+ }
99
+ : defaultIcon;
100
+ }
101
+ };
102
+
103
+ const dateFormat = (date, fmt) => {
104
+ date = new Date(date);
105
+ const o = {
106
+ 'M+': date.getMonth() + 1, //月份
107
+ 'D+': date.getDate(), //日
108
+ 'H+': date.getHours(), //小时
109
+ 'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, //小时
110
+ 'm+': date.getMinutes(), //分
111
+ 's+': date.getSeconds(), //秒
112
+ S: date.getMilliseconds(), //毫秒
113
+ X: '星期' + '日一二三四五六'.charAt(date.getDay()),
114
+ W: new Array(
115
+ 'Sunday',
116
+ 'Monday',
117
+ 'Tuesday',
118
+ 'Wednesday',
119
+ 'Thursday',
120
+ 'Friday',
121
+ 'Saturday'
122
+ )[date.getDay()],
123
+ w: new Array('Sun.', 'Mon.', ' Tues.', 'Wed.', ' Thur.', 'Fri.', 'Sat.')[
124
+ date.getDay()
125
+ ],
126
+ };
127
+ if (/(Y+)/.test(fmt))
128
+ fmt = fmt.replace(
129
+ RegExp.$1,
130
+ (date.getFullYear() + '').substr(4 - RegExp.$1.length)
131
+ );
132
+ for (var k in o)
133
+ if (new RegExp('(' + k + ')').test(fmt))
134
+ fmt = fmt.replace(
135
+ RegExp.$1,
136
+ RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
137
+ );
138
+ return fmt;
139
+ };
140
+ const getBreakWord = (str, breakNumber) => {
141
+ const re = new RegExp('([^]){1,' + breakNumber + '}', 'g');
142
+ return str.match(re);
143
+ };
144
+
145
+ //x轴标签逻辑
146
+ const getTicksOfAxis = (domain, ticksCount, showLast) => {
147
+ let len = domain.length;
148
+ if (ticksCount < 2 || ticksCount > len) return domain;
149
+ let step = Math.floor((len - ticksCount) / (ticksCount - 1));
150
+ let ticksArr = [];
151
+ if (showLast) {
152
+ let count = ticksCount, gap = 0;
153
+ step = (len - 1) / (count - 1);
154
+ const maxGap = Math.max(count-2,len-count);
155
+ //循环计算出最接近count值且能让step为整数的值
156
+ if(!Number.isInteger(step)){
157
+ while(gap<=maxGap){
158
+ gap++;
159
+ const left = (len-1)/(count-gap-1), right = (len-1)/(count+gap-1);
160
+ if(Number.isInteger(left)){
161
+ step = left;
162
+ break;
163
+ }
164
+ if(Number.isInteger(right)){
165
+ step = right;
166
+ break;
167
+ }
168
+ }
169
+ }
170
+ if(!Number.isInteger(step)) step = 1; //如果找不到整数的step,直接取1,返回所有刻度
171
+ ticksArr = domain.filter(function (d, i) {
172
+ return i % step === 0;
173
+ });
174
+ }else{
175
+ ticksArr = domain.filter(function (d, i) {
176
+ return i % (step + 1) === 0;
177
+ });
178
+ }
179
+ return ticksArr;
180
+ };
181
+
182
+ const getTickCoord = ({
183
+ orientation,
184
+ coordinate,
185
+ tickSize = 6,
186
+ x = 0,
187
+ y = 0,
188
+ }) => {
189
+ let x1, x2, y1, y2;
190
+ switch (orientation) {
191
+ case 'top':
192
+ x1 = x2 = coordinate;
193
+ y2 = y;
194
+ y1 = y2 - tickSize;
195
+ break;
196
+ case 'left':
197
+ y1 = y2 = coordinate;
198
+ x2 = x;
199
+ x1 = x2 - tickSize;
200
+ break;
201
+ case 'right':
202
+ y1 = y2 = coordinate;
203
+ x2 = x;
204
+ x1 = x2 + tickSize;
205
+ break;
206
+ default:
207
+ x1 = x2 = coordinate;
208
+ y2 = y;
209
+ y1 = y2 + tickSize;
210
+ break;
211
+ }
212
+ return { x1, x2, y1, y2 };
213
+ };
214
+ const getGridCoord = ({ orientation, coordinate, end }) => {
215
+ let x1, x2, y1, y2;
216
+ switch (orientation) {
217
+ case 'top':
218
+ x1 = x2 = coordinate;
219
+ y1 = 0;
220
+ y2 = end;
221
+ break;
222
+ case 'bottom':
223
+ x1 = x2 = coordinate;
224
+ y1 = 0;
225
+ y2 = end * -1;
226
+ break;
227
+ case 'left':
228
+ y1 = y2 = coordinate;
229
+ x1 = 0;
230
+ x2 = end;
231
+ break;
232
+ case 'right':
233
+ y1 = y2 = coordinate;
234
+ x1 = 0;
235
+ x2 = end * -1;
236
+ break;
237
+ }
238
+ return { x1, x2, y1, y2 };
239
+ };
240
+
241
+ const identity = (d) => d;
242
+
243
+ //获取鼠标指针坐标
244
+ const getMousePos = (evt, dom) => {
245
+ var rect = dom.getBoundingClientRect();
246
+ return {
247
+ x: evt.clientX - rect.left,
248
+ y: evt.clientY - rect.top,
249
+ w: rect.width,
250
+ h: rect.height,
251
+ };
252
+ };
253
+
254
+ const getFontStyle = (
255
+ { color, bold, italic, fontSize, fontFamily, letterSpacing },
256
+ type
257
+ ) => {
258
+ if (type == 'svg') {
259
+ return {
260
+ fontSize,
261
+ fontFamily,
262
+ letterSpacing,
263
+ fill: color,
264
+ fontWeight: bold ? 'bold' : 'normal',
265
+ fontStyle: italic ? 'italic' : 'normal',
266
+ };
267
+ }
268
+ return {
269
+ fontSize,
270
+ fontFamily,
271
+ letterSpacing,
272
+ color,
273
+ fontWeight: bold ? 'bold' : 'normal',
274
+ fontStyle: italic ? 'italic' : 'normal',
275
+ };
276
+ };
277
+ const formatFont=({ color, fill, bold, italic, ...rest },
278
+ type)=>{
279
+ if (type == 'svg') {
280
+ return {
281
+ fill: fill || color,
282
+ fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
283
+ fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
284
+ ...rest
285
+ };
286
+ }
287
+ return {
288
+ color:color|| fill,
289
+ fontWeight: typeof bold=="string" ? bold : ( bold ? 'bold' : 'normal'),
290
+ fontStyle: typeof italic=="string"? italic : (italic ? 'italic' : 'normal'),
291
+ ...rest
292
+ };
293
+ }
294
+
295
+ const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
296
+ marginTop +
297
+ 'px ' +
298
+ marginRight +
299
+ 'px ' +
300
+ marginBottom +
301
+ 'px ' +
302
+ marginLeft +
303
+ 'px';
304
+ const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
305
+ 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
306
+ const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
307
+ function band() {
308
+ var scale = ordinal().unknown(undefined),
309
+ domain = scale.domain,
310
+ ordinalRange = scale.range,
311
+ r0 = 0,
312
+ r1 = 1,
313
+ step,
314
+ bandwidth,
315
+ round = false,
316
+ paddingInner = 0,
317
+ paddingOuter = 0,
318
+ // seriesPaddingInner = 0,
319
+ // seriesPaddingOuter = 0,
320
+ // seriesLength = 0,
321
+ align = 0.5;
322
+
323
+ delete scale.unknown;
324
+
325
+ function rescale() {
326
+ var n = domain().length,
327
+ reverse = r1 < r0,
328
+ start = reverse ? r1 : r0,
329
+ stop = reverse ? r0 : r1;
330
+ step = (stop - start) / Math.max(1, n - paddingOuter * 2);
331
+ if (round) step = Math.floor(step);
332
+ start += (stop - start - step * n) * align;
333
+ bandwidth = step;
334
+ if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
335
+ var values = sequence(n).map(function (i) {
336
+ return start + step * i + step / 2;
337
+ });
338
+ return ordinalRange(reverse ? values.reverse() : values);
339
+ }
340
+
341
+ scale.domain = function (_) {
342
+ return arguments.length ? (domain(_), rescale()) : domain();
343
+ };
344
+
345
+ scale.range = function (_) {
346
+ return arguments.length
347
+ ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
348
+ : [r0, r1];
349
+ };
350
+
351
+ scale.rangeRound = function (_) {
352
+ return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
353
+ };
354
+
355
+ scale.bandwidth = function () {
356
+ return bandwidth;
357
+ };
358
+
359
+ scale.step = function () {
360
+ return step;
361
+ };
362
+
363
+ scale.seriesBandwidth = function () {
364
+ return seriesBandwidth;
365
+ };
366
+
367
+ scale.seriesStep = function () {
368
+ return seriesStep;
369
+ };
370
+
371
+ scale.round = function (_) {
372
+ return arguments.length ? ((round = !!_), rescale()) : round;
373
+ };
374
+
375
+ scale.padding = function (_) {
376
+ return arguments.length
377
+ ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
378
+ : paddingInner;
379
+ };
380
+
381
+ scale.paddingInner = function (_) {
382
+ return arguments.length
383
+ ? ((paddingInner = Math.min(1, _)), rescale())
384
+ : paddingInner;
385
+ };
386
+
387
+ scale.paddingOuter = function (_) {
388
+ return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
389
+ };
390
+
391
+ scale.align = function (_) {
392
+ return arguments.length
393
+ ? ((align = Math.max(0, Math.min(1, _))), rescale())
394
+ : align;
395
+ };
396
+
397
+ scale.copy = function () {
398
+ return band(domain(), [r0, r1])
399
+ .round(round)
400
+ .paddingInner(paddingInner)
401
+ .paddingOuter(paddingOuter)
402
+ .align(align);
403
+ };
404
+
405
+ return initRange.apply(rescale(), arguments);
406
+ }
407
+
408
+ function initRange(domain, range) {
409
+ switch (arguments.length) {
410
+ case 0:
411
+ break;
412
+ case 1:
413
+ this.range(domain);
414
+ break;
415
+ default:
416
+ this.range(range).domain(domain);
417
+ break;
418
+ }
419
+ return this;
420
+ }
421
+
422
+ const getStacks = (series) => {
423
+ const tmp = [];
424
+ series.forEach(({ type, stack, yOrZ }, name) => {
425
+ const current = tmp.find(
426
+ ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
427
+ _type == type && stack && _stack == stack && yOrZ == _yOrZ
428
+ );
429
+ if (!current) {
430
+ const common = {
431
+ type,
432
+ stack,
433
+ positive: 0,
434
+ negative: 0,
435
+ yOrZ,
436
+ s: [name],
437
+ };
438
+ if (type === 'band') {
439
+ const index = tmp.filter((item) => item.type === 'band').length;
440
+ tmp.push({
441
+ ...common,
442
+ index,
443
+ });
444
+ } else {
445
+ tmp.push({
446
+ ...common,
447
+ index: 0,
448
+ });
449
+ }
450
+ } else {
451
+ current.s.push(name);
452
+ }
453
+ });
454
+ return tmp;
455
+ };
456
+
457
+ const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
458
+ const tmp = {
459
+ y: [],
460
+ z: [],
461
+ };
462
+ for (let i = 0, j = data.length; i < j; i++) {
463
+ const d = data[i];
464
+ if (seriesY.get(d.s)) {
465
+ tmp.y.push(d);
466
+ continue;
467
+ }
468
+ if (seriesZ.get(d.s)) {
469
+ tmp.z.push(d);
470
+ }
471
+ }
472
+ return tmp;
473
+ };
474
+
475
+ const seriesYOrZ = (series) => {
476
+ const y = new Map();
477
+ const z = new Map();
478
+ series.forEach((value, key) => {
479
+ if (value.yOrZ === 'y') {
480
+ y.set(key, value);
481
+ } else {
482
+ z.set(key, value);
483
+ }
484
+ });
485
+ return { y, z };
486
+ };
487
+
488
+ const resetStacks = (stacks) => {
489
+ stacks.forEach((stack) => {
490
+ stack.positive = 0;
491
+ stack.negative = 0;
492
+ });
493
+ };
494
+
495
+ const getCurrentStack = (stack, stackMap) =>
496
+ stackMap.find(
497
+ ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
498
+ _type == stack.type &&
499
+ _stack == stack.stack &&
500
+ _yOrZ == stack.yOrZ &&
501
+ _s.includes(stack.name)
502
+ );
503
+
504
+ const getBandBackground = (pattern, fill) => {
505
+ if (!(pattern && pattern.path)) return getColor(fill);
506
+ try{
507
+ const { backgroundSize = '100% 100%', ..._pattern } = pattern;
508
+ return (
509
+ 'center top / ' +
510
+ backgroundSize +
511
+ ' url("data:image/svg+xml,' +
512
+ encodeURIComponent(
513
+ renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
514
+ ) +
515
+ '")'
516
+ );
517
+ }catch(e){}
518
+ return "";
519
+ };
520
+ const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
521
+
522
+ const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
523
+ const seriesStep = width / (bandLength == 0 ? 1 : bandLength);
524
+ const seriesWidth = seriesStep * (1 - paddingInner);
525
+ return {
526
+ seriesStep,
527
+ seriesWidth,
528
+ };
529
+ };
530
+
531
+ const getSeriesInfo = ({
532
+ step,
533
+ bandLength = 1,
534
+ paddingInner = 0,
535
+ paddingOuter = 0,
536
+ }) => {
537
+ if (bandLength == 0)
538
+ return {
539
+ seriesWidth: step,
540
+ seriesStep: step,
541
+ seriesStart: 0,
542
+ width: step,
543
+ };
544
+ const _step =
545
+ step / (bandLength + paddingOuter * 2 + paddingInner * (bandLength - 1));
546
+ return {
547
+ seriesWidth: _step,
548
+ seriesStep: (1 + paddingInner) * _step,
549
+ seriesStart: paddingOuter * _step,
550
+ width: step - paddingOuter * 2 * _step,
551
+ };
552
+ };
553
+
554
+ const isValidHttpUrl = (string) => {
555
+ let url;
556
+
557
+ try {
558
+ url = new URL(string);
559
+ } catch (_) {
560
+ return false;
561
+ }
562
+
563
+ return url.protocol === 'http:' || url.protocol === 'https:';
564
+ };
565
+
566
+ const getChildren = (svgStr) => {
567
+ const wrapper = document.createElement('div');
568
+ wrapper.innerHTML = svgStr;
569
+ const { childNodes } = wrapper;
570
+ const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
571
+
572
+ if (!!svgDom) {
573
+ return [...svgDom.childNodes];
574
+ }
575
+
576
+ return null;
577
+ };
578
+
579
+ const filterChildren = (children, tagNames) => {
580
+ return children.reduce((prev, node) => {
581
+ let { nodeName } = node;
582
+
583
+ if (tagNames.indexOf(nodeName) > -1) {
584
+ if (nodeName === 'g') {
585
+ return filterChildren([...node.childNodes], tagNames);
586
+ } else {
587
+ prev.push(node);
588
+ }
589
+ }
590
+
591
+ return prev;
592
+ }, []);
593
+ };
594
+
595
+ const getDomPath = (node) => {
596
+ switch (node.nodeName) {
597
+ case 'circle':
598
+ return toPath({
599
+ type: 'circle',
600
+ cx: +node.getAttribute('cx') || 0,
601
+ cy: +node.getAttribute('cy') || 0,
602
+ r: +node.getAttribute('r') || 0,
603
+ });
604
+
605
+ case 'ellipse':
606
+ return toPath({
607
+ type: 'ellipse',
608
+ cx: +node.getAttribute('cx') || 0,
609
+ cy: +node.getAttribute('cy') || 0,
610
+ rx: +node.getAttribute('rx') || 0,
611
+ ry: +node.getAttribute('ry') || 0,
612
+ });
613
+
614
+ case 'line':
615
+ return toPath({
616
+ type: 'line',
617
+ x1: +node.getAttribute('x1') || 0,
618
+ x2: +node.getAttribute('x2') || 0,
619
+ y1: +node.getAttribute('y1') || 0,
620
+ y2: +node.getAttribute('y2') || 0,
621
+ });
622
+
623
+ case 'path':
624
+ return toPath({
625
+ type: 'path',
626
+ d: node.getAttribute('d') || '',
627
+ });
628
+
629
+ case 'polygon':
630
+ return toPath({
631
+ type: 'polyline',
632
+ points: node.getAttribute('points') || '',
633
+ });
634
+
635
+ case 'polyline':
636
+ return toPath({
637
+ type: 'polyline',
638
+ points: node.getAttribute('points') || '',
639
+ });
640
+
641
+ case 'rect':
642
+ return toPath({
643
+ type: 'rect',
644
+ height: +node.getAttribute('height') || 0,
645
+ width: +node.getAttribute('width') || 0,
646
+ x: +node.getAttribute('x') || 0,
647
+ y: +node.getAttribute('y') || 0,
648
+ rx: +node.getAttribute('rx') || 0,
649
+ ry: +node.getAttribute('ry') || 0,
650
+ });
651
+ }
652
+ };
653
+
654
+ const sortPie = (data, order) => {
655
+ const _data = data.map((item) => ({ ...item }));
656
+ switch (order) {
657
+ case '':
658
+ _data.sort(({ index: a }, { index: b }) => ascending(a, b));
659
+ break;
660
+ case 'desc':
661
+ _data.sort(({ value: a }, { value: b }) => descending(a, b));
662
+ break;
663
+ case 'asc':
664
+ _data.sort(({ value: a }, { value: b }) => ascending(a, b));
665
+ break;
666
+ }
667
+ return _data;
668
+ };
669
+
670
+ // const getDataWithPercent = (data = [], precision = 0, type) => {
671
+ // const digits = Math.pow(10, precision);
672
+ // const targetSeats = digits * 100;
673
+
674
+ // const total = sum(data, (d) => d.value);
675
+
676
+ // const votesPerQuota = data.map((d, index) => ({
677
+ // ...d,
678
+ // vote: Math.round((d.value / total) * digits * 100),
679
+ // index,
680
+ // }));
681
+ // const currentSum = sum(votesPerQuota, (d) => d.vote);
682
+ // const remainder = targetSeats - currentSum;
683
+ // console.log(type+":",votesPerQuota, toFixed);
684
+ // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
685
+
686
+ // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
687
+ // ...data,
688
+ // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
689
+ // }));
690
+
691
+ // return tmp;
692
+ // };
693
+
694
+ const getDataWithPercent = (data = [], precision = 0) => {
695
+ let objData = [];
696
+ function getPercentWithPrecision(valueList, idx, precision) {
697
+ if (!valueList[idx]) {
698
+ return 0;
699
+ }
700
+ const sum = valueList.reduce( function (acc, val) {
701
+ return acc + val.value;
702
+ }, 0);
703
+ if (sum === 0) {
704
+ return 0;
705
+ }
706
+ const digits = Math.pow(10, precision);
707
+ const votesPerQuota = valueList.map(function (val) {
708
+ return val.value / sum * digits * 100;
709
+ });
710
+ const targetSeats = digits * 100;
711
+ const seats = votesPerQuota.map(function (votes) {
712
+ return Math.floor(votes);
713
+ });
714
+ let currentSum = seats.reduce( function (acc, val) {
715
+ return acc + val;
716
+ }, 0);
717
+ const remainder = votesPerQuota.map(function (votes, idx) {
718
+ return votes - seats[idx];
719
+ });
720
+ while (currentSum < targetSeats) {
721
+ let max = Number.NEGATIVE_INFINITY;
722
+ let maxId = null;
723
+ for (let i = 0, len = remainder.length; i < len; ++i) {
724
+ if (remainder[i] > max) {
725
+ max = remainder[i];
726
+ maxId = i;
727
+ }
728
+ }
729
+ ++seats[maxId];
730
+ remainder[maxId] = 0;
731
+ ++currentSum;
732
+ }
733
+ return seats[idx] / digits;
734
+ }
735
+ data.forEach((d, i) => {
736
+ objData.push({
737
+ ...d,
738
+ percent: getPercentWithPrecision(data, i, precision)
739
+ })
740
+ });
741
+ return objData
742
+ };
743
+
744
+ const excludeTypes = ['array', 'object', 'group', 'modal', 'colors'];
745
+ const reduceConfig = (config = []) => {
746
+ if (!Array.isArray(config)) {
747
+ return config;
748
+ }
749
+ let output = {};
750
+ for (let i = 0, len = config.length; i < len; i++) {
751
+ let type = config[i]._type;
752
+
753
+ output[config[i]._name] =
754
+ type && !excludeTypes.includes(type)
755
+ ? config[i]._value
756
+ : reduceConfig(config[i]._value);
757
+ }
758
+ return output;
759
+ };
760
+
761
+ //限制value的值在min和max之间
762
+ const mathR=(value, range)=>{
763
+ const [min,max] = range;
764
+ return Math.max(min,Math.min(value,max));
765
+ }
766
+
767
+ export {
768
+ dateFormat,
769
+ getBreakWord,
770
+ getTicksOfAxis,
771
+ getTickCoord,
772
+ getGridCoord,
773
+ identity,
774
+ getMousePos,
775
+ getFontStyle,
776
+ formatFont,
777
+ getMargin,
778
+ getTranslate3d,
779
+ getTranslate2d,
780
+ band,
781
+ getIcon,
782
+ getColorList,
783
+ getStacks,
784
+ dataYOrZ,
785
+ seriesYOrZ,
786
+ resetStacks,
787
+ getCurrentStack,
788
+ getBandBackground,
789
+ getBandwidth,
790
+ getBandSeriesStepAndWidth,
791
+ isValidHttpUrl,
792
+ getChildren,
793
+ filterChildren,
794
+ getDomPath,
795
+ sortPie,
796
+ getDataWithPercent,
797
+ reduceConfig,
798
+ getSeriesInfo,
799
+ mathR
800
+ };