mxpopstar
2022-05-03 e75ef5f04f61aa5fbd89fd5c413dcee1819b7a91
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.teechart;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
 
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
 
import com.dev.ntm.BattTestData;
import com.steema.teechart.DateTime;
import com.steema.teechart.TChart;
import com.steema.teechart.drawing.Color;
import com.steema.teechart.events.SeriesMouseEvent;
import com.steema.teechart.events.SeriesMouseListener;
import com.steema.teechart.events.TextResolver;
import com.steema.teechart.legend.LegendStyle;
import com.steema.teechart.styles.Line;
import com.steema.teechart.styles.MarksStyle;
import com.steema.teechart.styles.PointerStyle;
import com.steema.teechart.tools.MarksTip;
import com.steema.teechart.tools.MarksTipMouseAction;
 
/******************************* Tee Line Chart For MonCap **************************************/
public class TMultMonCapLineChart extends TChart
{
    private static final long serialVersionUID = 1L;
    
    public static final byte LineType_GroupVol = 0;
    public static final byte LineType_BattCurr = 1;
    public static final byte LineType_MonVol = 2;
    public static final byte LineType_MonCap = 3;
    public static final byte LineType_MonTmp = 4;
    public static final byte LineType_MonRes = 5;
    public static final byte LineType_MonSer = 6;
    public static final byte LineType_MonConnRes = 7;
    public static final byte LineType_MonSer_Percent = 8;
    
    public static final Color[] Line_Color = new Color[]{
        Color.aqua, Color.bisque, Color.blue, Color.darkGray, Color.fuchsia, 
        Color.gold,Color.green, Color.greenYellow, Color.lightYellow, Color.olive, 
        Color.orange, Color.pink, Color.red, Color.silver, Color.skyBlue, 
        Color.teal, Color.white, Color.yellow};
    
    private ArrayList<Line> al_LineSer;
    private byte m_LineType;
    private String m_LeftAxalValueFormat = "#";
    private MarksTip tooltip1;
    private boolean m_ShowChartMarks = true;
    
    public double maxValue = 0;
    public double minValue = 0;
    
    String serial_title = "";
    private Line m_SelectedLine = null;
    private SeriesMouseListener m_SeriesMouseListener = null;
    
