KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > LayeredBarGraph


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

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

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

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

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