@easyv/charts 1.6.12 → 1.6.14

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 (68) hide show
  1. package/.babelrc +8 -8
  2. package/.husky/commit-msg +3 -3
  3. package/CHANGELOG.md +18 -18
  4. package/commitlint.config.js +1 -1
  5. package/lib/components/Axis.js +1 -1
  6. package/lib/components/Background.js +2 -2
  7. package/lib/components/Band.js +3 -3
  8. package/lib/components/Brush.js +2 -2
  9. package/lib/components/Chart.js +2 -2
  10. package/lib/components/ChartContainer.js +3 -3
  11. package/lib/components/ConicalGradient.js +21 -21
  12. package/lib/components/Control.js +1 -1
  13. package/lib/components/ExtentData.js +2 -2
  14. package/lib/components/Indicator.js +2 -2
  15. package/lib/components/Label.js +32 -21
  16. package/lib/components/Legend.js +2 -2
  17. package/lib/components/Lighter.js +2 -2
  18. package/lib/components/Line.js +2 -2
  19. package/lib/components/LinearGradient.js +2 -2
  20. package/lib/components/Marquee.js +1 -1
  21. package/lib/components/StereoBar.js +2 -2
  22. package/lib/components/TextOverflow.js +1 -1
  23. package/lib/components/Tooltip.js +4 -4
  24. package/lib/css/index.module.css +42 -42
  25. package/lib/css/piechart.module.css +26 -26
  26. package/lib/hooks/useAnimateData.js +6 -6
  27. package/lib/hooks/useAxes.js +1 -2
  28. package/lib/hooks/useFilterData.js +5 -5
  29. package/lib/hooks/useStackData.js +5 -5
  30. package/lib/hooks/useTooltip.js +11 -11
  31. package/lib/utils/index.js +3 -3
  32. package/package.json +55 -55
  33. package/src/components/Background.tsx +61 -61
  34. package/src/components/Band.tsx +271 -271
  35. package/src/components/Brush.js +159 -159
  36. package/src/components/Chart.js +154 -154
  37. package/src/components/ChartContainer.tsx +71 -71
  38. package/src/components/ConicalGradient.js +258 -258
  39. package/src/components/Control.jsx +241 -241
  40. package/src/components/ExtentData.js +18 -18
  41. package/src/components/Indicator.js +58 -58
  42. package/src/components/Label.js +247 -242
  43. package/src/components/Legend.js +166 -166
  44. package/src/components/Lighter.jsx +173 -173
  45. package/src/components/Line.js +153 -153
  46. package/src/components/LinearGradient.js +29 -29
  47. package/src/components/PieTooltip.jsx +160 -160
  48. package/src/components/StereoBar.tsx +307 -307
  49. package/src/components/Tooltip.js +4 -4
  50. package/src/components/index.js +59 -59
  51. package/src/context/index.js +2 -2
  52. package/src/css/index.module.css +42 -42
  53. package/src/css/piechart.module.css +26 -26
  54. package/src/element/ConicGradient.jsx +55 -55
  55. package/src/element/Line.tsx +33 -33
  56. package/src/element/index.ts +3 -3
  57. package/src/formatter/index.js +1 -1
  58. package/src/formatter/legend.js +112 -112
  59. package/src/hooks/index.js +20 -20
  60. package/src/hooks/useAnimateData.ts +68 -68
  61. package/src/hooks/useAxes.js +1 -2
  62. package/src/hooks/useFilterData.js +78 -78
  63. package/src/hooks/useStackData.js +102 -102
  64. package/src/hooks/useTooltip.ts +104 -104
  65. package/src/index.js +6 -6
  66. package/src/types/index.d.ts +68 -68
  67. package/src/utils/index.js +782 -782
  68. package/tsconfig.json +23 -23
