KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > StackedBarGraph


1 package JSci.awt;
2
3 import java.awt.*;
4
5 /**
6 * A stacked bar graph AWT component.
7 * Multiple series are stacked.
8 * @version 1.2
9 * @author Lindsay Laird
10 */

11 public class StackedBarGraph extends BarGraph {
12         /**
13         * Constructs a stacked bar graph.
14         */

15         public StackedBarGraph(CategoryGraph2DModel gm) {
16                 super(gm);
17         }
18         /**
19         * Implementation of GraphDataListener.
20         * Application code will not use this method explicitly, it is used internally.
21         */

22         public void dataChanged(GraphDataEvent e) {
23                 float tmp;
24                 minY=0.0f;
25                 maxY=Float.NEGATIVE_INFINITY;
26                 model.firstSeries();
27                 for(int i=0;i<model.seriesLength();i++) {
28                         tmp=model.getValue(i);
29                         while(model.nextSeries())
30                                 tmp+=model.getValue(i);
31                         minY=Math.min(tmp,minY);
32                         maxY=Math.max(tmp,maxY);
33                         model.firstSeries();
34                 }
35                 if(minY==maxY) {
36                         minY-=0.5f;
37                         maxY+=0.5f;
38                 }
39                 setNumbering(numbering);
40         }
41         /**
42         * Draws the graph bars.
43         */

44         protected void drawBars(Graphics g) {
45 // bars
46
int dy,totalDy;
47                 for(int i=0;i<model.seriesLength();i++) {
48                         model.firstSeries();
49                         dy = drawStackedBar(g, i, model.getValue(i), barColor[0], 0);
50                         totalDy=dy;
51                         for(int n=1;model.nextSeries();n++) {
52                                 dy = drawStackedBar(g, i, model.getValue(i), barColor[n], -totalDy);
53                                 totalDy+=dy;
54                         }
55                 }
56         }
57         /**
58         * Draws a bar.
59         * @return the height of the bar.
60         */

61         private int drawStackedBar(Graphics g, int pos, float value, Color color, int yoffset) {
62                 Point p;
63                 if(value<0.0f)
64                         p=dataToScreen(pos,0.0f);
65                 else
66                         p=dataToScreen(pos,value);
67                 g.setColor(color);
68                 final int dy=Math.abs(p.y-origin.y);
69                 g.fillRect(p.x+barPad, p.y+yoffset, barWidth, dy);
70                 g.setColor(Color.black);
71                 g.drawRect(p.x+barPad, p.y+yoffset, barWidth, dy);
72                 return dy;
73         }
74 }
75
76
Popular Tags