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 GridGraphRenderer extends Axis 25 { 26 27 protected Format horizontalFormat = null; 28 29 protected Format verticalFormat = null; 30 31 32 38 public GridGraphRenderer(Format horizontalFormat, Format verticalFormat) 39 { 40 this.format = horizontalFormat; 41 this.horizontalFormat = horizontalFormat; 42 this.verticalFormat = verticalFormat; 43 } 44 45 46 49 protected void paintComponent(Graphics graphics) 50 { 51 double multiplier = 0.0d; 52 53 graphics.setFont(FONT); 54 FontMetrics metrics = graphics.getFontMetrics(); 55 graphics.setColor(Color.lightGray); 56 57 double minX = element.getXRange().getMin(); 58 double maxX = element.getXRange().getMax(); 59 double minY = element.getYRange().getMin(); 60 double maxY = element.getYRange().getMax(); 61 62 Insets insets = this.getInsets(); 63 int x = insets.left; 64 int y = insets.top; 65 double width = (double) this.getWidth() - 1 - insets.left - insets.right; 66 double height = (double) this.getHeight() - 1 - insets.top - insets.bottom; 67 68 double scaleX = width / (maxX - minX); 69 double scaleY = -height / (maxY - minY); 70 double offsetX = x - scaleX * minX; 71 double offsetY = y - scaleY * maxY; 72 73 if (this.horizontalScaling == GraphRenderer.ALIGN_LEFT) 74 { 75 scaleX = 1.0d; 76 offsetX = -minX; 77 } 78 if (this.horizontalScaling == GraphRenderer.ALIGN_RIGHT) 79 { 80 scaleX = 1.0d * this.horizontalScale; 81 offsetX = width - maxX * this.horizontalScale; 82 } 83 84 minX = (x - offsetX) / scaleX; 86 this.format = this.horizontalFormat; 87 88 String minText = this.getFormated(minX); 89 Rectangle2D minBounds = metrics.getStringBounds(minText, graphics); 90 String maxText = this.getFormated(maxX); 91 Rectangle2D maxBounds = metrics.getStringBounds(maxText, graphics); 92 93 double bound = Math.max(minBounds.getWidth() * 4.0d, maxBounds.getWidth() * 4.0d); 95 96 int ticks = 1; 98 while ((width / ticks) > bound) 99 { 100 ticks = ticks * 2; 101 } 102 103 multiplier = width / ticks / 2; 105 for (int i = 0; i <= (2 * ticks); i++) 106 { 107 graphics.drawLine((int) (i * multiplier), 0, (int) (i * multiplier), (int) height); 108 } 109 110 this.format = this.verticalFormat; 112 113 minText = this.getFormated(minY); 114 minBounds = metrics.getStringBounds(minText, graphics); 115 maxText = this.getFormated(maxY); 116 maxBounds = metrics.getStringBounds(maxText, graphics); 117 118 bound = Math.max(minBounds.getHeight() * 4.0d, maxBounds.getHeight() * 4.0d); 120 121 ticks = 1; 123 while ((height / ticks) > bound) 124 { 125 ticks = ticks * 2; 126 } 127 128 multiplier = height / ticks / 2; 130 for (int i = 0; i <= (2 * ticks); i++) 131 { 132 graphics.drawLine(0, (int) (i * multiplier), (int) width, (int) (i * multiplier)); 133 } 134 135 this.element.draw(graphics, scaleX, offsetX, scaleY, offsetY); 136 } 137 } 138 | Popular Tags |