@wings-j/chart-data-preprocess 0.0.0 → 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 氢灵子
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -42,39 +42,28 @@ interface Vector2 {
42
42
  x: number;
43
43
  y: number;
44
44
  }
45
+ type Vector1Array<T extends Vector1 = any> = T[];
46
+ type Vector2Array<T extends Vector2 = any> = T[];
47
+
45
48
  /**
46
- * Vector 1 List
49
+ * Downsample
50
+ * @description Largest triangle three buckets.
47
51
  * @type [T] Origin Type
52
+ * @param [array] Origin Array
53
+ * @param [length] Target Length
54
+ * @return Downsampled Array
48
55
  */
49
- declare class Vector1List<T = any> {
50
- array: T[];
51
- vectors: Vector1[];
52
- get tuples(): {
53
- origin: T;
54
- vector: Vector1 | undefined;
55
- }[];
56
- get xs(): number[];
57
- /**
58
- * Constructor
59
- * @param [array] Array
60
- * @param [xGetter] X Getter
61
- */
62
- constructor(array: T[], xGetter: (value: T) => number);
63
- /**
64
- * Clone
65
- * @param [transformer] Transformer
66
- * @return Cloned List
67
- */
68
- clone(transformer?: (vector: Vector1) => Vector1): Vector1List<T>;
69
- }
56
+ declare function downsample<T extends Vector2 = any>(array: Vector2Array<T>, length: number): (T | undefined)[];
70
57
 
71
58
  /**
72
59
  * Ratio
73
60
  * @type [T] Origin Type
74
- * @param [list] Array
61
+ * @param [array] Array
75
62
  * @return Array with X Ratio
76
63
  */
77
- declare function ratio<T = any>(list: Vector1List<T>): Vector1List<T>;
64
+ declare function ratio<T extends Vector1 = any>(array: Vector1Array<T>): {
65
+ x: number;
66
+ }[];
78
67
 
79
68
  /**
80
69
  * Parse
@@ -83,4 +72,4 @@ declare function ratio<T = any>(list: Vector1List<T>): Vector1List<T>;
83
72
  */
84
73
  declare function parse(value: any): number;
85
74
 
86
- export { type Vector1, Vector1List, type Vector2, max, mean, min, minMax, parse, ratio, sum };
75
+ export { type Vector1, type Vector1Array, type Vector2, type Vector2Array, downsample, max, mean, min, minMax, parse, ratio, sum };
package/dist/index.js CHANGED
@@ -25,10 +25,56 @@ function mean(array) {
25
25
  return sum(array) / array.length;
26
26
  }
27
27
 
28
+ // src/modules/methods/downsample.ts
29
+ function downsample(array, length) {
30
+ if (array.length <= length || array.length <= 3) {
31
+ return Array.from(array);
32
+ } else {
33
+ let temp = [array.at(0)];
34
+ let batch = (array.length - 2) / (length - 2);
35
+ let currentPoint = array.at(0);
36
+ let maxArea = -1;
37
+ let maxAreaPoint = currentPoint;
38
+ for (let i = 0; i < length - 2; i++) {
39
+ maxArea = -1;
40
+ maxAreaPoint = currentPoint;
41
+ let nextBucketAverage = { x: 0, y: 0 };
42
+ let nextBucketRange = [Math.floor((i + 1) * batch) + 1, Math.floor((i + 2) * batch) + 1];
43
+ nextBucketRange[1] = nextBucketRange[1] < array.length ? nextBucketRange[1] : array.length;
44
+ let nextBucketLength = nextBucketRange[1] - nextBucketRange[0];
45
+ if (nextBucketLength > 0) {
46
+ for (let j = nextBucketRange[0]; j < nextBucketRange[1]; j++) {
47
+ nextBucketAverage.x += array[j].x;
48
+ nextBucketAverage.y += array[j].y;
49
+ }
50
+ nextBucketAverage.x /= nextBucketLength;
51
+ nextBucketAverage.y /= nextBucketLength;
52
+ } else {
53
+ let fallback = array[nextBucketRange[0]] ?? array[array.length - 1] ?? currentPoint;
54
+ nextBucketAverage.x = fallback.x;
55
+ nextBucketAverage.y = fallback.y;
56
+ }
57
+ let currentBucketRange = [Math.floor(i * batch) + 1, Math.floor((i + 1) * batch) + 1];
58
+ for (let j = currentBucketRange[0]; j < currentBucketRange[1]; j++) {
59
+ let candidate = array[j];
60
+ let area = Math.abs((currentPoint.x - nextBucketAverage.x) * (candidate.y - currentPoint.y) - (currentPoint.x - candidate.x) * (nextBucketAverage.y - currentPoint.y)) * 0.5;
61
+ if (area > maxArea) {
62
+ maxArea = area;
63
+ maxAreaPoint = candidate;
64
+ }
65
+ }
66
+ temp.push(maxAreaPoint);
67
+ currentPoint = maxAreaPoint;
68
+ }
69
+ temp.push(array.at(-1));
70
+ return temp;
71
+ }
72
+ }
73
+
28
74
  // src/modules/transform.ts
29
- function ratio(list) {
30
- let total = sum(list.xs);
31
- return list.clone((a) => ({ x: a.x / total }));
75
+ function ratio(array) {
76
+ let total = sum(array.map((a) => a.x));
77
+ return array.map((a) => ({ x: a.x / total }));
32
78
  }
33
79
 
34
80
  // src/modules/utility.ts
@@ -47,40 +93,8 @@ function parse(value) {
47
93
  return NaN;
48
94
  }
49
95
  }
