@d-matrix/utils 1.16.1 → 1.17.0

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.
package/dist/echarts.d.ts CHANGED
@@ -1,3 +1,33 @@
1
1
  import deepmerge from 'deepmerge';
2
2
  import { EChartsOption } from 'echarts';
3
3
  export declare function mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption;
4
+ /**
5
+ * 场景:后端接口返回某几个时间的点的数据,需求是在接口数据的基础上每隔5分钟补一个点,以达到图中的效果
6
+ * https://raw.githubusercontent.com/mrdulin/pic-bucket-01/master/Dingtalk_20240724140535.jpg
7
+ * 每5分钟补一个点, Y轴值为前一个点的值
8
+ * 时间示例: [9:23, 9:27] => [9:23, 9:25, 9:27, 9:30]
9
+ *
10
+ * 填充的点只包含x,y轴字段,入参数据保持不变
11
+ */
12
+ export declare function fill<T extends Record<string, any>, XAxisField extends keyof T, YAxisField extends keyof T>(dataSource: T[], xAxisField: XAxisField, yAxisField: YAxisField): {
13
+ [x: string]: number | T[YAxisField];
14
+ }[];
15
+ /**
16
+ * 最大差值倍率计算;基础倍率1.2,Y轴刻度精度为0.01,并且Y轴刻度需要被5等分(即max - min需要被5等分),否则会出现重复的刻度;
17
+ * 判断max-min是否被5等分(是否小于0.01),小于0.01表示不能被5等分,基础倍率增加0.1后,再次判断,直到被5等分
18
+ * @param first
19
+ * @param maxDiff
20
+ * @returns 最大差值倍率
21
+ */
22
+ export declare const getDiffRate: (first: number, maxDiff: number, splitNumber?: number) => number;
23
+ /**
24
+ * 计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置
25
+ * @param data 原始数据
26
+ * @param key Y轴字段
27
+ * @param splitNumber Y轴splitNumber属性
28
+ * @returns
29
+ */
30
+ export declare function calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, splitNumber?: number): {
31
+ max: number;
32
+ min: number;
33
+ };
package/dist/echarts.js CHANGED
@@ -1,4 +1,7 @@
1
+ import dayjs from 'dayjs';
2
+ import Decimal from 'decimal.js-light';
1
3
  import deepmerge from 'deepmerge';