    public TMultMonCapLineChart(byte line_type)
    {
        m_LineType = line_type;
        al_LineSer = new ArrayList<Line>();
        
        this.getChart().getTitle().setVisible(false);
        this.getLegend().setVisible(true);
        this.getLegend().setColor(Color.transparent);
        this.getLegend().setLegendStyle(LegendStyle.SERIES);
        this.getLegend().setFontSeriesColor(true);
        this.getAspect().setView3D(false);
        this.getPanel().setMarginLeft(0.2);
        this.getPanel().setMarginRight(1);
        this.getPanel().setMarginTop(2);
        this.getPanel().setMarginBottom(2);
        this.getPanel().setColor(Color.BLACK);
        this.getAxes().getLeft().getLabels().getFont().setColor(Color.GRAY);
        this.getAxes().getLeft().getGrid().setColor(Color.fromArgb(50, 50, 50));
        this.getAxes().getLeft().getAxisPen().setColor(Color.GRAY);
        this.getAxes().getBottom().getAxisPen().setColor(Color.GRAY);
        this.getAxes().getBottom().getLabels().getFont().setColor(Color.GRAY);
        this.getAxes().getBottom().getLabels().setDateTimeFormat("YYYY-MM-dd HH:mm");
        this.getAxes().getBottom().getGrid().setVisible(false);
        
        this.addMouseListener(new MouseListener(){
 
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3)
                {
                    JPopupMenu popupMenu = new JPopupMenu();
                    JMenuItem it_showhide_mark = new JMenuItem("ÏÔʾ/Òþ²Ø ÊýÖµ±êÇ©");
                    it_showhide_mark.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            m_ShowChartMarks = !m_ShowChartMarks;
                            for(int n=0; n<al_LineSer.size(); n++)
                                al_LineSer.get(n).getMarks().setVisible(m_ShowChartMarks);
                        }
                    });
                    JMenuItem it_showhide_legend = new JMenuItem("ÏÔʾ/Òþ²Ø Í¼Àý");
                    it_showhide_legend.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                            boolean vs = TMultMonCapLineChart.this.getLegend().getVisible();
                            TMultMonCapLineChart.this.getLegend().setVisible(!vs);
                        }
                    });
                    
                    popupMenu.add(it_showhide_mark);
                    popupMenu.add(it_showhide_legend);
                    
                    popupMenu.show(e.getComponent(), e.getX(), e.getY());
                }
            }
 
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
 
            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
 
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }
 
            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub
                
            }});
        
        tooltip1 = new MarksTip(this.getChart());
        tooltip1.setMouseDelay(50);
        tooltip1.setMouseAction(MarksTipMouseAction.MOVE);
        tooltip1.setStyle(MarksStyle.XY);
        tooltip1.setToolTipResolver(new TextResolver(){
 
            @Override
            public String getText(Object arg0, String arg1) {
                // TODO Auto-generated method stub
                return serial_title + arg1.replace(" ", "    ");
            }
        });
        
      //-------------------------------------------------------//
        m_SeriesMouseListener = new SeriesMouseListener(){
 
            @Override
            public void seriesClicked(SeriesMouseEvent arg0) {
                // TODO Auto-generated method stub
                //if(arg0.getMouseEvent().getClickCount() > 1)
                //    System.out.println("seriesClicked");
            }
 
            @Override
            public void seriesEntered(SeriesMouseEvent arg0) {
                // TODO Auto-generated method stub
                Line L = (Line) arg0.getSource();
                if(L.getTitle().contains("#"))
                {
                    L.getLinePen().setWidth(2);
                }
                m_SelectedLine = L;
                serial_title = m_SelectedLine.getTitle() + "    ";
                //int index = (int) L.xScreenToValue((int) arg0.getPoint().getX());
                //System.out.println(al_LineSer.get(0).getLabels().get(index));
            }
            
            @Override
            public void seriesExited(SeriesMouseEvent arg0) {
                // TODO Auto-generated method stub
                Line L = (Line) arg0.getSource();
                if(L.getTitle().contains("#"))
                {
                    L.getLinePen().setWidth(1);
                }
                m_SelectedLine = null;
                serial_title = "";
            }
        };
        //-------------------------------------------------------//
    }
    
    
    
    public void setChartLineType(byte line_type)
    {
        m_LineType = line_type;
    }
    
    public void restoreZoom()
    {
        this.getZoom().undo();
        this.getAxes().getBottom().setMinMax(al_LineSer.get(0).getMinXValue()-1, 
                                                    al_LineSer.get(0).getMaxXValue()*1.05);
    }
    
    public void setLineSerialVisiable(int batt_num, boolean state)
    {
        if(-1 == batt_num)
        {
            for(int n=0; n<al_LineSer.size(); n++)
            {
                if(false == al_LineSer.get(n).getTitle().contains("#AVG"))
                    al_LineSer.get(n).setVisible(state);
            }
        }
        else
        {
            for(int n=0; n<al_LineSer.size(); n++)
            {
                if((al_LineSer.get(n).getTitle().equals("#" + batt_num))
                    || (al_LineSer.get(n).getTitle().equals("Cap: " + batt_num)))
                    al_LineSer.get(n).setVisible(state);
            }
        }
    }
    
    public void clearSerialData()
    {
        this.getSeries().clear();
        al_LineSer.clear();
    }
    
    public void updateChartData(BattTestData data, byte data_type)
    {
        clearSerialData();
        m_LineType = data_type;
        
        int mon_count = data.MonCount;
        int record_count = data.al_TestData.size();
        
        int[] monnum = data.getMonNum(0);
        float[] monvol = data.getMonData(BattTestData.MonData_Vol, 0);
        String value_format = "#0.###";
        for(int n=0; n<mon_count; n++)
        {
            Line line = new Line();
            if(0 == monnum[n])
                line.setTitle("#AVG");
            else line.setTitle("#" + monnum[n]);
            
            if(LineType_MonVol == m_LineType)
            {
                m_LeftAxalValueFormat = "#0.00V";
                value_format = "#0.000V";
            }
            else if(LineType_MonCap == m_LineType)
            {
                m_LeftAxalValueFormat = "#AH";
                value_format = "#AH";
            }
            else if(LineType_MonTmp == m_LineType)
            {
                m_LeftAxalValueFormat = "#0.0¡ãC";
                value_format = "#0.0¡ãC";
            }
            else if(LineType_MonRes == m_LineType)
            {
                m_LeftAxalValueFormat = "#0.00m¦¸";
                value_format = "#0.000m¦¸";
            }
            else if(LineType_MonSer == m_LineType)
            {
                m_LeftAxalValueFormat = "#";
                value_format = "#";
            }
            else if(LineType_MonSer_Percent == m_LineType)
            {
                m_LeftAxalValueFormat = "#%";
                value_format = "#0.0%";;
            }
            
            line.setValueFormat(value_format);
            line.setColor(Line_Color[n%Line_Color.length]);
            line.getPointer().setStyle(PointerStyle.CIRCLE);
            line.getPointer().setVertSize(3);
            line.getPointer().setHorizSize(3);
            line.getPointer().setVisible(true);
            line.addSeriesMouseListener(m_SeriesMouseListener);
            line.getMarks().setStyle(MarksStyle.VALUE);
            line.getMarks().setVisible(m_ShowChartMarks);
            line.getXValues().setDateTime(true);
            al_LineSer.add(line);
        }
        
        long dt_max = new DateTime(1990, 1, 1).getTimeInMillis();
        long dt_min = new DateTime(2990, 12, 12).getTimeInMillis();
        for(int km=0; km<record_count; km++)
        {
            //if((LineType_MonVol == m_LineType) || (LineType_MonCap == m_LineType))
            {
                monnum = data.getMonNum(km);
                monvol = data.getMonData(BattTestData.MonData_Vol, km);
            }
 
            float tmp_value = 0;
            int tmp_mon_index = 0;
            for(int n=0; n<mon_count; n++)
            {
                tmp_mon_index = n;
                //if((LineType_MonVol == m_LineType) || (LineType_MonCap == m_LineType))
                {
                    tmp_value = monvol[n];
                    tmp_mon_index = monnum[n];
                }
                
                int index = 0;
                if(n > 0)
                {
                    for(index=1; index<al_LineSer.size(); index++)
                    {
                        if(al_LineSer.get(index).getTitle().equals("#" + tmp_mon_index))
                            break;
                    }
                    if(index >= al_LineSer.size())
                        break;
                }
                
                long t = data.al_TestData.get(km).startTestTime.getTime();
                if(dt_max < t)
                    dt_max = t;
                if(dt_min > t)
                    dt_min=t;
                
                al_LineSer.get(index).add(new DateTime(t), tmp_value);
            }
        }
        
        maxValue = -90000;
        minValue = 90000;
        for(int n=0; n<mon_count; n++)
        {
            Line line = al_LineSer.get(n);
            this.addSeries(line);
            if(maxValue <= line.getMaxYValue())
                maxValue = line.getMaxYValue();
            if(minValue >= line.getMinYValue())
                minValue = line.getMinYValue();
        }
        if(minValue > 0)
            minValue *= 0.8;
        else minValue *= 1.1;
        
        if(maxValue > 0)
            maxValue *= 1.1;
        else maxValue *= 0.8;
        
        this.getZoom().undo();
        this.getAxes().getLeft().getLabels().setValueFormat(m_LeftAxalValueFormat);
        this.getAxes().getLeft().setMinMax(minValue, maxValue);
        long dt_time = dt_max-dt_min;
        this.getAxes().getBottom().setMinMax(dt_min-dt_time*0.25, dt_max+dt_time*0.5);
    }
    
    private Line newLine(int groupid)
    {
        Line line = new Line();
        line.setValueFormat("#AH  ");
        line.setColor(Line_Color[groupid%Line_Color.length]);
        line.setTitle("Cap: " + groupid);
        
        line.getPointer().setStyle(PointerStyle.CIRCLE);
        line.getPointer().setVertSize(3);
        line.getPointer().setHorizSize(3);
        line.getPointer().setVisible(true);
        line.getXValues().setDateTime(true);
        line.addSeriesMouseListener(m_SeriesMouseListener);
        line.getMarks().setStyle(MarksStyle.VALUE);
        line.getMarks().setVisible(m_ShowChartMarks);
        return line;
    }
    
    public void updateChartData_GroupCap(BattTestData data)
    {
        clearSerialData();
        
        int record_count = data.al_TestData.size();
        
        int[] monnum;
        float[] monvol;
        
        long dt_max = new DateTime(1990, 1, 1).getTimeInMillis();
        long dt_min = new DateTime(2990, 12, 12).getTimeInMillis();
        
        for(int km=0; km<record_count; km++)
        {
            monnum = data.getMonNum(km);
            monvol = data.getMonData(BattTestData.MonData_Vol, km);
            
            float tmp_value = monvol[0];
            int group_id = monnum[0];
            
            int index = 0;
            Line line_t = null;
            for(index=0; index<al_LineSer.size(); index++)
            {
                line_t = al_LineSer.get(index);
                if(line_t.getTitle().equals("Cap: " + group_id))
                    break;
            }
            if(index >= al_LineSer.size())
            {
                line_t = newLine(group_id);
                al_LineSer.add(line_t);
            }
            
            long t = data.al_TestData.get(km).startTestTime.getTime();
            if(dt_max < t)
                dt_max = t;
            if(dt_min > t)
                dt_min=t;
            
            line_t.add(new DateTime(t), tmp_value);
        }
        
        maxValue = -90000;
        minValue = 90000;
        
        for(int n=0; n<al_LineSer.size(); n++)
        {
            Line line = al_LineSer.get(n);
            this.addSeries(line);
            if(maxValue <= line.getMaxYValue())
                maxValue = line.getMaxYValue();
            if(minValue >= line.getMinYValue())
                minValue = line.getMinYValue();
        }
        if(minValue > 0)
            minValue *= 0.8;
        else minValue *= 1.1;
        
        if(maxValue > 0)
            maxValue *= 1.1;
        else maxValue *= 0.8;
        
        this.getZoom().undo();
        this.getAxes().getLeft().getLabels().setValueFormat("#");
        this.getAxes().getLeft().setMinMax(minValue, maxValue);
        long dt_time = dt_max-dt_min;
        this.getAxes().getBottom().setMinMax(dt_min-dt_time*0.25, dt_max+dt_time*0.5);
    }
}
/************************************************************************************************/