/* * AGPBarGraph * * Version 1.1 - Java 1.1 compatible * * Dec 6 2001 * * Copyright 2001 David Joiner and the Shodor Education Foundation */ import java.awt.*; /** * AGPBarGraph is a bar graph designed to be used with AnimatedGraphPaper */ public class AGPBarGraph extends BufferCanvas { /* * Member variables */ protected double graphRealMin = -10.0; protected double graphRealMax = 10.0; int topBorderWTitle=30; //with title int topBorderWOTitle=10; //without title int topBorder=topBorderWOTitle; int bottomBorderWLabel=50; int bottomBorderWOLabel=30; int bottomBorder=bottomBorderWOLabel; int leftBorderWLabel=75; int leftBorderWOLabel=50; int leftBorder=leftBorderWOLabel; int rightBorder=10; protected String title; protected String unit; protected int nBar; protected double [] barHeight; protected Color [] barColor; protected String [] barLabel; public AGPBarGraph () { super(); } /* * Get and Set methods */ public void setTitle(String title) { if (title == null) { this.title = null; topBorder = topBorderWOTitle; } else { this.title = new String(title); topBorder = topBorderWTitle; } } public String getTitle() { if (title == null) { return null; } else { return new String(title); } } public void setUnit(String unit) { if (unit == null) { this.unit = null; leftBorder = leftBorderWOLabel; } else { this.unit = new String(unit); leftBorder = leftBorderWLabel; } } public String getUnit() { return unit; } public void setGraphMax(double graphMax) { graphRealMax = graphMax; } public double getGraphMax() { return graphRealMax; } public void setGraphMin(double graphMin) { graphRealMin = graphMin; } public double getGraphMin() { return graphRealMin; } public void setBars(int n,double [] heights, Color [] colors, String [] labels) { nBar = n; barHeight = new double[n]; barColor = new Color[n]; barLabel = new String[n]; boolean allNull = true; for(int i=0; i graphRealMin && 0.0 < graphRealMax) { g.drawLine(leftBorder,zeroPos, width-rightBorder,zeroPos); } // draw labels for max, min, and if not blocking something else, zero String maxString = new String(""+graphRealMax); String minString = new String(""+graphRealMin); String zeroString = new String(""+0.0); double maxLength = (double)maxString.length(); int maxStringPos = leftBorder - (int)(maxLength*pixelsPerLetter) - (int)pixelsPerLetter; //int maxStringHeight = topBorder + (int)pixelsPerLetter; int maxStringHeight = realToDisplay(graphRealMax); g.drawString(maxString,maxStringPos,maxStringHeight); double minLength = (double)minString.length(); int minStringPos = leftBorder - (int)(minLength*pixelsPerLetter) - (int)pixelsPerLetter; //int minStringHeight = height - bottomBorder; int minStringHeight = realToDisplay(graphRealMin); g.drawString(minString,minStringPos,minStringHeight); int unitHeight = height/2+(int)pixelsPerLetter; if (Math.abs(zeroPos-maxStringPos) > (int)pixelsPerLetter*2 && Math.abs(zeroPos-unitHeight) > (int)pixelsPerLetter*2 && Math.abs(zeroPos-minStringPos) > (int)pixelsPerLetter*2 && 0.0 > graphRealMin && 0.0 < graphRealMax) { double zeroLength = (double)zeroString.length(); int zeroStringPos = leftBorder - (int)(zeroLength*pixelsPerLetter) - (int)pixelsPerLetter; g.drawString(zeroString,zeroStringPos,zeroPos); } // draw title if (title != null) { double titleLength = (double)title.length(); int titlePos = width/2- (int)(titleLength*pixelsPerLetter/2.0); int titleHeight = topBorder/2+(int)pixelsPerLetter; g.drawString(title,titlePos,titleHeight); } // draw units if (unit != null) { double unitLength = (double)unit.length(); int unitPos = leftBorder/2- (int)(unitLength*pixelsPerLetter/2.0); g.drawString(unit,unitPos,unitHeight); } // draw graph region, let bleed over edge a little if (nBar>0) { Color lastColor = g.getColor(); int spacing = (int)((double)graphWidth/(double)(2*nBar+1)); int position = spacing+leftBorder; for(int i = 0; i height - bottomBorder + 2) displayPos = height - bottomBorder + 2; if (displayPos < topBorder - 2) displayPos = topBorder -2; int displayBarBottom = realToDisplay(0.0); if (displayBarBottom > height - bottomBorder + 2) displayBarBottom = height - bottomBorder + 2; if (displayBarBottom < topBorder - 2) displayBarBottom = topBorder - 2; int displayBarHeight = displayBarBottom - displayPos; if (displayBarHeight<0) { displayPos = displayPos + displayBarHeight; displayBarHeight = -displayBarHeight; } g.setColor(barColor[i]); g.fillRect(position,displayPos, spacing,displayBarHeight); if (barLabel[i] != null) { g.setColor(Color.black); g.drawString(barLabel[i],position, height-bottomBorder/2+(int)pixelsPerLetter); g.setColor(barColor[i]); } position+=2*spacing; } g.setColor(lastColor); } repaint(); } public void thickLine(Graphics g, int x1,int y1,int x2,int y2) { g.drawLine(x1,y1,x2,y2); g.drawLine(x1+1,y1,x2+1,y2); g.drawLine(x1,y1+1,x2,y2+1); g.drawLine(x1-1,y1,x2-1,y2); g.drawLine(x1,y1-1,x2,y2-1); } }