KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JLayeredBarGraph


1 package JSci.swing;
2
3 import java.awt.*;
4 import JSci.awt.*;
5
6 /**
7 * A layered bar graph Swing component.
8 * Multiple series are layered.
9 * @version 1.2
10 * @author Mark Hale
11 */

12 public class JLayeredBarGraph extends JBarGraph {
13         /**
14         * Constructs a layered bar graph.
15         */

16         public JLayeredBarGraph(CategoryGraph2DModel gm) {
17                 super(gm);
18         }
19         /**
20         * Draws the graph bars.
21         */

22         protected void drawBars(Graphics g) {
23 // bars
24
int numSeries=1;
25                 model.firstSeries();
26                 while(model.nextSeries())
27                         numSeries++;
28                 if(numSeries==1) {
29                         for(int i=0;i<model.seriesLength();i++) {
30                                 drawBar(g, i, model.getValue(i), barColor[0]);
31                         }
32                 } else {
33                         float seriesValue[]=new float[numSeries];
34                         Color seriesColor[]=new Color[numSeries];
35                         for(int i=0;i<model.seriesLength();i++) {
36                                 model.firstSeries();
37                                 for(int j=0;j<numSeries;j++) {
38                                         seriesValue[j]=model.getValue(i);
39                                         seriesColor[j]=barColor[j];
40                                         model.nextSeries();
41                                 }
42                                 // sort
43
float val;
44                                 Color col;
45                                 for(int k,j=1;j<numSeries;j++) {
46                                         val=seriesValue[j];
47                                         col=seriesColor[j];
48                                         for(k=j-1;k>=0 && seriesValue[k]<val;k--) {
49                                                 seriesValue[k+1]=seriesValue[k];
50                                                 seriesColor[k+1]=seriesColor[k];
51                                         }
52                                         seriesValue[k+1]=val;
53                                         seriesColor[k+1]=col;
54                                 }
55                                 // draw
56
for(int j=0;j<numSeries;j++)
57                                         drawBar(g, i, seriesValue[j], seriesColor[j]);
58                         }
59                 }
60         }
61         /**
62         * Draws a bar.
63         */

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