@easyv/charts 1.0.59 → 1.0.63

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 (66) hide show
  1. package/.babelrc +8 -8
  2. package/lib/components/AnimateData.js +2 -2
  3. package/lib/components/Axis.js +12 -12
  4. package/lib/components/Background.js +2 -2
  5. package/lib/components/Carousel.js +2 -2
  6. package/lib/components/Chart.js +2 -2
  7. package/lib/components/ConicalGradient.js +21 -21
  8. package/lib/components/Indicator.js +2 -2
  9. package/lib/components/Label.js +7 -2
  10. package/lib/components/LinearGradient.js +2 -2
  11. package/lib/components/PieChart.js +3 -2
  12. package/lib/components/Tooltip.js +23 -4
  13. package/lib/css/index.module.css +41 -41
  14. package/lib/css/piechart.module.css +26 -26
  15. package/lib/formatter/legend.js +2 -2
  16. package/lib/hooks/useAnimateData.js +5 -5
  17. package/lib/hooks/useAxes.js +5 -5
  18. package/lib/hooks/useCarouselAxisX.js +5 -5
  19. package/lib/hooks/useExtentData.js +6 -6
  20. package/lib/hooks/useFilterData.js +5 -5
  21. package/lib/hooks/useStackData.js +5 -5
  22. package/lib/hooks/useTooltip.js +10 -10
  23. package/package.json +34 -34
  24. package/src/components/AnimateData.tsx +24 -24
  25. package/src/components/Axis.tsx +333 -333
  26. package/src/components/Background.tsx +45 -45
  27. package/src/components/Band.tsx +160 -160
  28. package/src/components/Brush.js +159 -159
  29. package/src/components/Carousel.tsx +144 -144
  30. package/src/components/Chart.js +99 -99
  31. package/src/components/ChartContainer.tsx +63 -63
  32. package/src/components/ConicalGradient.js +258 -258
  33. package/src/components/ExtentData.js +17 -17
  34. package/src/components/FilterData.js +23 -23
  35. package/src/components/Indicator.js +13 -13
  36. package/src/components/Label.js +196 -193
  37. package/src/components/Legend.js +158 -158
  38. package/src/components/Lighter.jsx +162 -162
  39. package/src/components/Line.js +126 -126
  40. package/src/components/LinearGradient.js +29 -29
  41. package/src/components/Mapping.js +71 -71
  42. package/src/components/PieChart.js +1093 -1092
  43. package/src/components/StackData.js +20 -20
  44. package/src/components/StereoBar.tsx +298 -298
  45. package/src/components/Tooltip.js +133 -117
  46. package/src/components/index.js +49 -49
  47. package/src/context/index.js +2 -2
  48. package/src/css/index.module.css +41 -41
  49. package/src/css/piechart.module.css +26 -26
  50. package/src/element/ConicGradient.jsx +56 -0
  51. package/src/element/Line.tsx +33 -33
  52. package/src/element/index.ts +3 -3
  53. package/src/formatter/index.js +1 -1
  54. package/src/formatter/legend.js +87 -87
  55. package/src/hooks/index.js +17 -17
  56. package/src/hooks/useAnimateData.ts +62 -62
  57. package/src/hooks/useAxes.js +143 -143
  58. package/src/hooks/useCarouselAxisX.js +163 -163
  59. package/src/hooks/useExtentData.js +88 -88
  60. package/src/hooks/useFilterData.js +72 -72
  61. package/src/hooks/useStackData.js +100 -100
  62. package/src/hooks/useTooltip.ts +96 -96
  63. package/src/index.js +6 -6
  64. package/src/types/index.d.ts +59 -59
  65. package/src/utils/index.js +640 -640
  66. package/tsconfig.json +22 -22
