1 7 package org.ejtools.graph.renderer; 8 9 import java.awt.Color ; 10 import java.awt.Dimension ; 11 import java.awt.FontMetrics ; 12 import java.awt.Graphics ; 13 import java.awt.Insets ; 14 import java.awt.geom.Rectangle2D ; 15 import java.text.Format ; 16 17 import org.ejtools.graph.Axis; 18 19 24 public class VerticalAxis extends Axis 25 { 26 32 public VerticalAxis(Format format, int position) 33 { 34 this.format = format; 35 this.orientation = VERTICAL; 36 this.position = position; 37 } 38 39 40 45 public void paintComponent(Graphics graphics) 46 { 47 String display; 48 double multiplier = 0.0d; 49 int base; 50 int small; 51 int middle; 52 int correction; 53 54 graphics.setFont(FONT); 55 FontMetrics metrics = graphics.getFontMetrics(); 56 57 Insets insets = this.getInsets(); 58 double height = (double) this.getHeight() - 1 - insets.top - insets.bottom; 62 63 double min = element.getYRange().getMin(); 64 double max = element.getYRange().getMax(); 65 66 String minText = this.getFormated(min); 67 Rectangle2D minBounds = metrics.getStringBounds(minText, graphics); 68 String maxText = this.getFormated(max); 69 Rectangle2D maxBounds = metrics.getStringBounds(maxText, graphics); 70 71 double newBound = Math.max(minBounds.getWidth(), maxBounds.getWidth()); 73 this.dimension = new Dimension ((int) (newBound + FONT.getSize()), this.getHeight()); 74 this.revalidate(); 75 76 double bound = Math.max(minBounds.getHeight() * 4.0d, maxBounds.getHeight() * 4.0d); 78 79 int ticks = 1; 81 while ((height / ticks) > bound) 82 { 83 ticks = ticks * 2; 84 } 85 86 if (this.position == LEFT) 88 { 89 middle = (int) (newBound); 90 small = middle + FONT.getSize() / 2 - 1; 91 base = middle + FONT.getSize() - 1; 92 } 93 else 94 { 95 middle = FONT.getSize() - 2; 96 small = FONT.getSize() / 2 - 1; 97 base = 0; 98 } 99 100 graphics.drawLine(base, 0, base, (int) height); 102 103 multiplier = height / ticks / 2; 105 graphics.setColor(Color.black); 106 for (int i = 0; i <= (2 * ticks); i++) 107 { 108 graphics.drawLine(base, (int) (i * multiplier), small, (int) (i * multiplier)); 109 } 110 111 multiplier = height / ticks; 113 114 graphics.drawLine(base, (int) height, middle, (int) height); 116 if (this.position == LEFT) 117 { 118 correction = -(int) minBounds.getWidth(); 119 } 120 else 121 { 122 correction = 2; 123 } 124 graphics.drawString(minText, middle + correction, (int) (height - metrics.getDescent())); 125 126 for (int i = 1; i < ticks; i++) 128 { 129 int j = (int) (height - i * multiplier); 130 graphics.drawLine(base, j, middle, j); 131 132 display = this.getFormated(min + i * (max - min) / ticks); 133 Rectangle2D middleBounds = metrics.getStringBounds(display, graphics); 134 135 j = j + (int) (middleBounds.getHeight() / 2); 136 if (this.position == LEFT) 137 { 138 correction = -(int) middleBounds.getWidth(); 139 } 140 else 141 { 142 correction = 2; 143 } 144 graphics.drawString(display, middle + correction, j); 145 } 146 147 graphics.drawLine(base, 0, middle, 0); 149 if (this.position == LEFT) 150 { 151 correction = -(int) maxBounds.getWidth(); 152 } 153 else 154 { 155 correction = 2; 156 } 157 graphics.drawString(maxText, middle + correction, metrics.getAscent()); 158 } 159 } 160 | Popular Tags |