comparison src/org/dancres/blitz/tools/dash/ChartPanel.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.tools.dash;
2
3 import java.awt.BorderLayout;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Map;
7 import javax.swing.JPanel;
8
9 import org.dancres.blitz.tools.dash.graph.*;
10
11 public class ChartPanel extends JPanel {
12 private Chart _chart = new Chart();
13 private Map _lookup = new HashMap();
14 private long[] _lastValues;
15 private final int MAX_SIZE = 100;
16 public ChartPanel() {
17 setLayout(new BorderLayout());
18 add(_chart, BorderLayout.CENTER);
19 }
20 public void update(String[] names, long[] newValues) {
21 try {
22 if (_lastValues == null) {
23 doUpdate(names, newValues);
24 }
25 for (int i = 0; i < newValues.length; i++) {
26 if (newValues[i] != _lastValues[i]) {
27 doUpdate(names, newValues);
28 return;
29 }
30 }
31 } catch (Exception ex) {
32 ex.printStackTrace();
33 }
34 }
35 private void doUpdate(String[] names, long[] newValues) throws Exception {
36 _lastValues = newValues;
37 for (int i = 0; i < newValues.length; i++) {
38 ArrayList history = (ArrayList) _lookup.get(names[i]);
39 if (history == null) {
40 if (newValues.length == 0 && newValues[i] == 0) {
41 return;
42 }
43 if (newValues.length > 0) {
44 //if they are all zero ommit
45 boolean ok = false;
46 for (int j = 0; !ok && j < newValues.length; j++) {
47 if (newValues[j] > 0) {
48 ok = true;
49 }
50 }
51 if (!ok) {
52 return;
53 }
54 }
55
56 history = new ArrayList();
57 _lookup.put(names[i], history);
58 //add twice the first time to get a line
59 history.add(new Long(newValues[i]));
60 //first time so add the data
61 long startVal=0;//newValues[i];// - 1;
62 //if(startVal<0){
63 //startVal=0;
64 //}
65 addData(names[i], new double[]{startVal, newValues[i]});
66 } else {
67
68 history.add(new Long(newValues[i]));
69 int n = history.size();
70 if (n > MAX_SIZE) {
71 n--;
72 history.remove(0);
73
74 }
75
76 double[] data = new double[n];
77 String[] labels = new String[n];
78 for (int j = 0; j < n; j++) {
79 labels[j] = "";
80 data[j] = ((Long) history.get(j)).longValue();
81
82 }
83 labels[0] = "Last update: " + new java.util.Date();
84 _chart.setDataAt(i, names[i], data, labels, null);
85 }
86 }
87 _chart.repaint();
88 }
89 private void addData(String name, double[] data) throws Exception {
90 String[] labs = new String[data.length];
91 labs[0] = "Last update: " + new java.util.Date();
92 for (int i = 1; i < labs.length; i++) {
93 labs[i] = "";
94 }
95 _chart.addData(name, data, labs);
96 }
97 }