@@ -1,640 +1,640 @@
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 + ', 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) => {
84
- switch (type) {
85
- case 'area':
86
- case 'line':
87
- return icon
88
- ? {
89
- ...defaultLineIcon,
90
- ...icon,
91
- }
92
- : defaultLineIcon;
93
- default:
94
- return icon
95
- ? {
96
- ...defaultIcon,
97
- ...icon,
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
- const getTickCoord = ({
145
- orientation,
146
- coordinate,
147
- tickSize = 6,
148
- x = 0,
149
- y = 0,
150
- }) => {
151
- let x1, x2, y1, y2;
152
- switch (orientation) {
153
- case 'top':
154
- x1 = x2 = coordinate;
155
- y2 = y;
156
- y1 = y2 - tickSize;
157
- break;
158
- case 'left':
159
- y1 = y2 = coordinate;
160
- x2 = x;
161
- x1 = x2 - tickSize;
162
- break;
163
- case 'right':
164
- y1 = y2 = coordinate;
165
- x2 = x;
166
- x1 = x2 + tickSize;
167
- break;
168
- default:
169
- x1 = x2 = coordinate;
170
- y2 = y;
171
- y1 = y2 + tickSize;
172
- break;
173
- }
174
- return { x1, x2, y1, y2 };
175
- };
176
- const getGridCoord = ({ orientation, coordinate, end }) => {
177
- let x1, x2, y1, y2;
178
- switch (orientation) {
179
- case 'top':
180
- x1 = x2 = coordinate;
181
- y1 = 0;
182
- y2 = end;
183
- break;
184
- case 'bottom':
185
- x1 = x2 = coordinate;
186
- y1 = 0;
187
- y2 = end * -1;
188
- break;
189
- case 'left':
190
- y1 = y2 = coordinate;
191
- x1 = 0;
192
- x2 = end;
193
- break;
194
- case 'right':
195
- y1 = y2 = coordinate;
196
- x1 = 0;
197
- x2 = end * -1;
198
- break;
199
- }
200
- return { x1, x2, y1, y2 };
201
- };
202
-
203
- const identity = (d) => d;
204
-
205
- //获取鼠标指针坐标
206
- const getMousePos = (evt, dom) => {
207
- var rect = dom.getBoundingClientRect();
208
- return {
209
- x: evt.clientX - rect.left,
210
- y: evt.clientY - rect.top,
211
- w: rect.width,
212
- h: rect.height,
213
- };
214
- };
215
-
216
- const getFontStyle = (
217
- { color, bold, italic, fontSize, fontFamily, letterSpacing },
218
- type
219
- ) => {
220
- if (type == 'svg') {
221
- return {
222
- fontSize,
223
- fontFamily,
224
- letterSpacing,
225
- fill: color,
226
- fontWeight: bold ? 'bold' : 'normal',
227
- fontStyle: italic ? 'italic' : 'normal',
228
- };
229
- }
230
- return {
231
- fontSize,
232
- fontFamily,
233
- letterSpacing,
234
- color,
235
- fontWeight: bold ? 'bold' : 'normal',
236
- fontStyle: italic ? 'italic' : 'normal',
237
- };
238
- };
239
-
240
- const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
241
- marginTop +
242
- 'px ' +
243
- marginRight +
244
- 'px ' +
245
- marginBottom +
246
- 'px ' +
247
- marginLeft +
248
- 'px';
249
- const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
250
- 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
251
- const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
252
- function band() {
253
- var scale = ordinal().unknown(undefined),
254
- domain = scale.domain,
255
- ordinalRange = scale.range,
256
- r0 = 0,
257
- r1 = 1,
258
- step,
259
- bandwidth,
260
- round = false,
261
- paddingInner = 0,
262
- paddingOuter = 0,
263
- // seriesPaddingInner = 0,
264
- // seriesPaddingOuter = 0,
265
- // seriesLength = 0,
266
- align = 0.5;
267
-
268
- delete scale.unknown;
269
-
270
- function rescale() {
271
- var n = domain().length,
272
- reverse = r1 < r0,
273
- start = reverse ? r1 : r0,
274
- stop = reverse ? r0 : r1;
275
- step = (stop - start) / Math.max(1, n - paddingOuter * 2);
276
- if (round) step = Math.floor(step);
277
- start += (stop - start - step * n) * align;
278
- bandwidth = step;
279
- if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
280
- var values = sequence(n).map(function (i) {
281
- return start + step * i + step / 2;
282
- });
283
- return ordinalRange(reverse ? values.reverse() : values);
284
- }
285
-
286
- scale.domain = function (_) {
287
- return arguments.length ? (domain(_), rescale()) : domain();
288
- };
289
-
290
- scale.range = function (_) {
291
- return arguments.length
292
- ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
293
- : [r0, r1];
294
- };
295
-
296
- scale.rangeRound = function (_) {
297
- return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
298
- };
299
-
300
- scale.bandwidth = function () {
301
- return bandwidth;
302
- };
303
-
304
- scale.step = function () {
305
- return step;
306
- };
307
-
308
- scale.seriesBandwidth = function () {
309
- return seriesBandwidth;
310
- };
311
-
312
- scale.seriesStep = function () {
313
- return seriesStep;
314
- };
315
-
316
- scale.round = function (_) {
317
- return arguments.length ? ((round = !!_), rescale()) : round;
318
- };
319
-
320
- scale.padding = function (_) {
321
- return arguments.length
322
- ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
323
- : paddingInner;
324
- };
325
-
326
- scale.paddingInner = function (_) {
327
- return arguments.length
328
- ? ((paddingInner = Math.min(1, _)), rescale())
329
- : paddingInner;
330
- };
331
-
332
- scale.paddingOuter = function (_) {
333
- return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
334
- };
335
-
336
- scale.align = function (_) {
337
- return arguments.length
338
- ? ((align = Math.max(0, Math.min(1, _))), rescale())
339
- : align;
340
- };
341
-
342
- scale.copy = function () {
343
- return band(domain(), [r0, r1])
344
- .round(round)
345
- .paddingInner(paddingInner)
346
- .paddingOuter(paddingOuter)
347
- .align(align);
348
- };
349
-
350
- return initRange.apply(rescale(), arguments);
351
- }
352
-
353
- function initRange(domain, range) {
354
- switch (arguments.length) {
355
- case 0:
356
- break;
357
- case 1:
358
- this.range(domain);
359
- break;
360
- default:
361
- this.range(range).domain(domain);
362
- break;
363
- }
364
- return this;
365
- }
366
-
367
- const getStacks = (series) => {
368
- const tmp = [];
369
- series.forEach(({ type, stack, yOrZ }, name) => {
370
- const current = tmp.find(
371
- ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
372
- _type == type && stack && _stack == stack && yOrZ == _yOrZ
373
- );
374
- if (!current) {
375
- const common = {
376
- type,
377
- stack,
378
- positive: 0,
379
- negative: 0,
380
- yOrZ,
381
- s: [name],
382
- };
383
- if (type === 'band') {
384
- const index = tmp.filter((item) => item.type === 'band').length;
385
- tmp.push({
386
- ...common,
387
- index,
388
- });
389
- } else {
390
- tmp.push({
391
- ...common,
392
- index: 0,
393
- });
394
- }
395
- } else {
396
- current.s.push(name);
397
- }
398
- });
399
- return tmp;
400
- };
401
-
402
- const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
403
- const tmp = {
404
- y: [],
405
- z: [],
406
- };
407
- for (let i = 0, j = data.length; i < j; i++) {
408
- const d = data[i];
409
- if (seriesY.get(d.s)) {
410
- tmp.y.push(d);
411
- continue;
412
- }
413
- if (seriesZ.get(d.s)) {
414
- tmp.z.push(d);
415
- }
416
- }
417
- return tmp;
418
- };
419
-
420
- const seriesYOrZ = (series) => {
421
- const y = new Map();
422
- const z = new Map();
423
- series.forEach((value, key) => {
424
- if (value.yOrZ === 'y') {
425
- y.set(key, value);
426
- } else {
427
- z.set(key, value);
428
- }
429
- });
430
- return { y, z };
431
- };
432
-
433
- const resetStacks = (stacks) => {
434
- stacks.forEach((stack) => {
435
- stack.positive = 0;
436
- stack.negative = 0;
437
- });
438
- };
439
-
440
- const getCurrentStack = (stack, stackMap) =>
441
- stackMap.find(
442
- ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
443
- _type == stack.type &&
444
- _stack == stack.stack &&
445
- _yOrZ == stack.yOrZ &&
446
- _s.includes(stack.name)
447
- );
448
-
449
- const getBandBackground = (pattern, fill) => {
450
- if (!(pattern && pattern.path)) return getColor(fill);
451
- const { backgroundSize = '100% 100%', ..._pattern } = pattern;
452
- return (
453
- 'center top / ' +
454
- backgroundSize +
455
- ' url("data:image/svg+xml,' +
456
- encodeURIComponent(
457
- renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
458
- ) +
459
- '")'
460
- );
461
- };
462
- const getBandwidth = (step, paddingOuter) =>
463
- step / Math.max(1, 1 + paddingOuter);
464
-
465
- const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
466
- const seriesStep = width / Math.max(1, bandLength - paddingInner);
467
- const seriesWidth = seriesStep * (1 - paddingInner);
468
- return {
469
- seriesStep,
470
- seriesWidth,
471
- };
472
- };
473
- const isValidHttpUrl = (string) => {
474
- let url;
475
-
476
- try {
477
- url = new URL(string);
478
- } catch (_) {
479
- return false;
480
- }
481
-
482
- return url.protocol === 'http:' || url.protocol === 'https:';
483
- };
484
-
485
- const getChildren = (svgStr) => {
486
- const wrapper = document.createElement('div');
487
- wrapper.innerHTML = svgStr;
488
- const { childNodes } = wrapper;
489
- const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
490
-
491
- if (!!svgDom) {
492
- return [...svgDom.childNodes];
493
- }
494
-
495
- return null;
496
- };
497
-
498
- const filterChildren = (children, tagNames) => {
499
- return children.reduce((prev, node) => {
500
- let { nodeName } = node;
501
-
502
- if (tagNames.indexOf(nodeName) > -1) {
503
- if (nodeName === 'g') {
504
- return filterChildren([...node.childNodes], tagNames);
505
- } else {
506
- prev.push(node);
507
- }
508
- }
509
-
510
- return prev;
511
- }, []);
512
- };
513
-
514
- const getDomPath = (node) => {
515
- switch (node.nodeName) {
516
- case 'circle':
517
- return toPath({
518
- type: 'circle',
519
- cx: +node.getAttribute('cx') || 0,
520
- cy: +node.getAttribute('cy') || 0,
521
- r: +node.getAttribute('r') || 0,
522
- });
523
-
524
- case 'ellipse':
525
- return toPath({
526
- type: 'ellipse',
527
- cx: +node.getAttribute('cx') || 0,
528
- cy: +node.getAttribute('cy') || 0,
529
- rx: +node.getAttribute('rx') || 0,
530
- ry: +node.getAttribute('ry') || 0,
531
- });
532
-
533
- case 'line':
534
- return toPath({
535
- type: 'line',
536
- x1: +node.getAttribute('x1') || 0,
537
- x2: +node.getAttribute('x2') || 0,
538
- y1: +node.getAttribute('y1') || 0,
539
- y2: +node.getAttribute('y2') || 0,
540
- });
541
-
542
- case 'path':
543
- return toPath({
544
- type: 'path',
545
- d: node.getAttribute('d') || '',
546
- });
547
-
548
- case 'polygon':
549
- return toPath({
550
- type: 'polyline',
551
- points: node.getAttribute('points') || '',
552
- });
553
-
554
- case 'polyline':
555
- return toPath({
556
- type: 'polyline',
557
- points: node.getAttribute('points') || '',
558
- });
559
-
560
- case 'rect':
561
- return toPath({
562
- type: 'rect',
563
- height: +node.getAttribute('height') || 0,
564
- width: +node.getAttribute('width') || 0,
565
- x: +node.getAttribute('x') || 0,
566
- y: +node.getAttribute('y') || 0,
567
- rx: +node.getAttribute('rx') || 0,
568
- ry: +node.getAttribute('ry') || 0,
569
- });
570
- }
571
- };
572
-
573
- const sortPie = (data, order) => {
574
- const _data = data.map((item) => ({ ...item }));
575
- switch (order) {
576
- case '':
577
- _data.sort(({ index: a }, { index: b }) => ascending(a, b));
578
- break;
579
- case 'desc':
580
- _data.sort(({ value: a }, { value: b }) => descending(a, b));
581
- break;
582
- case 'asc':
583
- _data.sort(({ value: a }, { value: b }) => ascending(a, b));
584
- break;
585
- }
586
- return _data;
587
- };
588
-
589
- const getDataWithPercent = (data = [], precision = 0) => {
590
- const digits = Math.pow(10, precision);
591
- const targetSeats = digits * 100;
592
-
593
- const total = sum(data, (d) => d.value);
594
-
595
- const votesPerQuota = data.map((d, index) => ({
596
- ...d,
597
- vote: Math.round((d.value / total) * digits * 100),
598
- index,
599
- }));
600
- const currentSum = sum(votesPerQuota, (d) => d.vote);
601
- const remainder = targetSeats - currentSum;
602
-
603
- votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
604
-
605
- const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
606
- ...data,
607
- percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
608
- }));
609
-
610
- return tmp;
611
- };
612
- export {
613
- dateFormat,
614
- getBreakWord,
615
- getTickCoord,
616
- getGridCoord,
617
- identity,
618
- getMousePos,
619
- getFontStyle,
620
- getMargin,
621
- getTranslate3d,
622
- getTranslate2d,
623
- band,
624
- getIcon,
625
- getColorList,
626
- getStacks,
627
- dataYOrZ,
628
- seriesYOrZ,
629
- resetStacks,
630
- getCurrentStack,
631
- getBandBackground,
632
- getBandwidth,
633
- getBandSeriesStepAndWidth,
634
- isValidHttpUrl,
635
- getChildren,
636
- filterChildren,
637
- getDomPath,
638
- sortPie,
639
- getDataWithPercent,
640
- };
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 + ', 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) => {
84
+ switch (type) {
85
+ case 'area':
86
+ case 'line':
87
+ return icon
88
+ ? {
89
+ ...defaultLineIcon,
90
+ ...icon,
91
+ }
92
+ : defaultLineIcon;
93
+ default:
94
+ return icon
95
+ ? {
96
+ ...defaultIcon,
97
+ ...icon,
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
+ const getTickCoord = ({
145
+ orientation,
146
+ coordinate,
147
+ tickSize = 6,
148
+ x = 0,
149
+ y = 0,
150
+ }) => {
151
+ let x1, x2, y1, y2;
152
+ switch (orientation) {
153
+ case 'top':
154
+ x1 = x2 = coordinate;
155
+ y2 = y;
156
+ y1 = y2 - tickSize;
157
+ break;
158
+ case 'left':
159
+ y1 = y2 = coordinate;
160
+ x2 = x;
161
+ x1 = x2 - tickSize;
162
+ break;
163
+ case 'right':
164
+ y1 = y2 = coordinate;
165
+ x2 = x;
166
+ x1 = x2 + tickSize;
167
+ break;
168
+ default:
169
+ x1 = x2 = coordinate;
170
+ y2 = y;
171
+ y1 = y2 + tickSize;
172
+ break;
173
+ }
174
+ return { x1, x2, y1, y2 };
175
+ };
176
+ const getGridCoord = ({ orientation, coordinate, end }) => {
177
+ let x1, x2, y1, y2;
178
+ switch (orientation) {
179
+ case 'top':
180
+ x1 = x2 = coordinate;
181
+ y1 = 0;
182
+ y2 = end;
183
+ break;
184
+ case 'bottom':
185
+ x1 = x2 = coordinate;
186
+ y1 = 0;
187
+ y2 = end * -1;
188
+ break;
189
+ case 'left':
190
+ y1 = y2 = coordinate;
191
+ x1 = 0;
192
+ x2 = end;
193
+ break;
194
+ case 'right':
195
+ y1 = y2 = coordinate;
196
+ x1 = 0;
197
+ x2 = end * -1;
198
+ break;
199
+ }
200
+ return { x1, x2, y1, y2 };
201
+ };
202
+
203
+ const identity = (d) => d;
204
+
205
+ //获取鼠标指针坐标
206
+ const getMousePos = (evt, dom) => {
207
+ var rect = dom.getBoundingClientRect();
208
+ return {
209
+ x: evt.clientX - rect.left,
210
+ y: evt.clientY - rect.top,
211
+ w: rect.width,
212
+ h: rect.height,
213
+ };
214
+ };
215
+
216
+ const getFontStyle = (
217
+ { color, bold, italic, fontSize, fontFamily, letterSpacing },
218
+ type
219
+ ) => {
220
+ if (type == 'svg') {
221
+ return {
222
+ fontSize,
223
+ fontFamily,
224
+ letterSpacing,
225
+ fill: color,
226
+ fontWeight: bold ? 'bold' : 'normal',
227
+ fontStyle: italic ? 'italic' : 'normal',
228
+ };
229
+ }
230
+ return {
231
+ fontSize,
232
+ fontFamily,
233
+ letterSpacing,
234
+ color,
235
+ fontWeight: bold ? 'bold' : 'normal',
236
+ fontStyle: italic ? 'italic' : 'normal',
237
+ };
238
+ };
239
+
240
+ const getMargin = ({ marginTop, marginRight, marginBottom, marginLeft }) =>
241
+ marginTop +
242
+ 'px ' +
243
+ marginRight +
244
+ 'px ' +
245
+ marginBottom +
246
+ 'px ' +
247
+ marginLeft +
248
+ 'px';
249
+ const getTranslate3d = ({ x = 0, y = 0, z = 0 }) =>
250
+ 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)';
251
+ const getTranslate2d = ({ x = 0, y = 0 }) => 'translate(' + x + ', ' + y + ')';
252
+ function band() {
253
+ var scale = ordinal().unknown(undefined),
254
+ domain = scale.domain,
255
+ ordinalRange = scale.range,
256
+ r0 = 0,
257
+ r1 = 1,
258
+ step,
259
+ bandwidth,
260
+ round = false,
261
+ paddingInner = 0,
262
+ paddingOuter = 0,
263
+ // seriesPaddingInner = 0,
264
+ // seriesPaddingOuter = 0,
265
+ // seriesLength = 0,
266
+ align = 0.5;
267
+
268
+ delete scale.unknown;
269
+
270
+ function rescale() {
271
+ var n = domain().length,
272
+ reverse = r1 < r0,
273
+ start = reverse ? r1 : r0,
274
+ stop = reverse ? r0 : r1;
275
+ step = (stop - start) / Math.max(1, n - paddingOuter * 2);
276
+ if (round) step = Math.floor(step);
277
+ start += (stop - start - step * n) * align;
278
+ bandwidth = step;
279
+ if (round) (start = Math.round(start)), (bandwidth = Math.round(bandwidth));
280
+ var values = sequence(n).map(function (i) {
281
+ return start + step * i + step / 2;
282
+ });
283
+ return ordinalRange(reverse ? values.reverse() : values);
284
+ }
285
+
286
+ scale.domain = function (_) {
287
+ return arguments.length ? (domain(_), rescale()) : domain();
288
+ };
289
+
290
+ scale.range = function (_) {
291
+ return arguments.length
292
+ ? (([r0, r1] = _), (r0 = +r0), (r1 = +r1), rescale())
293
+ : [r0, r1];
294
+ };
295
+
296
+ scale.rangeRound = function (_) {
297
+ return ([r0, r1] = _), (r0 = +r0), (r1 = +r1), (round = true), rescale();
298
+ };
299
+
300
+ scale.bandwidth = function () {
301
+ return bandwidth;
302
+ };
303
+
304
+ scale.step = function () {
305
+ return step;
306
+ };
307
+
308
+ scale.seriesBandwidth = function () {
309
+ return seriesBandwidth;
310
+ };
311
+
312
+ scale.seriesStep = function () {
313
+ return seriesStep;
314
+ };
315
+
316
+ scale.round = function (_) {
317
+ return arguments.length ? ((round = !!_), rescale()) : round;
318
+ };
319
+
320
+ scale.padding = function (_) {
321
+ return arguments.length
322
+ ? ((paddingInner = Math.min(1, (paddingOuter = +_))), rescale())
323
+ : paddingInner;
324
+ };
325
+
326
+ scale.paddingInner = function (_) {
327
+ return arguments.length
328
+ ? ((paddingInner = Math.min(1, _)), rescale())
329
+ : paddingInner;
330
+ };
331
+
332
+ scale.paddingOuter = function (_) {
333
+ return arguments.length ? ((paddingOuter = +_), rescale()) : paddingOuter;
334
+ };
335
+
336
+ scale.align = function (_) {
337
+ return arguments.length
338
+ ? ((align = Math.max(0, Math.min(1, _))), rescale())
339
+ : align;
340
+ };
341
+
342
+ scale.copy = function () {
343
+ return band(domain(), [r0, r1])
344
+ .round(round)
345
+ .paddingInner(paddingInner)
346
+ .paddingOuter(paddingOuter)
347
+ .align(align);
348
+ };
349
+
350
+ return initRange.apply(rescale(), arguments);
351
+ }
352
+
353
+ function initRange(domain, range) {
354
+ switch (arguments.length) {
355
+ case 0:
356
+ break;
357
+ case 1:
358
+ this.range(domain);
359
+ break;
360
+ default:
361
+ this.range(range).domain(domain);
362
+ break;
363
+ }
364
+ return this;
365
+ }
366
+
367
+ const getStacks = (series) => {
368
+ const tmp = [];
369
+ series.forEach(({ type, stack, yOrZ }, name) => {
370
+ const current = tmp.find(
371
+ ({ type: _type, stack: _stack, yOrZ: _yOrZ }) =>
372
+ _type == type && stack && _stack == stack && yOrZ == _yOrZ
373
+ );
374
+ if (!current) {
375
+ const common = {
376
+ type,
377
+ stack,
378
+ positive: 0,
379
+ negative: 0,
380
+ yOrZ,
381
+ s: [name],
382
+ };
383
+ if (type === 'band') {
384
+ const index = tmp.filter((item) => item.type === 'band').length;
385
+ tmp.push({
386
+ ...common,
387
+ index,
388
+ });
389
+ } else {
390
+ tmp.push({
391
+ ...common,
392
+ index: 0,
393
+ });
394
+ }
395
+ } else {
396
+ current.s.push(name);
397
+ }
398
+ });
399
+ return tmp;
400
+ };
401
+
402
+ const dataYOrZ = (data, { y: seriesY, z: seriesZ }) => {
403
+ const tmp = {
404
+ y: [],
405
+ z: [],
406
+ };
407
+ for (let i = 0, j = data.length; i < j; i++) {
408
+ const d = data[i];
409
+ if (seriesY.get(d.s)) {
410
+ tmp.y.push(d);
411
+ continue;
412
+ }
413
+ if (seriesZ.get(d.s)) {
414
+ tmp.z.push(d);
415
+ }
416
+ }
417
+ return tmp;
418
+ };
419
+
420
+ const seriesYOrZ = (series) => {
421
+ const y = new Map();
422
+ const z = new Map();
423
+ series.forEach((value, key) => {
424
+ if (value.yOrZ === 'y') {
425
+ y.set(key, value);
426
+ } else {
427
+ z.set(key, value);
428
+ }
429
+ });
430
+ return { y, z };
431
+ };
432
+
433
+ const resetStacks = (stacks) => {
434
+ stacks.forEach((stack) => {
435
+ stack.positive = 0;
436
+ stack.negative = 0;
437
+ });
438
+ };
439
+
440
+ const getCurrentStack = (stack, stackMap) =>
441
+ stackMap.find(
442
+ ({ stack: _stack, type: _type, yOrZ: _yOrZ, s: _s }) =>
443
+ _type == stack.type &&
444
+ _stack == stack.stack &&
445
+ _yOrZ == stack.yOrZ &&
446
+ _s.includes(stack.name)
447
+ );
448
+
449
+ const getBandBackground = (pattern, fill) => {
450
+ if (!(pattern && pattern.path)) return getColor(fill);
451
+ const { backgroundSize = '100% 100%', ..._pattern } = pattern;
452
+ return (
453
+ 'center top / ' +
454
+ backgroundSize +
455
+ ' url("data:image/svg+xml,' +
456
+ encodeURIComponent(
457
+ renderToStaticMarkup(<SvgBackground fill={fill} pattern={_pattern} />)
458
+ ) +
459
+ '")'
460
+ );
461
+ };
462
+ const getBandwidth = (step, paddingOuter) =>
463
+ step / Math.max(1, 1 + paddingOuter);
464
+
465
+ const getBandSeriesStepAndWidth = ({ width, paddingInner, bandLength }) => {
466
+ const seriesStep = width / Math.max(1, bandLength - paddingInner);
467
+ const seriesWidth = seriesStep * (1 - paddingInner);
468
+ return {
469
+ seriesStep,
470
+ seriesWidth,
471
+ };
472
+ };
473
+ const isValidHttpUrl = (string) => {
474
+ let url;
475
+
476
+ try {
477
+ url = new URL(string);
478
+ } catch (_) {
479
+ return false;
480
+ }
481
+
482
+ return url.protocol === 'http:' || url.protocol === 'https:';
483
+ };
484
+
485
+ const getChildren = (svgStr) => {
486
+ const wrapper = document.createElement('div');
487
+ wrapper.innerHTML = svgStr;
488
+ const { childNodes } = wrapper;
489
+ const svgDom = [...childNodes].find((item) => item.tagName === 'svg');
490
+
491
+ if (!!svgDom) {
492
+ return [...svgDom.childNodes];
493
+ }
494
+
495
+ return null;
496
+ };
497
+
498
+ const filterChildren = (children, tagNames) => {
499
+ return children.reduce((prev, node) => {
500
+ let { nodeName } = node;
501
+
502
+ if (tagNames.indexOf(nodeName) > -1) {
503
+ if (nodeName === 'g') {
504
+ return filterChildren([...node.childNodes], tagNames);
505
+ } else {
506
+ prev.push(node);
507
+ }
508
+ }
509
+
510
+ return prev;
511
+ }, []);
512
+ };
513
+
514
+ const getDomPath = (node) => {
515
+ switch (node.nodeName) {
516
+ case 'circle':
517
+ return toPath({
518
+ type: 'circle',
519
+ cx: +node.getAttribute('cx') || 0,
520
+ cy: +node.getAttribute('cy') || 0,
521
+ r: +node.getAttribute('r') || 0,
522
+ });
523
+
524
+ case 'ellipse':
525
+ return toPath({
526
+ type: 'ellipse',
527
+ cx: +node.getAttribute('cx') || 0,
528
+ cy: +node.getAttribute('cy') || 0,
529
+ rx: +node.getAttribute('rx') || 0,
530
+ ry: +node.getAttribute('ry') || 0,
531
+ });
532
+
533
+ case 'line':
534
+ return toPath({
535
+ type: 'line',
536
+ x1: +node.getAttribute('x1') || 0,
537
+ x2: +node.getAttribute('x2') || 0,
538
+ y1: +node.getAttribute('y1') || 0,
539
+ y2: +node.getAttribute('y2') || 0,
540
+ });
541
+
542
+ case 'path':
543
+ return toPath({
544
+ type: 'path',
545
+ d: node.getAttribute('d') || '',
546
+ });
547
+
548
+ case 'polygon':
549
+ return toPath({
550
+ type: 'polyline',
551
+ points: node.getAttribute('points') || '',
552
+ });
553
+
554
+ case 'polyline':
555
+ return toPath({
556
+ type: 'polyline',
557
+ points: node.getAttribute('points') || '',
558
+ });
559
+
560
+ case 'rect':
561
+ return toPath({
562
+ type: 'rect',
563
+ height: +node.getAttribute('height') || 0,
564
+ width: +node.getAttribute('width') || 0,
565
+ x: +node.getAttribute('x') || 0,
566
+ y: +node.getAttribute('y') || 0,
567
+ rx: +node.getAttribute('rx') || 0,
568
+ ry: +node.getAttribute('ry') || 0,
569
+ });
570
+ }
571
+ };
572
+
573
+ const sortPie = (data, order) => {
574
+ const _data = data.map((item) => ({ ...item }));
575
+ switch (order) {
576
+ case '':
577
+ _data.sort(({ index: a }, { index: b }) => ascending(a, b));
578
+ break;
579
+ case 'desc':
580
+ _data.sort(({ value: a }, { value: b }) => descending(a, b));
581
+ break;
582
+ case 'asc':
583
+ _data.sort(({ value: a }, { value: b }) => ascending(a, b));
584
+ break;
585
+ }
586
+ return _data;
587
+ };
588
+
589
+ const getDataWithPercent = (data = [], precision = 0) => {
590
+ const digits = Math.pow(10, precision);
591
+ const targetSeats = digits * 100;
592
+
593
+ const total = sum(data, (d) => d.value);
594
+
595
+ const votesPerQuota = data.map((d, index) => ({
596
+ ...d,
597
+ vote: Math.round((d.value / total) * digits * 100),
598
+ index,
599
+ }));
600
+ const currentSum = sum(votesPerQuota, (d) => d.vote);
601
+ const remainder = targetSeats - currentSum;
602
+
603
+ votesPerQuota.sort(({ value: a }, { value: b }) => (a % total) - (b % total));
604
+
605
+ const tmp = votesPerQuota.map(({ vote, ...data }, index) => ({
606
+ ...data,
607
+ percent: toFixed((vote + (index < remainder ? 1 : 0)) / digits, precision),
608
+ }));
609
+
610
+ return tmp;
611
+ };
612
+ export {
613
+ dateFormat,
614
+ getBreakWord,
615
+ getTickCoord,
616
+ getGridCoord,
617
+ identity,
618
+ getMousePos,
619
+ getFontStyle,
620
+ getMargin,
621
+ getTranslate3d,
622
+ getTranslate2d,
623
+ band,
624
+ getIcon,
625
+ getColorList,
626
+ getStacks,
627
+ dataYOrZ,
628
+ seriesYOrZ,
629
+ resetStacks,
630
+ getCurrentStack,
631
+ getBandBackground,
632
+ getBandwidth,
633
+ getBandSeriesStepAndWidth,
634
+ isValidHttpUrl,
635
+ getChildren,
636
+ filterChildren,
637
+ getDomPath,
638
+ sortPie,
639
+ getDataWithPercent,
640
+ };