1 7 package org.ejtools.graph.renderer; 8 9 import java.awt.Color ; 10 import java.awt.FontMetrics ; 11 import java.awt.Graphics ; 12 import java.awt.Insets ; 13 import java.awt.geom.Rectangle2D ; 14 import java.text.Format ; 15 16 import org.ejtools.graph.Axis; 17 import org.ejtools.graph.GraphRenderer; 18 19 24 public class HorizontalAxis extends Axis 25 { 26 32 public HorizontalAxis(Format format, int position) 33 { 34 this.format = format; 35 this.orientation = HORIZONTAL; 36 this.position = position; 37 } 38 39 40 45 public void paintComponent(Graphics graphics) 46 { 47 String display; 48 double multiplier = 0.0d; 49 50 graphics.setFont(FONT); 51 FontMetrics metrics = graphics.getFontMetrics(); 52 53 Insets insets = this.getInsets(); 54 int x = insets.left; 55 double width = (double) this.getWidth() - 1 - insets.left - insets.right; 57 double height = (double) this.getHeight() - 1 - insets.top - insets.bottom; 58 59 double min = element.getXRange().getMin(); 60 double max = element.getXRange().getMax(); 61 62 double scaleX = width / (max - min); 63 double offsetX = x - scaleX * min; 64 65 if (this.horizontalScaling == GraphRenderer.ALIGN_LEFT) 66 { 67 scaleX = 1.0d; 68 offsetX = -min; 69 } 70 if (this.horizontalScaling == GraphRenderer.ALIGN_RIGHT) 71 { 72 scaleX = 1.0d * this.horizontalScale; 73 offsetX = width - max * this.horizontalScale; 74 } 75 76 min = (x - offsetX) / scaleX; 77 78 String minText = this.getFormated(min); 79 Rectangle2D minBounds = metrics.getStringBounds(minText, graphics); 80 String maxText = this.getFormated(max); 81 Rectangle2D maxBounds = metrics.getStringBounds(maxText, graphics); 82 83 double bound = Math.max(minBounds.getWidth() * 4.0d, maxBounds.getWidth() * 4.0d); 85 86 int ticks = 1; 88 while ((width / ticks) > bound) 89 { 90 ticks = ticks * 2; 91 } 92 93 graphics.drawLine(0, 0, (int) width, 0); 95 96 multiplier = width / ticks / 2; 98 graphics.setColor(Color.black); 99 for (int i = 0; i <= (2 * ticks); i++) 100 { 101 int j = (int) (i * multiplier); 102 graphics.drawLine(j, 0, j, FONT.getSize() / 2); 103 } 104 105 multiplier = width / ticks; 107 108 graphics.drawLine(0, 0, 0, FONT.getSize() - metrics.getDescent()); 110 graphics.drawString(minText, 0, (int) (height - metrics.getDescent())); 111 112 for (int i = 1; i < ticks; i++) 114 { 115 int j = (int) (i * multiplier); 116 graphics.drawLine(j, 0, j, FONT.getSize() - metrics.getDescent()); 117 118 display = this.getFormated(min + i * (max - min) / ticks); 119 Rectangle2D middleBounds = metrics.getStringBounds(display, graphics); 120 121 j = j - (int) (middleBounds.getWidth() / 2); 122 123 graphics.drawString(display, j, (int) (height - metrics.getDescent())); 124 } 125 126 graphics.drawLine((int) width, 0, (int) width, FONT.getSize() - metrics.getDescent()); 128 graphics.drawString(maxText, (int) (width - maxBounds.getWidth()), (int) (height - metrics.getDescent())); 129 } 130 } 131 | Popular Tags |