1 import java.applet.Applet ; 2 import java.awt.BorderLayout ; 3 import java.awt.Button ; 4 import java.awt.Frame ; 5 import java.awt.Panel ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.awt.event.WindowAdapter ; 9 import java.awt.event.WindowEvent ; 10 import java.util.Properties ; 11 12 import jcckit.GraphicsPlotCanvas; 13 import jcckit.data.DataCurve; 14 import jcckit.data.DataPlot; 15 import jcckit.data.DataPoint; 16 import jcckit.util.ConfigParameters; 17 import jcckit.util.PropertiesBasedConfigData; 18 19 20 25 public class AnimatedChart extends Applet { 26 private double[] _data = new double[] 27 {55.5, 34.2, 47.4, 53.1, 69.9, 68.7, 81.1}; 28 private DataPlot _dataPlot; 29 30 public void init() { 31 GraphicsPlotCanvas plotCanvas = createPlotCanvas(); 32 33 _dataPlot = new DataPlot(); 34 _dataPlot.addElement(new DataCurve("")); 35 plotCanvas.connect(_dataPlot); 36 37 setLayout(new BorderLayout ()); 38 add(plotCanvas.getGraphicsCanvas(), BorderLayout.CENTER); 39 add(createControlPanel(), BorderLayout.SOUTH); 40 } 41 42 private GraphicsPlotCanvas createPlotCanvas() { 43 Properties props = new Properties (); 44 ConfigParameters config 45 = new ConfigParameters(new PropertiesBasedConfigData(props)); 46 props.put("plot/legendVisible", "false"); 47 props.put("plot/coordinateSystem/xAxis/minimum", "-0.5"); 48 props.put("plot/coordinateSystem/xAxis/maximum", "6.5"); 49 props.put("plot/coordinateSystem/xAxis/axisLabel", ""); 50 props.put("plot/coordinateSystem/xAxis/ticLabelFormat/className", 51 "jcckit.plot.TicLabelMap"); 52 props.put("plot/coordinateSystem/xAxis/ticLabelFormat/map", 53 "0=Mo;1=Tu;2=We;3=Th;4=Fr;5=Sa;6=Su"); 54 props.put("plot/coordinateSystem/yAxis/axisLabel", "growth rate"); 55 props.put("plot/coordinateSystem/yAxis/maximum", "100"); 56 props.put("plot/coordinateSystem/yAxis/ticLabelFormat", "%d%%"); 57 props.put("plot/curveFactory/definitions", "curve"); 58 props.put("plot/curveFactory/curve/withLine", "false"); 59 props.put("plot/curveFactory/curve/symbolFactory/className", 60 "jcckit.plot.BarFactory"); 61 props.put("plot/curveFactory/curve/symbolFactory/attributes/className", 62 "jcckit.graphic.ShapeAttributes"); 63 props.put("plot/curveFactory/curve/symbolFactory/attributes/fillColor", 64 "0xfe8000"); 65 props.put("plot/curveFactory/curve/symbolFactory/attributes/lineColor", 66 "0"); 67 props.put("plot/curveFactory/curve/symbolFactory/size", "0.08"); 68 props.put("plot/initialHintForNextCurve/className", 69 "jcckit.plot.PositionHint"); 70 props.put("plot/initialHintForNextCurve/position", "0 0.1"); 71 72 return new GraphicsPlotCanvas(config); 73 } 74 75 private Panel createControlPanel() { 76 Panel controlPanel = new Panel (); 77 Button startButton = new Button ("animate"); 78 startButton.addActionListener(new ActionListener () { 79 public void actionPerformed(ActionEvent e) { 80 new Thread () { 81 public void run() { 82 animate(); 83 } 84 }.start(); 85 } 86 }); 87 controlPanel.add(startButton); 88 89 return controlPanel; 90 } 91 92 private void animate() { 93 DataCurve curve = new DataCurve(""); 94 for (int i = 0; i < _data.length; i++) { 95 curve.addElement(new DataPoint(i, 0)); 96 } 97 _dataPlot.replaceElementAt(0, curve); 98 99 for (int i = 0; i < _data.length; i++) { 100 double x = i; 101 double y = 0; 102 while (y < _data[i]) { 103 try { 104 Thread.sleep(50); 105 } catch (InterruptedException e) {} 106 y = Math.min(_data[i], y + 5); 107 curve.replaceElementAt(i, new DataPoint(x, y)); 108 } 109 } 110 } 111 112 113 public static void main(String [] args) { 114 Frame frame = new Frame ("Animated Chart"); 115 frame.addWindowListener(new WindowAdapter () { 116 public void windowClosing(WindowEvent event) { 117 System.exit(0); 118 } 119 }); 120 Applet applet = new AnimatedChart(); 121 applet.init(); 122 frame.add(applet); 123 frame.setSize(600, 500); 124 frame.show(); 125 } 126 } 127 | Popular Tags |