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