@easyv/charts 1.4.29 → 1.4.30

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