|
2.1 Example: Animated ChartThe applet AnimatedChart (see the Examples page) presents a chart in an animated way. By clicking the button "animate" all bars are deleted first (more precisely their height is set to zero). Now each bar grows with a constant speed up to its value.
The basic structure of the applet reads (click
here to see the complete code):
The init() method (which is always invoked when the applet is started) does the following:
Creating a GraphicsPlotCanvas is mainly a configuration task concerning
private GraphicsPlotCanvas createPlotCanvas() { Properties props = new Properties(); ConfigParameters config = new ConfigParameters(new PropertiesBasedConfigData(props)); props.put("plot/legendVisible", "false"); props.put("plot/coordinateSystem/xAxis/minimum", "-0.5"); props.put("plot/coordinateSystem/xAxis/maximum", "6.5"); props.put("plot/coordinateSystem/xAxis/axisLabel", ""); props.put("plot/coordinateSystem/xAxis/ticLabelFormat/className", "jcckit.plot.TicLabelMap"); props.put("plot/coordinateSystem/xAxis/ticLabelFormat/map", "0=Mo;1=Tu;2=We;3=Th;4=Fr;5=Sa;6=Su"); props.put("plot/coordinateSystem/yAxis/axisLabel", "growth rate"); props.put("plot/coordinateSystem/yAxis/maximum", "100"); props.put("plot/coordinateSystem/yAxis/ticLabelFormat", "%d%%"); props.put("plot/curveFactory/definitions", "curve"); props.put("plot/curveFactory/curve/withLine", "false"); props.put("plot/curveFactory/curve/symbolFactory/className", "jcckit.plot.BarFactory"); props.put("plot/curveFactory/curve/symbolFactory/attributes/className", "jcckit.graphic.ShapeAttributes"); props.put("plot/curveFactory/curve/symbolFactory/attributes/fillColor", "0xfe8000"); props.put("plot/curveFactory/curve/symbolFactory/attributes/lineColor", "0"); props.put("plot/curveFactory/curve/symbolFactory/size", "0.08"); props.put("plot/initialHintForNextCurve/className", "jcckit.plot.PositionHint"); props.put("plot/initialHintForNextCurve/position", "0 0.1"); return new GraphicsPlotCanvas(config); }Here is not the place to explain all parameters. Please, consult the API documentation especially the constructors of PlotCanvas, Plot, CartesianCoordinateSystem, and SimpleCurveFactory. See also Chapter 4.
The animation is started after the animate button has been hit. This is done
in an extra Thread which is created inside the ActionListener
registrated at the button:
In a first step the heights of all bars are set to zero. This is done by creating a new DataCurve with DataPoints where the y-coordinate is zero. The new curve replaces the current one. Note, that no explicit update of the view is necessary. This is done automatically for all Plot instances connected with the DataPlot instance where such data operations like curve replacements take place. Now the animation starts: For each bar the y-value is increased by 5 until its final value is reached. Between each increase the animation is paused by 50 milliseconds. After an increase the corresponding curve point is replaced by a new instance. Note, that DataPoint is an immutable class. Thus, we can not change coordinates of a data point directly. Again, the view will be updated automatically. |
(C) 2003-2004 Franz-Josef Elmer. All rights reserved. Last modified: 1/18/2004 |