@@ -1,782 +1,782 @@
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
-
278
- const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
279
- marginTop +
280
- 'px ' +
281
- marginRight +
282
- 'px ' +
283
- marginBottom +
284
- 'px ' +
285
- marginLeft +
286
- 'px';
287
- const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
288
- 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
289
- const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
290
- function band() {
291
- var scale = ordinal().unknown(undefined),
292
- domain = scale.domain,
293
- ordinalRange = scale.range,
294
- r0 = 0,
295
- r1 = 1,
296
- step,
297
- bandwidth,
298
- round = false,
299
- paddingInner = 0,
300
- paddingOuter = 0,
301
- // seriesPaddingInner = 0,
302
- // seriesPaddingOuter = 0,
303
- // seriesLength = 0,
304
- align = 0.5;
305
-
306
- delete scale.unknown;
307
-
308
- function rescale() {
309
- var n = domain().length,
310
- reverse = r1 < r0,
311
- start = reverse ? r1 : r0,
312
- stop = reverse ? r0 : r1;
313
- step = (stop - start) / Math.max(1, n - paddingOuter * 2);
314
- if (round) step = Math.floor(step);
315
- start += (stop - start - step * n) * align;
316
- bandwidth = step;
317
- if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
318
- var values = sequence(n).map(function (i) {
319
- return start + step * i + step / 2;
320
- });
321
- return ordinalRange(reverse ? values.reverse() : values);
322
- }
323
-
324
- scale.domain = function (_) {
325
- return arguments.length ? (domain(_), rescale()) : domain();
326
- };
327
-
328
- scale.range = function (_) {
329
- return arguments.length
330
- ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
331
- : [r0, r1];
332
- };
333
-
334
- scale.rangeRound = function (_) {
335
- return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
336
- };
337
-
338
- scale.bandwidth = function () {
339
- return bandwidth;
340
- };
341
-
342
- scale.step = function () {
343
- return step;
344
- };
345
-
346
- scale.seriesBandwidth = function () {
347
- return seriesBandwidth;
348
- };
349
-
350
- scale.seriesStep = function () {
351
- return seriesStep;
352
- };
353
-
354
- scale.round = function (_) {
355
- return arguments.length ? ((round = !!_), rescale()) : round;
356
- };
357
-
358
- scale.padding = function (_) {
359
- return arguments.length
360
- ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
361
- : paddingInner;
362
- };
363
-
364
- scale.paddingInner = function (_) {
365
- return arguments.length
366
- ? ((paddingInner = Math.min(1, _)), rescale())
367
- : paddingInner;
368
- };
369
-
370
- scale.paddingOuter = function (_) {
371
- return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
372
- };
373
-
374
- scale.align = function (_) {
375
- return arguments.length
376
- ? ((align = Math.max(0, Math.min(1, _))), rescale())
377
- : align;
378
- };
379
-
380
- scale.copy = function () {
381
- return band(domain(), [r0, r1])
382
- .round(round)
383
- .paddingInner(paddingInner)
384
- .paddingOuter(paddingOuter)
385
- .align(align);
386
- };
387
-
388
- return initRange.apply(rescale(), arguments);
389
- }
390
-
391
- function initRange(domain, range) {
392
- switch (arguments.length) {
393
- case 0:
394
- break;
395
- case 1:
396
- this.range(domain);
397
- break;
398
- default:
399
- this.range(range).domain(domain);
400
- break;
401
- }
402
- return this;
403
- }
404
-
405
- const getStacks = (series) => {
406
- const tmp = [];
407
- series.forEach(({ type, stack, yOrZ }, name) => {
408
- const current = tmp.find(
409
- ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
410
- _type == type && stack && _stack == stack && yOrZ == _yOrZ
411
- );
412
- if (!current) {
413
- const common = {
414
- type,
415
- stack,
416
- positive: 0,
417
- negative: 0,
418
- yOrZ,
419
- s: [name],
420
- };
421
- if (type === 'band') {
422
- const index = tmp.filter((item) => item.type === 'band').length;
423
- tmp.push({
424
- ...common,
425
- index,
426
- });
427
- } else {
428
- tmp.push({
429
- ...common,
430
- index: 0,
431
- });
432
- }
433
- } else {
434
- current.s.push(name);
435
- }
436
- });
437
- return tmp;
438
- };
439
-
440
- const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
441
- const tmp = {
442
- y: [],
443
- z: [],
444
- };
445
- for (let i = 0, j = data.length; i < j; i++) {
446
- const d = data[i];
447
- if (seriesY.get(d.s)) {
448
- tmp.y.push(d);
449
- continue;
450
- }
451
- if (seriesZ.get(d.s)) {
452
- tmp.z.push(d);
453
- }
454
- }
455
- return tmp;
456
- };
457
-
458
- const seriesYOrZ = (series) => {
459
- const y = new Map();
460
- const z = new Map();
461
- series.forEach((value, key) => {
462
- if (value.yOrZ === 'y') {
463
- y.set(key, value);
464
- } else {
465
- z.set(key, value);
466
- }
467
- });
468
- return { y, z };
469
- };
470
-
471
- const resetStacks = (stacks) => {
472
- stacks.forEach((stack) => {
473
- stack.positive = 0;
474
- stack.negative = 0;
475
- });
476
- };
477
-
478
- const getCurrentStack = (stack, stackMap) =>
479
- stackMap.find(
480
- ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
481
- _type == stack.type &&
482
- _stack == stack.stack &&
483
- _yOrZ == stack.yOrZ &&
484
- _s.includes(stack.name)
485
- );
486
-
487
- const getBandBackground = (pattern, fill) => {
488
- if (!(pattern && pattern.path)) return getColor(fill);
489
- try{
490
- const { backgroundSize = '100% 100%', ..._pattern } = pattern;
491
- return (
492
- 'center top / ' +
493
- backgroundSize +
494
- ' url("data:image/svg+xml,' +
495
- encodeURIComponent(
496
- renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
497
- ) +
498
- '")'
499
- );
500
- }catch(e){}
501
- return "";
502
- };
503
- const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
504
-
505
- const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
506
- const seriesStep = width / (bandLength == 0 ? 1 : bandLength);
507
- const seriesWidth = seriesStep * (1 - paddingInner);
508
- return {
509
- seriesStep,
510
- seriesWidth,
511
- };
512
- };
513
-
514
- const getSeriesInfo = ({
515
- step,
516
- bandLength = 1,
517
- paddingInner = 0,
518
- paddingOuter = 0,
519
- }) => {
520
- if (bandLength == 0)
521
- return {
522
- seriesWidth: step,
523
- seriesStep: step,
524
- seriesStart: 0,
525
- width: step,
526
- };
527
- const _step =
528
- step / (bandLength + paddingOuter * 2 + paddingInner * (bandLength - 1));
529
- return {
530
- seriesWidth: _step,
531
- seriesStep: (1 + paddingInner) * _step,
532
- seriesStart: paddingOuter * _step,
533
- width: step - paddingOuter * 2 * _step,
534
- };
535
- };
536
-
537
- const isValidHttpUrl = (string) => {
538
- let url;
539
-
540
- try {
541
- url = new URL(string);
542
- } catch (_) {
543
- return false;
544
- }
545
-
546
- return url.protocol === 'http:' || url.protocol === 'https:';
547
- };
548
-
549
- const getChildren = (svgStr) => {
550
- const wrapper = document.createElement('div');
551
- wrapper.innerHTML = svgStr;
552
- const { childNodes } = wrapper;
553
- const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
554
-
555
- if (!!svgDom) {
556
- return [...svgDom.childNodes];
557
- }
558
-
559
- return null;
560
- };
561
-
562
- const filterChildren = (children, tagNames) => {
563
- return children.reduce((prev, node) => {
564
- let { nodeName } = node;
565
-
566
- if (tagNames.indexOf(nodeName) > -1) {
567
- if (nodeName === 'g') {
568
- return filterChildren([...node.childNodes], tagNames);
569
- } else {
570
- prev.push(node);
571
- }
572
- }
573
-
574
- return prev;
575
- }, []);
576
- };
577
-
578
- const getDomPath = (node) => {
579
- switch (node.nodeName) {
580
- case 'circle':
581
- return toPath({
582
- type: 'circle',
583
- cx: +node.getAttribute('cx') || 0,
584
- cy: +node.getAttribute('cy') || 0,
585
- r: +node.getAttribute('r') || 0,
586
- });
587
-
588
- case 'ellipse':
589
- return toPath({
590
- type: 'ellipse',
591
- cx: +node.getAttribute('cx') || 0,
592
- cy: +node.getAttribute('cy') || 0,
593
- rx: +node.getAttribute('rx') || 0,
594
- ry: +node.getAttribute('ry') || 0,
595
- });
596
-
597
- case 'line':
598
- return toPath({
599
- type: 'line',
600
- x1: +node.getAttribute('x1') || 0,
601
- x2: +node.getAttribute('x2') || 0,
602
- y1: +node.getAttribute('y1') || 0,
603
- y2: +node.getAttribute('y2') || 0,
604
- });
605
-
606
- case 'path':
607
- return toPath({
608
- type: 'path',
609
- d: node.getAttribute('d') || '',
610
- });
611
-
612
- case 'polygon':
613
- return toPath({
614
- type: 'polyline',
615
- points: node.getAttribute('points') || '',
616
- });
617
-
618
- case 'polyline':
619
- return toPath({
620
- type: 'polyline',
621
- points: node.getAttribute('points') || '',
622
- });
623
-
624
- case 'rect':
625
- return toPath({
626
- type: 'rect',
627
- height: +node.getAttribute('height') || 0,
628
- width: +node.getAttribute('width') || 0,
629
- x: +node.getAttribute('x') || 0,
630
- y: +node.getAttribute('y') || 0,
631
- rx: +node.getAttribute('rx') || 0,
632
- ry: +node.getAttribute('ry') || 0,
633
- });
634
- }
635
- };
636
-
637
- const sortPie = (data, order) => {
638
- const _data = data.map((item) => ({ ...item }));
639
- switch (order) {
640
- case '':
641
- _data.sort(({ index: a }, { index: b }) => ascending(a, b));
642
- break;
643
- case 'desc':
644
- _data.sort(({ value: a }, { value: b }) => descending(a, b));
645
- break;
646
- case 'asc':
647
- _data.sort(({ value: a }, { value: b }) => ascending(a, b));
648
- break;
649
- }
650
- return _data;
651
- };
652
-
653
- // const getDataWithPercent = (data = [], precision = 0, type) => {
654
- // const digits = Math.pow(10, precision);
655
- // const targetSeats = digits * 100;
656
-
657
- // const total = sum(data, (d) => d.value);
658
-
659
- // const votesPerQuota = data.map((d, index) => ({
660
- // ...d,
661
- // vote: Math.round((d.value / total) * digits * 100),
662
- // index,
663
- // }));
664
- // const currentSum = sum(votesPerQuota, (d) => d.vote);
665
- // const remainder = targetSeats - currentSum;
666
- // console.log(type+":",votesPerQuota, toFixed);
667
- // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
668
-
669
- // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
670
- // ...data,
671
- // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
672
- // }));
673
-
674
- // return tmp;
675
- // };
676
-
677
- const getDataWithPercent = (data = [], precision = 0) => {
678
- let objData = [];
679
- function getPercentWithPrecision(valueList, idx, precision) {
680
- if (!valueList[idx]) {
681
- return 0;
682
- }
683
- const sum = valueList.reduce( function (acc, val) {
684
- return acc + val.value;
685
- }, 0);
686
- if (sum === 0) {
687
- return 0;
688
- }
689
- const digits = Math.pow(10, precision);
690
- const votesPerQuota = valueList.map(function (val) {
691
- return val.value / sum * digits * 100;
692
- });
693
- const targetSeats = digits * 100;
694
- const seats = votesPerQuota.map(function (votes) {
695
- return Math.floor(votes);
696
- });
697
- let currentSum = seats.reduce( function (acc, val) {
698
- return acc + val;
699
- }, 0);
700
- const remainder = votesPerQuota.map(function (votes, idx) {
701
- return votes - seats[idx];
702
- });
703
- while (currentSum < targetSeats) {
704
- let max = Number.NEGATIVE_INFINITY;
705
- let maxId = null;
706
- for (let i = 0, len = remainder.length; i < len; ++i) {
707
- if (remainder[i] > max) {
708
- max = remainder[i];
709
- maxId = i;
710
- }
711
- }
712
- ++seats[maxId];
713
- remainder[maxId] = 0;
714
- ++currentSum;
715
- }
716
- return seats[idx] / digits;
717
- }
718
- data.forEach((d, i) => {
719
- objData.push({
720
- ...d,
721
- percent: getPercentWithPrecision(data, i, precision)
722
- })
723
- });
724
- return objData
725
- };
726
-
727
- const excludeTypes = ['array', 'object', 'group', 'modal', 'colors'];
728
- const reduceConfig = (config = []) => {
729
- if (!Array.isArray(config)) {
730
- return config;
731
- }
732
- let output = {};
733
- for (let i = 0, len = config.length; i < len; i++) {
734
- let type = config[i]._type;
735
-
736
- output[config[i]._name] =
737
- type && !excludeTypes.includes(type)
738
- ? config[i]._value
739
- : reduceConfig(config[i]._value);
740
- }
741
- return output;
742
- };
743
-
744
- //限制value的值在min和max之间
745
- const mathR=(value, range)=>{
746
- const [min,max] = range;
747
- return Math.max(min,Math.min(value,max));
748
- }
749
-
750
- export {
751
- dateFormat,
752
- getBreakWord,
753
- getTicksOfAxis,
754
- getTickCoord,
755
- getGridCoord,
756
- identity,
757
- getMousePos,
758
- getFontStyle,
759
- getMargin,
760
- getTranslate3d,
761
- getTranslate2d,
762
- band,
763
- getIcon,
764
- getColorList,
765
- getStacks,
766
- dataYOrZ,
767
- seriesYOrZ,
768
- resetStacks,
769
- getCurrentStack,
770
- getBandBackground,
771
- getBandwidth,
772
- getBandSeriesStepAndWidth,
773
- isValidHttpUrl,
774
- getChildren,
775
- filterChildren,
776
- getDomPath,
777
- sortPie,
778
- getDataWithPercent,
779
- reduceConfig,
780
- getSeriesInfo,
781
- mathR
782
- };
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
+
278
+ const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
279
+ marginTop +
280
+ 'px ' +
281
+ marginRight +
282
+ 'px ' +
283
+ marginBottom +
284
+ 'px ' +
285
+ marginLeft +
286
+ 'px';
287
+ const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
288
+ 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
289
+ const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
290
+ function band() {
291
+ var scale = ordinal().unknown(undefined),
292
+ domain = scale.domain,
293
+ ordinalRange = scale.range,
294
+ r0 = 0,
295
+ r1 = 1,
296
+ step,
297
+ bandwidth,
298
+ round = false,
299
+ paddingInner = 0,
300
+ paddingOuter = 0,
301
+ // seriesPaddingInner = 0,
302
+ // seriesPaddingOuter = 0,
303
+ // seriesLength = 0,
304
+ align = 0.5;
305
+
306
+ delete scale.unknown;
307
+
308
+ function rescale() {
309
+ var n = domain().length,
310
+ reverse = r1 < r0,
311
+ start = reverse ? r1 : r0,
312
+ stop = reverse ? r0 : r1;
313
+ step = (stop - start) / Math.max(1, n - paddingOuter * 2);
314
+ if (round) step = Math.floor(step);
315
+ start += (stop - start - step * n) * align;
316
+ bandwidth = step;
317
+ if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
318
+ var values = sequence(n).map(function (i) {
319
+ return start + step * i + step / 2;
320
+ });
321
+ return ordinalRange(reverse ? values.reverse() : values);
322
+ }
323
+
324
+ scale.domain = function (_) {
325
+ return arguments.length ? (domain(_), rescale()) : domain();
326
+ };
327
+
328
+ scale.range = function (_) {
329
+ return arguments.length
330
+ ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
331
+ : [r0, r1];
332
+ };
333
+
334
+ scale.rangeRound = function (_) {
335
+ return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
336
+ };
337
+
338
+ scale.bandwidth = function () {
339
+ return bandwidth;
340
+ };
341
+
342
+ scale.step = function () {
343
+ return step;
344
+ };
345
+
346
+ scale.seriesBandwidth = function () {
347
+ return seriesBandwidth;
348
+ };
349
+
350
+ scale.seriesStep = function () {
351
+ return seriesStep;
352
+ };
353
+
354
+ scale.round = function (_) {
355
+ return arguments.length ? ((round = !!_), rescale()) : round;
356
+ };
357
+
358
+ scale.padding = function (_) {
359
+ return arguments.length
360
+ ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
361
+ : paddingInner;
362
+ };
363
+
364
+ scale.paddingInner = function (_) {
365
+ return arguments.length
366
+ ? ((paddingInner = Math.min(1, _)), rescale())
367
+ : paddingInner;
368
+ };
369
+
370
+ scale.paddingOuter = function (_) {
371
+ return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
372
+ };
373
+
374
+ scale.align = function (_) {
375
+ return arguments.length
376
+ ? ((align = Math.max(0, Math.min(1, _))), rescale())
377
+ : align;
378
+ };
379
+
380
+ scale.copy = function () {
381
+ return band(domain(), [r0, r1])
382
+ .round(round)
383
+ .paddingInner(paddingInner)
384
+ .paddingOuter(paddingOuter)
385
+ .align(align);
386
+ };
387
+
388
+ return initRange.apply(rescale(), arguments);
389
+ }
390
+
391
+ function initRange(domain, range) {
392
+ switch (arguments.length) {
393
+ case 0:
394
+ break;
395
+ case 1:
396
+ this.range(domain);
397
+ break;
398
+ default:
399
+ this.range(range).domain(domain);
400
+ break;
401
+ }
402
+ return this;
403
+ }
404
+
405
+ const getStacks = (series) => {
406
+ const tmp = [];
407
+ series.forEach(({ type, stack, yOrZ }, name) => {
408
+ const current = tmp.find(
409
+ ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
410
+ _type == type && stack && _stack == stack && yOrZ == _yOrZ
411
+ );
412
+ if (!current) {
413
+ const common = {
414
+ type,
415
+ stack,
416
+ positive: 0,
417
+ negative: 0,
418
+ yOrZ,
419
+ s: [name],
420
+ };
421
+ if (type === 'band') {
422
+ const index = tmp.filter((item) => item.type === 'band').length;
423
+ tmp.push({
424
+ ...common,
425
+ index,
426
+ });
427
+ } else {
428
+ tmp.push({
429
+ ...common,
430
+ index: 0,
431
+ });
432
+ }
433
+ } else {
434
+ current.s.push(name);
435
+ }
436
+ });
437
+ return tmp;
438
+ };
439
+
440
+ const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
441
+ const tmp = {
442
+ y: [],
443
+ z: [],
444
+ };
445
+ for (let i = 0, j = data.length; i < j; i++) {
446
+ const d = data[i];
447
+ if (seriesY.get(d.s)) {
448
+ tmp.y.push(d);
449
+ continue;
450
+ }
451
+ if (seriesZ.get(d.s)) {
452
+ tmp.z.push(d);
453
+ }
454
+ }
455
+ return tmp;
456
+ };
457
+
458
+ const seriesYOrZ = (series) => {
459
+ const y = new Map();
460
+ const z = new Map();
461
+ series.forEach((value, key) => {
462
+ if (value.yOrZ === 'y') {
463
+ y.set(key, value);
464
+ } else {
465
+ z.set(key, value);
466
+ }
467
+ });
468
+ return { y, z };
469
+ };
470
+
471
+ const resetStacks = (stacks) => {
472
+ stacks.forEach((stack) => {
473
+ stack.positive = 0;
474
+ stack.negative = 0;
475
+ });
476
+ };
477
+
478
+ const getCurrentStack = (stack, stackMap) =>
479
+ stackMap.find(
480
+ ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
481
+ _type == stack.type &&
482
+ _stack == stack.stack &&
483
+ _yOrZ == stack.yOrZ &&
484
+ _s.includes(stack.name)
485
+ );
486
+
487
+ const getBandBackground = (pattern, fill) => {
488
+ if (!(pattern && pattern.path)) return getColor(fill);
489
+ try{
490
+ const { backgroundSize = '100% 100%', ..._pattern } = pattern;
491
+ return (
492
+ 'center top / ' +
493
+ backgroundSize +
494
+ ' url("data:image/svg+xml,' +
495
+ encodeURIComponent(
496
+ renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
497
+ ) +
498
+ '")'
499
+ );
500
+ }catch(e){}
501
+ return "";
502
+ };
503
+ const getBandwidth = (step, paddingOuter) => step * (1 - paddingOuter);
504
+
505
+ const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
506
+ const seriesStep = width / (bandLength == 0 ? 1 : bandLength);
507
+ const seriesWidth = seriesStep * (1 - paddingInner);
508
+ return {
509
+ seriesStep,
510
+ seriesWidth,
511
+ };
512
+ };
513
+
514
+ const getSeriesInfo = ({
515
+ step,
516
+ bandLength = 1,
517
+ paddingInner = 0,
518
+ paddingOuter = 0,
519
+ }) => {
520
+ if (bandLength == 0)
521
+ return {
522
+ seriesWidth: step,
523
+ seriesStep: step,
524
+ seriesStart: 0,
525
+ width: step,
526
+ };
527
+ const _step =
528
+ step / (bandLength + paddingOuter * 2 + paddingInner * (bandLength - 1));
529
+ return {
530
+ seriesWidth: _step,
531
+ seriesStep: (1 + paddingInner) * _step,
532
+ seriesStart: paddingOuter * _step,
533
+ width: step - paddingOuter * 2 * _step,
534
+ };
535
+ };
536
+
537
+ const isValidHttpUrl = (string) => {
538
+ let url;
539
+
540
+ try {
541
+ url = new URL(string);
542
+ } catch (_) {
543
+ return false;
544
+ }
545
+
546
+ return url.protocol === 'http:' || url.protocol === 'https:';
547
+ };
548
+
549
+ const getChildren = (svgStr) => {
550
+ const wrapper = document.createElement('div');
551
+ wrapper.innerHTML = svgStr;
552
+ const { childNodes } = wrapper;
553
+ const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
554
+
555
+ if (!!svgDom) {
556
+ return [...svgDom.childNodes];
557
+ }
558
+
559
+ return null;
560
+ };
561
+
562
+ const filterChildren = (children, tagNames) => {
563
+ return children.reduce((prev, node) => {
564
+ let { nodeName } = node;
565
+
566
+ if (tagNames.indexOf(nodeName) > -1) {
567
+ if (nodeName === 'g') {
568
+ return filterChildren([...node.childNodes], tagNames);
569
+ } else {
570
+ prev.push(node);
571
+ }
572
+ }
573
+
574
+ return prev;
575
+ }, []);
576
+ };
577
+
578
+ const getDomPath = (node) => {
579
+ switch (node.nodeName) {
580
+ case 'circle':
581
+ return toPath({
582
+ type: 'circle',
583
+ cx: +node.getAttribute('cx') || 0,
584
+ cy: +node.getAttribute('cy') || 0,
585
+ r: +node.getAttribute('r') || 0,
586
+ });
587
+
588
+ case 'ellipse':
589
+ return toPath({
590
+ type: 'ellipse',
591
+ cx: +node.getAttribute('cx') || 0,
592
+ cy: +node.getAttribute('cy') || 0,
593
+ rx: +node.getAttribute('rx') || 0,
594
+ ry: +node.getAttribute('ry') || 0,
595
+ });
596
+
597
+ case 'line':
598
+ return toPath({
599
+ type: 'line',
600
+ x1: +node.getAttribute('x1') || 0,
601
+ x2: +node.getAttribute('x2') || 0,
602
+ y1: +node.getAttribute('y1') || 0,
603
+ y2: +node.getAttribute('y2') || 0,
604
+ });
605
+
606
+ case 'path':
607
+ return toPath({
608
+ type: 'path',
609
+ d: node.getAttribute('d') || '',
610
+ });
611
+
612
+ case 'polygon':
613
+ return toPath({
614
+ type: 'polyline',
615
+ points: node.getAttribute('points') || '',
616
+ });
617
+
618
+ case 'polyline':
619
+ return toPath({
620
+ type: 'polyline',
621
+ points: node.getAttribute('points') || '',
622
+ });
623
+
624
+ case 'rect':
625
+ return toPath({
626
+ type: 'rect',
627
+ height: +node.getAttribute('height') || 0,
628
+ width: +node.getAttribute('width') || 0,
629
+ x: +node.getAttribute('x') || 0,
630
+ y: +node.getAttribute('y') || 0,
631
+ rx: +node.getAttribute('rx') || 0,
632
+ ry: +node.getAttribute('ry') || 0,
633
+ });
634
+ }
635
+ };
636
+
637
+ const sortPie = (data, order) => {
638
+ const _data = data.map((item) => ({ ...item }));
639
+ switch (order) {
640
+ case '':
641
+ _data.sort(({ index: a }, { index: b }) => ascending(a, b));
642
+ break;
643
+ case 'desc':
644
+ _data.sort(({ value: a }, { value: b }) => descending(a, b));
645
+ break;
646
+ case 'asc':
647
+ _data.sort(({ value: a }, { value: b }) => ascending(a, b));
648
+ break;
649
+ }
650
+ return _data;
651
+ };
652
+
653
+ // const getDataWithPercent = (data = [], precision = 0, type) => {
654
+ // const digits = Math.pow(10, precision);
655
+ // const targetSeats = digits * 100;
656
+
657
+ // const total = sum(data, (d) => d.value);
658
+
659
+ // const votesPerQuota = data.map((d, index) => ({
660
+ // ...d,
661
+ // vote: Math.round((d.value / total) * digits * 100),
662
+ // index,
663
+ // }));
664
+ // const currentSum = sum(votesPerQuota, (d) => d.vote);
665
+ // const remainder = targetSeats - currentSum;
666
+ // console.log(type+":",votesPerQuota, toFixed);
667
+ // votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
668
+
669
+ // const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
670
+ // ...data,
671
+ // percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
672
+ // }));
673
+
674
+ // return tmp;
675
+ // };
676
+
677
+ const getDataWithPercent = (data = [], precision = 0) => {
678
+ let objData = [];
679
+ function getPercentWithPrecision(valueList, idx, precision) {
680
+ if (!valueList[idx]) {
681
+ return 0;
682
+ }
683
+ const sum = valueList.reduce( function (acc, val) {
684
+ return acc + val.value;
685
+ }, 0);
686
+ if (sum === 0) {
687
+ return 0;
688
+ }
689
+ const digits = Math.pow(10, precision);
690
+ const votesPerQuota = valueList.map(function (val) {
691
+ return val.value / sum * digits * 100;
692
+ });
693
+ const targetSeats = digits * 100;
694
+ const seats = votesPerQuota.map(function (votes) {
695
+ return Math.floor(votes);
696
+ });
697
+ let currentSum = seats.reduce( function (acc, val) {
698
+ return acc + val;
699
+ }, 0);
700
+ const remainder = votesPerQuota.map(function (votes, idx) {
701
+ return votes - seats[idx];
702
+ });
703
+ while (currentSum < targetSeats) {
704
+ let max = Number.NEGATIVE_INFINITY;
705
+ let maxId = null;
706
+ for (let i = 0, len = remainder.length; i < len; ++i) {
707
+ if (remainder[i] > max) {
708
+ max = remainder[i];
709
+ maxId = i;
710
+ }
711
+ }
712
+ ++seats[maxId];
713
+ remainder[maxId] = 0;
714
+ ++currentSum;
715
+ }
716
+ return seats[idx] / digits;
717
+ }
718
+ data.forEach((d, i) => {
719
+ objData.push({
720
+ ...d,
721
+ percent: getPercentWithPrecision(data, i, precision)
722
+ })
723
+ });
724
+ return objData
725
+ };
726
+
727
+ const excludeTypes = ['array', 'object', 'group', 'modal', 'colors'];
728
+ const reduceConfig = (config = []) => {
729
+ if (!Array.isArray(config)) {
730
+ return config;
731
+ }
732
+ let output = {};
733
+ for (let i = 0, len = config.length; i < len; i++) {
734
+ let type = config[i]._type;
735
+
736
+ output[config[i]._name] =
737
+ type && !excludeTypes.includes(type)
738
+ ? config[i]._value
739
+ : reduceConfig(config[i]._value);
740
+ }
741
+ return output;
742
+ };
743
+
744
+ //限制value的值在min和max之间
745
+ const mathR=(value, range)=>{
746
+ const [min,max] = range;
747
+ return Math.max(min,Math.min(value,max));
748
+ }
749
+
750
+ export {
751
+ dateFormat,
752
+ getBreakWord,
753
+ getTicksOfAxis,
754
+ getTickCoord,
755
+ getGridCoord,
756
+ identity,
757
+ getMousePos,
758
+ getFontStyle,
759
+ getMargin,
760
+ getTranslate3d,
761
+ getTranslate2d,
762
+ band,
763
+ getIcon,
764
+ getColorList,
765
+ getStacks,
766
+ dataYOrZ,
767
+ seriesYOrZ,
768
+ resetStacks,
769
+ getCurrentStack,
770
+ getBandBackground,
771
+ getBandwidth,
772
+ getBandSeriesStepAndWidth,
773
+ isValidHttpUrl,
774
+ getChildren,
775
+ filterChildren,
776
+ getDomPath,
777
+ sortPie,
778
+ getDataWithPercent,
779
+ reduceConfig,
780
+ getSeriesInfo,
781
+ mathR
782
+ };