1 25 package org.jrobin.graph; 26 27 import java.awt.Color ; 28 import java.awt.Stroke ; 29 import java.awt.Graphics2D ; 30 31 38 class ChartGraphics 39 { 40 private Graphics2D g; 44 45 private int width, height; 46 private long xStart, xEnd; 47 private double yStart, yEnd; 48 49 private double widthDelta = 1.0d, heightDelta = 3.0d; 50 51 58 ChartGraphics( Graphics2D graphics ) 59 { 60 g = graphics; 61 } 62 63 64 75 void drawLine(int x1, int y1, int x2, int y2) 76 { 77 g.drawLine( x1, -y1, x2, -y2 ); 78 } 79 80 90 void fillRect(int x1, int y1, int x2, int y2) 92 { 93 g.fillRect( x1, -y2, x2 - x1, - (y2 - y1) ); 94 } 95 96 101 void setColor( Color c ) 102 { 103 g.setColor( c ); 104 } 105 106 111 void setDimensions( int width, int height ) 112 { 113 this.width = width; 114 this.height = height; 115 } 116 117 122 void setXRange( long start, long end ) 123 { 124 xStart = start; 125 xEnd = end; 126 127 if ( xEnd != xStart ) 128 widthDelta = width * 1.0d / (( xEnd - xStart) * 1.0d); 129 else 130 widthDelta = 1.0d; 131 } 132 133 138 void setYRange( double lower, double upper ) 139 { 140 yStart = lower; 141 yEnd = upper; 142 143 if ( yEnd != yStart ) 144 heightDelta = height * 1.0d / (( yEnd - yStart) * 1.0d); 145 else 146 heightDelta = 1.0d; 147 148 yStart = (yStart < 0 ? 0 : Math.abs(yStart)); 149 } 150 151 156 int getX( long timestamp ) 157 { 158 return (int) ((timestamp - xStart) * widthDelta); 159 } 160 161 166 int getY( double value ) 167 { 168 if ( Double.isNaN(value) ) return Integer.MIN_VALUE; 169 170 return (int) ((value - yStart ) * heightDelta); 171 } 172 173 double getInverseY( int value ) 174 { 175 if ( value == Integer.MIN_VALUE ) return Double.NaN; 176 177 return (value * 1.0d/heightDelta) + yStart; 178 } 179 180 184 void setStroke( Stroke s ) 185 { 186 g.setStroke( s ); 187 } 188 189 193 int getMinX() 194 { 195 return 0; 196 } 197 198 202 int getMaxX() 203 { 204 return 0 + width; 205 } 206 207 211 int getMinY() 212 { 213 return 0; 214 } 215 216 220 int getMaxY() 221 { 222 return 0 + height; 223 } 224 225 229 Graphics2D getGraphics() 230 { 231 return g; 232 } 233 } 234 | Popular Tags |