wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
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
package com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.data;
 
import android.graphics.Color;
 
import com.mm.android.deviceaddmodule.mobilecommon.widget.linechart.utils.Utils;
 
import java.util.ArrayList;
import java.util.List;
 
 
public class Line {
 
    private List<Entry> entries = new ArrayList<>();
 
 
    private double mYMax = Double.MIN_VALUE;
    private double mYMin = Double.MAX_VALUE;
    private double mXMax = Double.MIN_VALUE;
    private double mXMin = Double.MAX_VALUE;
 
    private int lineColor = Color.BLACK;
    private int lineWidth = (int) Utils.dp2px(1); //px
    private int circleColor = Color.RED;
    private int circleR = 2;
 
    private boolean isFilled = false;
    private int lineAlpha = 50;
 
    private boolean isEnable = true;
    private boolean isDrawCircle = true;
    private boolean isDrawLegend = false;
    private int legendWidth = 50;
    private int legendHeight = 17;
    private int legendTextSize = 8;
    private String name = "line";
 
    private CallBack_OnEntryClick onEntryClick;
 
    public Line() {
        this(null);
    }
 
    public Line(List<Entry> entries) {
 
        circleR = (int) Utils.dp2px(circleR);
        legendWidth = (int) Utils.dp2px(legendWidth);
        legendTextSize = (int) Utils.dp2px(legendTextSize);
        legendHeight = (int) Utils.dp2px(legendHeight);
 
        if (entries != null) {
            setEntries(entries);
        }
    }
 
    private void calMinMax(List<Entry> entries) {
 
        mYMax = -Double.MAX_VALUE;
        mYMin = Double.MAX_VALUE;
        mXMax = -Double.MAX_VALUE;
        mXMin = Double.MAX_VALUE;
 
        for (Entry entry : entries) {
 
            if (entry.getX() < mXMin) {
                mXMin = entry.getX();
            }
            if (entry.getX() > mXMax) {
                mXMax = entry.getX();
            }
 
            if (entry.getY() < mYMin) {
                mYMin = entry.getY();
            }
            if (entry.getY() > mYMax) {
                mYMax = entry.getY();
            }
        }
    }
 
    /**
     * 在数组的前面添加数据
     *
     * @param entry
     */
    public void addEntryInHead(Entry entry) {
        if (entries == null) {
            entries = new ArrayList<>();
        }
        entries.add(0, entry);
 
        // 计算最大最小
        if (entry.getX() < mXMin) {
            mXMin = entry.getX();
        }
        if (entry.getX() > mXMax) {
            mXMax = entry.getX();
        }
 
        if (entry.getY() < mYMin) {
            mYMin = entry.getY();
        }
        if (entry.getY() > mYMax) {
            mYMax = entry.getY();
        }
    }
 
    public void addEntry(Entry entry) {
        if (entries == null) {
            entries = new ArrayList<>();
        }
        entries.add(entry);
 
        // 计算最大最小
        if (entry.getX() < mXMin) {
            mXMin = entry.getX();
        }
        if (entry.getX() > mXMax) {
            mXMax = entry.getX();
        }
 
        if (entry.getY() < mYMin) {
            mYMin = entry.getY();
        }
        if (entry.getY() > mYMax) {
            mYMax = entry.getY();
        }
    }
 
    public List<Entry> getEntries() {
        return entries;
    }
 
