1 19 package jcckit.plot; 20 21 import java.util.Vector ; 22 23 import jcckit.data.DataCurve; 24 import jcckit.data.DataEvent; 25 import jcckit.data.DataListener; 26 import jcckit.data.DataPlot; 27 import jcckit.data.DataPoint; 28 import jcckit.graphic.ClippingShape; 29 import jcckit.graphic.GraphPoint; 30 import jcckit.graphic.GraphicalComposite; 31 import jcckit.graphic.GraphicalElement; 32 import jcckit.transformation.Transformation; 33 import jcckit.util.ConfigParameters; 34 import jcckit.util.Factory; 35 36 54 public class Plot implements DataListener { 55 56 public static final String COORDINATE_SYSTEM_KEY = "coordinateSystem", 57 CURVE_FACTORY_KEY = "curveFactory", 58 LEGEND_VISIBLE_KEY = "legendVisible", 59 LEGEND_KEY = "legend", 60 INITIAL_HINT_FOR_NEXT_CURVE_KEY 61 = "initialHintForNextCurve"; 62 private final Vector _plotListeners = new Vector (); 63 private DataPlot _dataPlot; 64 private final CurveFactory _curveFactory; 65 private final Vector _curves = new Vector (); 66 private final Vector _nextCurveHints = new Vector (); 67 private final Hint _initialHintForNextCurve; 68 private final Legend _legend; 69 private final boolean _legendVisibility; 70 71 private GraphicalElement _coordinateSystemView; 72 private ClippingShape _clippingShape; 73 private Transformation _transformation; 74 private GraphicalElement _annotation; 75 private GraphicalComposite _legendView = new GraphicalComposite(null); 76 77 101 public Plot(ConfigParameters config) { 102 CoordinateSystem coordinateSystem = (CoordinateSystem) Factory.create( 103 config.getNode(COORDINATE_SYSTEM_KEY), 104 CartesianCoordinateSystem.class.getName()); 105 setCoordinateSystem(coordinateSystem); 106 _curveFactory = (CurveFactory) Factory.create( 107 config.getNode(CURVE_FACTORY_KEY), 108 SimpleCurveFactory.class.getName()); 109 _initialHintForNextCurve = (Hint) Factory.createOrGet( 110 config.getNode(INITIAL_HINT_FOR_NEXT_CURVE_KEY), null); 111 _legend = new Legend(config.getNode(LEGEND_KEY)); 112 _legendVisibility = config.getBoolean(LEGEND_VISIBLE_KEY, true); 113 } 114 115 122 public void setCoordinateSystem(CoordinateSystem coordinateSystem) 123 { 124 _coordinateSystemView = coordinateSystem.getView(); 125 _clippingShape = coordinateSystem.getClippingShape(); 126 _transformation = coordinateSystem.getTransformation(); 127 if (_dataPlot != null) 128 { 129 generateCurves(_dataPlot); 130 } 131 notifyListeners( 132 new PlotEvent(this, PlotEventType.COODINATE_SYSTEM_CHANGED, null)); 133 } 134 135 139 public void addPlotListener(PlotListener listener) { 140 if (!_plotListeners.contains(listener)) { 141 _plotListeners.addElement(listener); 142 } 143 } 144 145 149 public void removePlotListener(PlotListener listener) { 150 _plotListeners.removeElement(listener); 151 } 152 153 157 protected void notifyListeners(PlotEvent event) { 158 for (int i = 0, n = _plotListeners.size(); i < n; i++) { 159 ((PlotListener) _plotListeners.elementAt(i)).plotChanged(event); 160 } 161 } 162 163 180 public void connect(DataPlot dataPlot) { 181 if (_dataPlot != null) { 182 _dataPlot.removeDataListener(this); 183 notifyListeners(new PlotEvent(this, PlotEventType.DATA_PLOT_DISCONNECTED, 184 _dataPlot)); 185 } 186 _dataPlot = dataPlot; 187 if (_dataPlot != null) 188 { 189 _dataPlot.addDataListener(this); 190 generateCurves(_dataPlot); 191 notifyListeners(new PlotEvent(this, PlotEventType.DATA_PLOT_CONNECTED, 192 _dataPlot)); 193 } 194 } 195 196 202 public DataPoint transform(GraphPoint point) { 203 return _transformation.transformToData(point); 204 } 205 206 211 public GraphicalComposite getCompletePlot() { 212 GraphicalComposite result = new GraphicalComposite(null); 213 result.addElement(_coordinateSystemView); 214 GraphicalElement[] curves = getCurves(); 215 for (int i = 0; i < curves.length; i++) { 216 result.addElement(curves[i]); 217 } 218 if (_annotation != null) { 219 result.addElement(_annotation); 220 } 221 if (_legendVisibility) { 222 result.addElement(getLegend()); 223 } 224 return result; 225 } 226 227 228 public GraphicalElement getCoordinateSystem() { 229 return _coordinateSystemView; 230 } 231 232 233 public GraphicalElement[] getCurves() { 234 synchronized (_curves) { 235 GraphicalElement[] curves = new GraphicalElement[_curves.size()]; 236 for (int i = 0; i < curves.length; i++) { 237 curves[i] = ((Curve) _curves.elementAt(i)).getView(); 238 } 239 return curves; 240 } 241 } 242 243 247 public GraphicalElement getAnnotation() 248 { 249 return _annotation; 250 } 251 252 258 public void setAnnotation(GraphicalElement annotation) 259 { 260 _annotation = annotation; 261 } 262 263 264 public boolean isLegendVisible() { 265 return _legendVisibility; 266 } 267 268 269 public GraphicalElement getLegend() { 270 return _legendView; 271 } 272 273 293 public void dataChanged(DataEvent event) { 294 Integer index = new Integer (0); 295 PlotEventType type = PlotEventType.DATA_PLOT_CHANGED; 296 synchronized (_curves) { 297 int numberOfCurves = _curves.size(); 298 if (event.getContainer() instanceof DataCurve 299 && numberOfCurves == _dataPlot.getNumberOfElements()) { 300 DataCurve curve = (DataCurve) event.getContainer(); 301 index = new Integer (curve.getContainer().getIndexOf(curve)); 302 type = PlotEventType.DATA_CURVE_CHANGED; 303 fillCurve(index.intValue(), curve); 304 if (index.intValue() < numberOfCurves - 1) { 305 Vector curveHints 306 = (Vector ) _nextCurveHints.elementAt(index.intValue()); 307 for (int i = 0, n = curveHints.size(); i < n; i++) { 308 if (curveHints.elementAt(i) != null) { 309 type = PlotEventType.DATA_PLOT_CHANGED; 310 for (int j = index.intValue()+1; j < numberOfCurves; j++) { 311 fillCurve(j, (DataCurve) _dataPlot.getElement(j)); 312 } 313 break; 314 } 315 } 316 } 317 } else { 318 generateCurves(_dataPlot); 319 } 320 } 321 notifyListeners(new PlotEvent(Plot.this, type, index)); 322 } 323 324 328 private void generateCurves(DataPlot dataPlot) { 329 synchronized (_curves) { 330 _legendView = new GraphicalComposite(null); 331 _legendView.addElement(_legend.getBox()); 332 _curves.setSize(0); 333 _nextCurveHints.setSize(0); 334 for (int i = 0, n = dataPlot.getNumberOfElements(); i < n; i++) { 335 Curve curve = _curveFactory.create(i, n, _clippingShape, _legend); 336 _curves.addElement(curve); 337 _nextCurveHints.addElement(new Vector ()); 338 DataCurve dataCurve = (DataCurve) dataPlot.getElement(i); 339 _legendView.addElement(curve.getLegendSymbol()); 340 _legendView.addElement( 341 _legend.createCurveTitle(i, n, dataCurve.getTitle())); 342 fillCurve(i, dataCurve); 343 } 344 } 345 } 346 347 private void fillCurve(int curveIndex, DataCurve dataCurve) { 348 Vector curveHints = (Vector ) _nextCurveHints.elementAt(curveIndex); 349 Curve curve = (Curve) _curves.elementAt(curveIndex); 350 curve.removeAllPoints(); 351 for (int i = 0, n = dataCurve.getNumberOfElements(); i < n; i++) { 352 setHintForNextCurve(curveHints, i, 353 curve.addPoint(_transformation.transformToGraph( 354 (DataPoint) dataCurve.getElement(i)), 355 getHintForNextCurve(curveIndex - 1, i))); 356 } 357 } 358 359 private Hint getHintForNextCurve(int curveIndex, int pointIndex) { 360 Hint result = _initialHintForNextCurve; 361 if (curveIndex >= 0) { 362 Vector curveHints = (Vector ) _nextCurveHints.elementAt(curveIndex); 363 result = pointIndex < curveHints.size() ? 364 (Hint) curveHints.elementAt(pointIndex) 365 : getHintForNextCurve(curveIndex - 1, pointIndex); 366 } 367 return result; 368 } 369 370 private void setHintForNextCurve(Vector curveHints, int pointIndex, 371 Hint hint) { 372 while (curveHints.size() <= pointIndex) { 373 curveHints.addElement(_initialHintForNextCurve); 374 } 375 curveHints.setElementAt(hint, pointIndex); 376 } 377 } 378 | Popular Tags |