111
hxb
2022-11-24 0a3e07f10937484145f33c7560607b4b2353cb81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.model;
 
import android.graphics.Color;
 
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.adapter.DefaultValueAdapter;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.adapter.IValueAdapter;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.data.Line;
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.Utils;
 
import java.util.List;
 
/**
 * 轴线相关
 * -----------------------
 * 区域事先约定,优于计算!
 * <p>
 */
 
public abstract class Axis {
 
    public static final float D_AXIS_WIDTH = 2;// d 代表default
//    public static final float D_LEG_WIDTH = 5;
    public static final float D_LEG_WIDTH = 0;
    public static final float D_LABEL_TXT = 7;
    public static final float D_UNIT_TXT = 12;
 
    public static final int D_LABEL_COUNT = 0;// 建议label的数量,决定从外部传
 
 
    //////////////////////////  label 相关  /////////////////////////
    double[] labelValues = new double[]{};
    int labelCount = 6;
    int _labelCountAdvice = D_LABEL_COUNT;
    float labelDimen;
    int labelColor = Color.BLACK;
    float labelTextSize;
 
 
 
    String duration;
    CalWay calWay = CalWay.perfect;
 
    ///////////////////////////////  unit 相关  ////////////////////////////
    boolean _enableUnit = true;
    String _unit = "";
    float unitDimen;
    float unitTxtSize;
    int unitColor = Color.RED;
 
    /////////////////////////////////  轴线相关  //////////////////////////////////
    int axisColor = Color.BLACK;
    float axisWidth;
    float leg;// 轴线上的小腿(多出来的小不点:叫他小腿吧)
 
    /////////////////////////////////  预警线相关  ////////////////////////////////
    List<WarnLine> listWarnLins;
 
 
    boolean enable = true;// axis is enable?
    IValueAdapter _ValueAdapter;
 
    public Axis() {
 
        axisWidth = Utils.dp2px(D_AXIS_WIDTH);
        labelTextSize = Utils.dp2px(D_LABEL_TXT);
        leg = Utils.dp2px(D_LEG_WIDTH);
        unitTxtSize = Utils.dp2px(D_UNIT_TXT);
 
        // value adapter
        _ValueAdapter = new DefaultValueAdapter(2);
    }
 
    /**
     * 计算与存储:可见区域内的每一步的数值
     * -----------------------------
     * 注意:可见区域!
     */
    public void calValues(double min, double max, Line line) {
 
        double range;
        range = max - min;
 
        if (Math.abs(max - min) == 0) {
            return;
        }
 
        if (calWay == CalWay.perfect) {
            // 漂亮:展现的更合理
 
            double rawInterval = range / (_labelCountAdvice - 1);
            // 1.以最大数值为量程
            double interval = Utils.roundNumber2One(rawInterval);//314->300
            // 2. 量程>5,则以10为单位
            double intervalMagnitude = Math.pow(10, (int) Math.log10(interval));//100
            int intervalSigDigit = (int) (interval / intervalMagnitude);
            if (intervalSigDigit > 5) {
                interval = Math.floor(10 * intervalMagnitude);// 以10位单位 //
            }
 
            double first = Math.floor(min / interval) * interval;//有几个interval
            double last = Math.ceil(max / interval) * interval;
 
            double f;
            int n = 0;
            if (interval != 0.0) {
                for (f = first; f <= last; f += interval) {
                    ++n;
                }
            }
            labelCount = n;
 
            if (labelValues.length < labelCount) {
                labelValues = new double[labelCount];
            }
 
            f = first;
            for (int i = 0; i < n; f += interval, ++i) {
 
                if (f == 0.0) // Fix for negative zero case (Where value == -0.0, and 0.0 == -0.0)
                    f = 0.0;
 
                labelValues[i] = (float) f;
            }
 
 
        } else if (calWay == CalWay.justAvg) {
            // 平均:达到共产主义
            labelCount = _labelCountAdvice;
 
            if (labelValues.length < labelCount) {
                labelValues = new double[labelCount];
            }
 
            double v = min;
            double interval = range / (labelCount - 1);
 
            labelValues[0] = min;
            for (int i = 1; i < labelCount - 1; i++) {
                v = v + interval;
                labelValues[i] = v;
            }
            labelValues[labelCount - 1] = max;
        } else if (calWay == CalWay.every) {
            // 每个:将可视范围内,这条线上的每个数据在x轴上的label都绘制出来
 
            if (line != null && line.getEntries().size() != 0) {
                int minIndex = Line.getEntryIndex(line.getEntries(), min, Line.Rounding.DOWN);
                int maxIndex = Line.getEntryIndex(line.getEntries(), max, Line.Rounding.UP);
 
                labelCount = (maxIndex - minIndex) + 1;
                if (labelValues.length < labelCount) {
                    labelValues = new double[labelCount];
                }
 
                int count = 0;
                for (int i = minIndex; i <= maxIndex; i++) {
                    if (this instanceof XAxis) {
                        labelValues[count++] = line.getEntries().get(i).getX();
                    } else {
                        labelValues[count++] = line.getEntries().get(i).getY();
                    }
                }
            }
 
        } else if (calWay == CalWay.lc_day) {
            labelValues = new double[]{0, 4, 8,12,16,20,24};
            duration = "DAY";
        }else if (calWay == CalWay.lc_week) {
            labelValues = new double[]{0, 3, 6};
            duration = "WEEK";
        }else if (calWay == CalWay.lc_month) {
            labelValues = new double[]{0, 5, 10, 15, 20, 25, 30};
            duration = "MONTH";
        } else if (calWay == CalWay.lc_year) {
            labelValues = new double[]{0, 1, 5, 9};
            duration = "YEAR";
        }
    }
 
