1 |
//---PACKAGE DECLARATION--- |
2 |
|
3 |
//---IMPORTS--- |
4 |
|
5 |
import java.util.*; |
6 |
|
7 |
/** |
8 |
* Class for storing and drawing 2-dimensional plot data. |
9 |
* This class is used to provide an object to store 2-dimensional plot |
10 |
* data. Pairs of values may be added to this object and the maximum |
11 |
* and minimum values for each axis are also known internally. This |
12 |
* information is used to produce a scaled graph, making the most of |
13 |
* the available viewing area. |
14 |
* |
15 |
* @author $Author: pjm2 $ |
16 |
* @version $Id: PlotData.java,v 1.2 2001/02/03 17:26:07 pjm2 Exp $ |
17 |
*/ |
18 |
public class PlotData { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: 1.2 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
public PlotData(long minX, long maxX) { |
32 |
_minX = (double)minX; |
33 |
_maxX = (double)maxX; |
34 |
} |
35 |
|
36 |
|
37 |
//---PUBLIC METHODS--- |
38 |
|
39 |
public double getMaxY() { |
40 |
return _maxY; |
41 |
} |
42 |
|
43 |
public Iterator iterator() { |
44 |
return _data.iterator(); |
45 |
} |
46 |
|
47 |
public long countPoints() { |
48 |
return _data.size(); |
49 |
} |
50 |
|
51 |
public void addPoint(long x, long y) { |
52 |
addPoint((double)x, (double)y); |
53 |
} |
54 |
|
55 |
public void addPoint(long x, double y) { |
56 |
addPoint((double)x, y); |
57 |
} |
58 |
|
59 |
// Add a point to the plot data. |
60 |
public void addPoint(double x, double y){ |
61 |
|
62 |
int newX = (int)((x - _minX) / (_maxX - _minX) * (double)SHTMLLayout.CHART_WIDTH); |
63 |
if (newX == _lastX) { |
64 |
_cumulativeValue += y; |
65 |
_cumulativeCount++; |
66 |
_lastX = newX; |
67 |
return; |
68 |
} |
69 |
else { |
70 |
if (_cumulativeCount == 0) { |
71 |
_cumulativeValue = y; |
72 |
_cumulativeCount = 1; |
73 |
_lastX = newX; |
74 |
return; |
75 |
} |
76 |
} |
77 |
|
78 |
double yTemp = y; |
79 |
y = _cumulativeValue / _cumulativeCount; |
80 |
_cumulativeCount = 1; |
81 |
_cumulativeValue = yTemp; |
82 |
|
83 |
// Add a new point to the LinkedList. |
84 |
PairData p = new PairData(_lastDoubleX, y); |
85 |
_data.add(p); |
86 |
|
87 |
if (_data.size() == 1) { |
88 |
_maxY = y; |
89 |
} |
90 |
|
91 |
if (y > _maxY) { |
92 |
_maxY = y; |
93 |
} |
94 |
|
95 |
_lastDoubleX = x; |
96 |
_lastX = newX; |
97 |
} |
98 |
|
99 |
// Add the last point. |
100 |
public void flushPoints() { |
101 |
if (_cumulativeCount > 0) { |
102 |
addPoint(2* _maxX, 0); |
103 |
} |
104 |
} |
105 |
|
106 |
|
107 |
// Move each point so that it fits in the view perfectly. |
108 |
public void scaleToFitX(int width, int height){ |
109 |
Iterator it = _data.iterator(); |
110 |
while (it.hasNext()){ |
111 |
PairData p = (PairData)it.next(); |
112 |
double x = p.getX(); |
113 |
double y = p.getY(); |
114 |
x = (x - _minX) / (_maxX - _minX) * (double)width; |
115 |
p.setPair(x, y); |
116 |
} |
117 |
} |
118 |
|
119 |
/** |
120 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
121 |
* method to provide clean logging (every class should have this). |
122 |
* |
123 |
* @return the name of this class and its CVS revision |
124 |
*/ |
125 |
public String toString() { |
126 |
return this.getClass().getName() + "(" + REVISION.substring(11, REVISION.length() - 2) + ")"; |
127 |
} |
128 |
|
129 |
//---PRIVATE METHODS--- |
130 |
|
131 |
//---ACCESSOR/MUTATOR METHODS--- |
132 |
|
133 |
//---ATTRIBUTES--- |
134 |
|
135 |
private LinkedList _data = new LinkedList(); |
136 |
private double _maxY; |
137 |
private double _minY = 0; |
138 |
private double _minX; |
139 |
private double _maxX; |
140 |
|
141 |
private int _lastX = -1; |
142 |
private int _cumulativeCount = 0; |
143 |
private double _cumulativeValue = 0; |
144 |
private double _lastDoubleX = 0; |
145 |
|
146 |
//---STATIC ATTRIBUTES--- |
147 |
|
148 |
} |