1 package JSci.awt; 2 3 import java.awt.*; 4 5 11 public class StackedBarGraph extends BarGraph { 12 15 public StackedBarGraph(CategoryGraph2DModel gm) { 16 super(gm); 17 } 18 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 44 protected void drawBars(Graphics g) { 45 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 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 |