    public void setEntries(List<Entry> entries) {
        // check
        if (entries == null) {
            throw new IllegalArgumentException("entries must be no null");
        }
 
        this.entries = entries;
 
        calMinMax(entries);
    }
 
 
    /**
     * 根据提供的 x数值,找出list中离它最近的数,返回其下标。
     * --------------
     * 考虑到性能的原因,采用二分查找法。(速度很快,不错!)
     *
     * @param entries
     * @param hitValue
     * @param rounding
     * @return
     */
    public static int getEntryIndex(List<Entry> entries, double hitValue, Rounding rounding) {
 
        if (entries == null || entries.isEmpty())
            return -1;
 
        int low = 0;
        int high = entries.size() - 1;
        int closet = low;
 
        while (low < high) {
            int m = (low + high) / 2;
 
            double fm = entries.get(m).getX();// middle
            double fr = entries.get(m + 1).getX();// right
 
            if (hitValue >= fm && hitValue <= fr) {
 
                // 中_右
                double d1 = Math.abs(hitValue - fm);
                double d2 = Math.abs(hitValue - fr);
 
                if (d1 <= d2) {
                    closet = m;
                } else {
                    closet = m + 1;
                }
 
                low = high = closet;
                break;
 
            } else if (hitValue < fm) {
 
                if (m >= 1) {
                    double fl = entries.get(m - 1).getX();// left
 
                    if (hitValue >= fl && hitValue <= fm) {
                        // 中_左
                        double d0 = Math.abs(hitValue - fl);
                        double d1 = Math.abs(hitValue - fm);
 
                        if (d0 <= d1) {
                            closet = m - 1;
                        } else {
                            closet = m;
                        }
 
                        low = high = closet;
                        break;
                    }
                }
 
                high = m - 1;
                closet = high;
            } else if (hitValue > fr) {
                low = m + 1;
                closet = low;
            }
        }
 
        // trick
        high++;
        low--;
        closet = Math.min(Math.max(closet, 0), entries.size() - 1);
 
        int result = low;
        if (rounding == Rounding.UP) {
            result = Math.min(high, entries.size() - 1);//多一个
        } else if (rounding == Rounding.DOWN) {
            result = Math.max(low, 0);//少一个
        } else if (rounding == Rounding.CLOSEST) {
            result = closet;
        }
        return result;
    }
 
 
    public double getmYMax() {
        return mYMax;
    }
 
    public void setmYMax(double mYMax) {
        this.mYMax = mYMax;
    }
 
    public double getmYMin() {
        return mYMin;
    }
 
    public void setmYMin(double mYMin) {
        this.mYMin = mYMin;
    }
 
    public double getmXMax() {
        return mXMax;
    }
 
    public void setmXMax(double mXMax) {
        this.mXMax = mXMax;
    }
 
    public double getmXMin() {
        return mXMin;
    }
 
    public void setmXMin(double mXMin) {
        this.mXMin = mXMin;
    }
 
    public int getLineColor() {
        return lineColor;
    }
 
    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }
 
    public int getLineWidth() {
        return lineWidth;
    }
 
    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
    }
 
    public boolean isEnable() {
        return isEnable;
    }
 
    public void setEnable(boolean enable) {
        isEnable = enable;
    }
 
    public boolean isDrawCircle() {
        return isDrawCircle;
    }
 
    public void setDrawCircle(boolean drawCircle) {
        isDrawCircle = drawCircle;
    }
 
    public int getLegendHeight() {
        return legendHeight;
    }
 
    public void setLegendHeight(int legendHeight) {
        this.legendHeight = legendHeight;
    }
 
    public int getLegendTextSize() {
        return legendTextSize;
    }
 
    public void setLegendTextSize(int legendTextSize) {
        this.legendTextSize = legendTextSize;
    }
 
    public int getLegendWidth() {
        return legendWidth;
    }
 
    public void setLegendWidth(int legendWidth) {
        this.legendWidth = legendWidth;
    }
 
    public boolean isDrawLegend() {
        return isDrawLegend;
    }
 
    public void setDrawLegend(boolean drawLegend) {
        isDrawLegend = drawLegend;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getCircleR() {
        return circleR;
    }
 
    public void setCircleR(int circleR) {
        this.circleR = circleR;
    }
 
    public int getCircleColor() {
        return circleColor;
    }
 
    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
    }
 
    public CallBack_OnEntryClick getOnEntryClick() {
        return onEntryClick;
    }
 
    public void setOnEntryClick(CallBack_OnEntryClick onEntryClick) {
        this.onEntryClick = onEntryClick;
    }
 
    public boolean isFilled() {
        return isFilled;
    }
 
    public void setFilled(boolean filled) {
        isFilled = filled;
    }
 
    public int getLineAlpha() {
        return lineAlpha;
    }
 
    public void setLineAlpha(int lineAlpha) {
        this.lineAlpha = lineAlpha;
    }
 
    public enum Rounding {
        UP,
        DOWN,
        CLOSEST,
    }
 
    public interface CallBack_OnEntryClick {
        void onEntry(Line line, Entry entry);
    }
 
}