    /**
     * 轴线左边 label和indicator的距离
     *
     * @return
     */
    public float offsetLeft(float labelWidth, float unitHeight) {
        labelDimen = labelWidth;
        unitDimen = unitHeight;
 
        float sum = labelDimen;
        if (_enableUnit) {
            sum += unitDimen;
        }
        sum += leg;
        return sum;
    }
 
    /**
     * 轴线底部 label和indicator的距离
     *
     * @return
     */
    public float offsetBottom(float labelHeight, float unitHeight) {
        labelDimen = labelHeight;
        unitDimen = unitHeight;
 
        float sum;
        sum = labelDimen;
        if (_enableUnit) {
            sum += unitDimen;
        }
        sum += leg;
        return sum;
    }
 
 
    public float getLabelDimen() {
        return labelDimen;
    }
 
    public void setLabelDimen(float labelDimen) {
        this.labelDimen = labelDimen;
    }
 
    public float getUnitDimen() {
        return unitDimen;
    }
 
    public void setUnitDimen(float unitDimen) {
        this.unitDimen = unitDimen;
    }
 
 
    ///////////////////////////////  get set  //////////////////////////////////////
 
 
    public IValueAdapter get_ValueAdapter() {
        return _ValueAdapter;
    }
 
    public void set_ValueAdapter(IValueAdapter _ValueAdapter) {
        this._ValueAdapter = _ValueAdapter;
    }
 
    public boolean is_enableUnit() {
        return _enableUnit;
    }
 
    public void set_enableUnit(boolean _enableUnit) {
        this._enableUnit = _enableUnit;
    }
 
    public int get_labelCountAdvice() {
        return _labelCountAdvice;
    }
 
    public void set_labelCountAdvice(int _labelCountAdvice) {
        this._labelCountAdvice = _labelCountAdvice;
    }
 
    public String get_unit() {
        return _unit;
    }
 
    public void set_unit(String _unit) {
        this._unit = _unit;
    }
 
    public float getUnitTxtSize() {
        return unitTxtSize;
    }
 
    public void setUnitTxtSize(float unitTxtSize) {
        this.unitTxtSize = unitTxtSize;
    }
 
    public int getUnitColor() {
        return unitColor;
    }
 
    public void setUnitColor(int unitColor) {
        this.unitColor = unitColor;
    }
 
    public int getAxisColor() {
        return axisColor;
    }
 
    public void setAxisColor(int axisColor) {
        this.axisColor = axisColor;
    }
 
    public float getAxisWidth() {
        return axisWidth;
    }
 
    public void setAxisWidth(float axisWidth) {
        this.axisWidth = axisWidth;
    }
 
    public boolean isEnable() {
        return enable;
    }
 
    public void setEnable(boolean enable) {
        this.enable = enable;
    }
 
    public float getLeg() {
        return leg;
    }
 
    public void setLeg(float leg) {
        this.leg = leg;
    }
 
    public float getLabelTextSize() {
        return labelTextSize;
    }
 
    public void setLabelTextSize(float labelTextSize) {
        this.labelTextSize = labelTextSize;
    }
 
    public int getLabelColor() {
        return labelColor;
    }
 
    public void setLabelColor(int labelColor) {
        this.labelColor = labelColor;
    }
 
    public int getLabelCount() {
        return labelCount;
    }
 
    public void setLabelCount(int labelCount) {
        this.labelCount = labelCount;
    }
 
    public double[] getLabelValues() {
        return labelValues;
    }
 
    public void setLabelValues(double[] labelValues) {
        this.labelValues = labelValues;
    }
 
    public List<WarnLine> getListWarnLins() {
        return listWarnLins;
    }
 
    public void setListWarnLins(List<WarnLine> listWarnLins) {
        this.listWarnLins = listWarnLins;
    }
 
    public CalWay getCalWay() {
        return calWay;
    }
 
    public String getDuration() {
        return duration;
    }
 
    public void setDuration(String duration) {
        this.duration = duration;
    }
    /**
     * 轴线上一堆label的计算方式
     *
     * @param calWay
     */
    public void setCalWay(CalWay calWay) {
        this.calWay = calWay;
    }
 
    /**
     * 轴线上的一堆数据的计算方式
     */
    public enum CalWay {
        /**
         * 漂亮:展现的更合理
         */
        perfect,
        /**
         * 平均:
         */
        justAvg,
        /**
         * 每个:将可视范围内,这条线上的每个数据在x轴上的label都绘制出来
         */
        every,
 
        lc_day,
        lc_week,
        lc_month,
        lc_year,
 
    }
}