1+ export class ArrayStatistic {
2+
3+ // geostatsInstance: null,
4+
5+ /**
6+ * 初始化插件实例
7+ */
8+ static newInstance ( ) {
9+ // if(!this.geostatsInstance) {
10+ // // this.geostatsInstance = new geostats();
11+ // // }
12+ if ( ! this . geostatsInstance ) {
13+
14+ this . geostatsInstance = new window . geostats ( ) ;
15+ }
16+ return this . geostatsInstance ;
17+ }
18+
19+ /**
20+ * 设置需要被处理的数组
21+ *
22+ * @param array
23+ */
24+ static getInstance ( array ) {
25+ let instance = this . newInstance ( ) ;
26+ instance . setSerie ( array ) ;
27+ return instance ;
28+ }
29+
30+ /**
31+ * 获取数组统计的值
32+ *
33+ * @param array 需要统计的数组
34+ * @param type 统计方法
35+ */
36+ static getArrayStatistic ( array , type ) {
37+ if ( ! array . length ) {
38+ return 0 ;
39+ }
40+ if ( type === "Sum" || type === "求和" ) {
41+ return this . getSum ( array ) ;
42+ } else if ( type === "Maximum" || type === "最大值" ) {
43+ return this . getMax ( array ) ;
44+ } else if ( type === "Minimum" || type === "最小值" ) {
45+ return this . getMin ( array ) ;
46+ } else if ( type === "Average" || type === "平均值" ) {
47+ return this . getMean ( array ) ;
48+ } else if ( type === "Median" || type === "中位数" ) {
49+ return this . getMedian ( array ) ;
50+ } else if ( type === "times" || type === "计数" ) {
51+ return this . getTimes ( array ) ;
52+ }
53+ }
54+
55+ /**
56+ * 获取数组分段后的数值
57+ *
58+ * @param array 需要分段的数组
59+ * @param type 分段方法
60+ * @param segNum 分段个数
61+ */
62+ static getArraySegments ( array , type , segNum ) {
63+ if ( type === "offset" ) {
64+ return this . getEqInterval ( array , segNum ) ;
65+ } else if ( type === "jenks" ) {
66+ return this . getJenks ( array , segNum ) ;
67+ } else if ( type === "square" ) {
68+ // 数据都必须 >= 0
69+ let minValue = this . getMin ( array ) ;
70+ if ( minValue >= 0 ) {
71+ return this . getSqrtInterval ( array , segNum ) ;
72+ } else {
73+ //console.log('数据都必须 >= 0');
74+ // Util.showMessage(Language.hasNegValue + Language.noSupportRange, 'ERROR');
75+ return false ;
76+ }
77+
78+ } else if ( type === "logarithm" ) {
79+ // 数据都必须 > 0
80+ let minValue = this . getMin ( array ) ;
81+ if ( minValue > 0 ) {
82+ return this . getGeometricProgression ( array , segNum ) ;
83+ } else {
84+ //console.log('数据都必须 > 0');
85+ // Util.showMessage(Language.hasZeroNegValue + Language.noSupportRange, 'ERROR');
86+ return false ;
87+ }
88+ }
89+ }
90+
91+ /**
92+ * 求和
93+ * @param array
94+ * @returns {number }
95+ */
96+ static getSum ( array ) {
97+ return this . getInstance ( array ) . sum ( ) ;
98+ }
99+
100+ /**
101+ * 最小值
102+ * @param array
103+ * @returns {* }
104+ */
105+ static getMax ( array ) {
106+ return this . getInstance ( array ) . max ( ) ;
107+ }
108+
109+ /**
110+ * 最大值
111+ * @param array
112+ * @returns {* }
113+ */
114+ static getMin ( array ) {
115+ return this . getInstance ( array ) . min ( ) ;
116+ }
117+
118+ /**
119+ * 求平均
120+ * @param array
121+ * @returns {number }
122+ */
123+ static getMean ( array ) {
124+ return this . getInstance ( array ) . mean ( ) ;
125+ }
126+
127+ /**
128+ * 求中位数
129+ *
130+ * @param array
131+ * @returns {number }
132+ */
133+ static getMedian ( array ) {
134+ return this . getInstance ( array ) . median ( ) ;
135+ }
136+
137+ /**
138+ * 计数
139+ *
140+ * @param array
141+ * @returns {number }
142+ */
143+ static getTimes ( array ) {
144+ return array . length ;
145+ }
146+
147+ /**
148+ * 等距分段法
149+ *
150+ * @param array
151+ * @param segNum
152+ */
153+ static getEqInterval ( array , segNum ) {
154+ return this . getInstance ( array ) . getClassEqInterval ( segNum ) ;
155+ }
156+
157+ /**
158+ * 自然断裂法
159+ *
160+ * @param array
161+ * @param segNum
162+ */
163+ static getJenks ( array , segNum ) {
164+ return this . getInstance ( array ) . getClassJenks ( segNum ) ;
165+ }
166+
167+ /**
168+ * 平方根分段法
169+ *
170+ * @param array
171+ * @param segNum
172+ */
173+ static getSqrtInterval ( array , segNum ) {
174+ array = array . map ( function ( value ) {
175+ return Math . sqrt ( value ) ;
176+ } ) ;
177+ let breaks = this . getInstance ( array ) . getClassEqInterval ( segNum ) ;
178+ return (
179+ breaks . map ( function ( value ) {
180+ return value * value ;
181+ } )
182+ )
183+ }
184+
185+ /**
186+ * 对数分段法
187+ *
188+ * @param array
189+ * @param segNum
190+ */
191+ static getGeometricProgression ( array , segNum ) {
192+ return this . getInstance ( array ) . getClassGeometricProgression ( segNum ) ;
193+ }
194+
195+ }
0 commit comments