50
-
51
- // src/types/vector-list.ts
52
- var Vector1List = class _Vector1List {
53
- xGetter;
54
- array;
55
- vectors;
56
- get xs() {
57
- return this.vectors.map((a) => a.x);
58
- }
59
- /**
60
- * Constructor
61
- * @param [array] Array
62
- * @param [xGetter] X Getter
63
- */
64
- constructor(array, xGetter) {
65
- this.xGetter = xGetter;
66
- this.array = array;
67
- this.vectors = array.map((a) => ({ x: xGetter(a) }));
68
- }
69
- /**
70
- * Clone
71
- * @param [transformer] Transformer
72
- * @return Cloned List
73
- */
74
- clone(transformer) {
75
- let temp = new _Vector1List(this.array, this.xGetter);
76
- if (transformer) {
77
- temp.vectors = temp.vectors.map(transformer);
78
- }
79
- return temp;
80
- }
81
- };
82
96
  export {
83
- Vector1List,
97
+ downsample,
84
98
  max,
85
99
  mean,
86
100
  min,
@@ -89,4 +103,3 @@ export {
89
103
  ratio,
90
104
  sum
91
105
  };
92
- //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wings-j/chart-data-preprocess",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "Tools for preprocessing data before generating charts.",
5
5
  "license": "MIT",
6
6
  "author": "WingsJ",
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/modules/math.ts","../src/modules/transform.ts","../src/modules/utility.ts","../src/types/vector-list.ts"],"sourcesContent":["/**\n * Min\n * @param [array] Array\n * @return Value\n */\nfunction min(array: number[]): number {\n return Math.min(...array);\n}\n/**\n * Max\n * @param [array] Array\n * @return Value\n */\nfunction max(array: number[]): number {\n return Math.max(...array);\n}\n/**\n * Min Max\n * @param [array] Array\n * @return Min Value and Max Value\n */\nfunction minMax(array: number[]): [number, number] {\n let min = Infinity;\n let max = -Infinity;\n\n for (let a of array) {\n if (a < min) {\n min = a;\n }\n if (a > max) {\n max = a;\n }\n }\n\n return [min, max];\n}\n/**\n * Sum\n * @param [array] Array\n * @return Value\n */\nfunction sum(array: number[]): number {\n return array.reduce((p, c) => p + c, 0);\n}\n/**\n * Mean\n * @param [array] Array\n * @return Value\n */\nfunction mean(array: number[]): number {\n return sum(array) / array.length;\n}\n\nexport { max, mean, min, minMax, sum };\n","import type { Vector1List } from '../types/vector-list';\nimport { sum } from './math';\n\n/**\n * Ratio\n * @type [T] Origin Type\n * @param [list] Array\n * @return Array with X Ratio\n */\nfunction ratio<T = any>(list: Vector1List<T>) {\n let total = sum(list.xs);\n\n return list.clone(a => ({ x: a.x / total }));\n}\n\nexport { ratio };\n","/**\n * Parse\n * @param [value] Value\n * @return Number\n */\nfunction parse(value: any): number {\n if (value == null || value === undefined || value === '') {\n return 0;\n } else if (typeof value === 'number') {\n return value;\n } else if (typeof value === 'string') {\n if (value.endsWith('%')) {\n return Number.parseFloat(value) / 100;\n } else {\n return Number.parseFloat(value);\n }\n } else {\n return NaN;\n }\n}\n\nexport { parse };\n","/**\n * Vector 1\n */\ninterface Vector1 {\n x: number;\n}\n/**\n * Vector 2\n */\ninterface Vector2 {\n x: number;\n y: number;\n}\n\n/**\n * Vector 1 List\n * @type [T] Origin Type\n */\nclass Vector1List<T = any> {\n private xGetter: (value: T) => number;\n array: T[];\n vectors: Vector1[];\n\n get xs() {\n return this.vectors.map(a => a.x);\n }\n\n /**\n * Constructor\n * @param [array] Array\n * @param [xGetter] X Getter\n */\n constructor(array: T[], xGetter: (value: T) => number) {\n this.xGetter = xGetter;\n this.array = array;\n this.vectors = array.map(a => ({ x: xGetter(a) }));\n }\n\n /**\n * Clone\n * @param [transformer] Transformer\n * @return Cloned List\n */\n clone(transformer?: (vector: Vector1) => Vector1): Vector1List<T> {\n let temp = new Vector1List(this.array, this.xGetter);\n if (transformer) {\n temp.vectors = temp.vectors.map(transformer);\n }\n\n return temp;\n }\n}\n\nexport { Vector1List };\nexport type { Vector1, Vector2 };\n"],"mappings":";AAKA,SAAS,IAAI,OAAyB;AACpC,SAAO,KAAK,IAAI,GAAG,KAAK;AAC1B;AAMA,SAAS,IAAI,OAAyB;AACpC,SAAO,KAAK,IAAI,GAAG,KAAK;AAC1B;AAMA,SAAS,OAAO,OAAmC;AACjD,MAAIA,OAAM;AACV,MAAIC,OAAM;AAEV,WAAS,KAAK,OAAO;AACnB,QAAI,IAAID,MAAK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