4
+ import { trueTypeOf } from './operator';
2
5
  const combineMerge = (target, source, options) => {
3
6
  const destination = target.slice();
4
7
  source.forEach((item, index) => {
@@ -17,3 +20,92 @@ const combineMerge = (target, source, options) => {
17
20
  export function mergeOption(defaults, overrides, option) {
18
21
  return deepmerge(defaults, overrides, Object.assign({ arrayMerge: combineMerge }, option));
19
22
  }
23
+ /**
24
+ * 场景:后端接口返回某几个时间的点的数据,需求是在接口数据的基础上每隔5分钟补一个点,以达到图中的效果
25
+ * https://raw.githubusercontent.com/mrdulin/pic-bucket-01/master/Dingtalk_20240724140535.jpg
26
+ * 每5分钟补一个点, Y轴值为前一个点的值
27
+ * 时间示例: [9:23, 9:27] => [9:23, 9:25, 9:27, 9:30]
28
+ *
29
+ * 填充的点只包含x,y轴字段,入参数据保持不变
30
+ */
31
+ // TODO improve TS types
32
+ export function fill(dataSource, xAxisField, yAxisField) {
33
+ const data = [];
34
+ for (let i = 0; i < dataSource.length; i++) {
35
+ const current = dataSource[i];
36
+ const next = dataSource[i + 1];
37
+ data.push(Object.assign(Object.assign({}, current), { [xAxisField]: dayjs(current[xAxisField]).valueOf(), [yAxisField]: current[yAxisField] }));
38
+ if (next !== null && next !== undefined) {
39
+ let currentDate = dayjs(current[xAxisField]);
40
+ const nextDate = dayjs(next[xAxisField]);
41
+ const timeDiff = dayjs(next[xAxisField]).diff(dayjs(current[xAxisField]), 'minute');
42
+ let count = Math.ceil(timeDiff / 5);
43
+ const remainder = currentDate.minute() % 5;
44
+ if (remainder > 0 && nextDate.minute() % 5 !== 0) {
45
+ currentDate = dayjs(current[xAxisField]).add(5 - remainder, 'minute');
46
+ data.push({ [xAxisField]: currentDate.valueOf(), [yAxisField]: current[yAxisField] });
47
+ }
48
+ for (let j = 1; j < count; j++) {
49
+ data.push({
50
+ [xAxisField]: currentDate
51
+ .clone()
52
+ .add(5 * j, 'minute')
53
+ .valueOf(),
54
+ [yAxisField]: current[yAxisField],
55
+ });
56
+ }
57
+ }
58
+ }
59
+ return data;
60
+ }
61
+ /**
62
+ * 最大差值倍率计算;基础倍率1.2,Y轴刻度精度为0.01,并且Y轴刻度需要被5等分(即max - min需要被5等分),否则会出现重复的刻度;
63
+ * 判断max-min是否被5等分(是否小于0.01),小于0.01表示不能被5等分,基础倍率增加0.1后,再次判断,直到被5等分
64
+ * @param first
65
+ * @param maxDiff
66
+ * @returns 最大差值倍率
67
+ */
68
+ export const getDiffRate = (first, maxDiff, splitNumber = 5) => {
69
+ let diffRate = 1.2;
70
+ if (first === 0)
71
+ return diffRate;
72
+ const decimalPlaces = new Decimal(first).dp();
73
+ const minDiff = 1 / Math.pow(10, decimalPlaces);
74
+ function calc(f, d) {
75
+ const max = f + d * diffRate;
76
+ const min = f - d * diffRate;
77
+ if ((max - min) / splitNumber < minDiff) {
78
+ diffRate += 0.1;
79
+ return calc(f, d);
80
+ }
81
+ return diffRate;
82
+ }
83
+ return calc(first, maxDiff);
84
+ };
85
+ /**
86
+ * 计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置
87
+ * @param data 原始数据
88
+ * @param key Y轴字段
89
+ * @param splitNumber Y轴splitNumber属性
90
+ * @returns
91
+ */
92
+ export function calcYAxisRange(data, key, splitNumber = 5) {
93
+ var _a, _b;
94
+ const maxValue = (_a = Math.max(...data.map((o) => o[key]))) !== null && _a !== void 0 ? _a : 0;
95
+ const minValue = (_b = Math.min(...data.map((o) => o[key]))) !== null && _b !== void 0 ? _b : 0;
96
+ let firstValue = 0;
97
+ for (const item of data) {
98
+ if (trueTypeOf(item[key]) === 'number') {
99
+ firstValue = item[key];
100
+ break;
101
+ }
102
+ }
103
+ // 最大值与第一个点的差值,最小值与第一个点的差值,取最大
104
+ // 例如:first = 1, max = 1.3, min = 0.6, maxDiff = abs(min - first) = 0.4
105
+ const maxDiff = Math.max(Math.abs(maxValue - firstValue), Math.abs(minValue - firstValue));
106
+ // 差值缩放比例
107
+ const diffRate = getDiffRate(firstValue, maxDiff, splitNumber);
108
+ const max = firstValue + (maxDiff === 0 ? firstValue / 6 : maxDiff * diffRate);
109
+ const min = firstValue - (maxDiff === 0 ? firstValue / 6 : maxDiff * diffRate);
110
+ return { max, min };
111
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.16.1",
4
+ "version": "1.17.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
@@ -61,6 +61,7 @@
61
61
  "vite": "^4.5.2"
62
62
  },
63
63
  "dependencies": {
64
+ "dayjs": "^1.11.12",
64
65
  "decimal.js-light": "^2.5.1",
65
66
  "deepmerge": "^4.3.1",
66
67
  "react-fast-compare": "^3.2.2"
package/readme.md CHANGED
@@ -440,6 +440,16 @@ removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g:
440
440
 
441
441
  deep merge Echarts配置,用法见[测试用例](./tests//echarts/echarts.cy.ts)
442
442
 
443
+ - `fill<T extends Record<string, any>, XAxisField extends keyof T, YAxisField extends keyof T>(dataSource: T[], xAxisField:XAxisField, yAxisField: YAxisField): T[]`
444
+
445
+ 场景:后端接口返回某几个时间点的数据,需求是在接口数据的基础上每隔5分钟补一个点,以达到图中的效果: [折线图](https://raw.githubusercontent.com/mrdulin/pic-bucket-01/master/Dingtalk_20240724140535.jpg)。
446
+
447
+ 填充的点的Y轴值为前一个点的值, 时间示例: [9:23, 9:27] => [9:23, 9:25, 9:27, 9:30],更多,见[测试用例](./tests/echarts/fill.cy.ts)
448
+
449
+ - `calcYAxisRange<T extends Record<string, any>, Key extends keyof T>(data: T[], key: Key, splitNumber = 5): { max:number; min:number }`
450
+
451
+ 计算echarts YAxis的max和min属性,以达到根据实际数据动态调整,使折线图的波动明显。且第一个点始终在Y轴中间位置,[效果图](https://raw.githubusercontent.com/mrdulin/pic-bucket-01/master/Dingtalk_20240724140535.jpg)
452
+
443
453
  ## 测试
444
454
 
445
455
  运行全部组件测试