1 9 package org.ozoneDB.tools; 10 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.util.*; 14 15 16 18 public class ChartCanvas extends Canvas { 19 20 public static int DEFAULT_TIME_RANGE = 10000; 21 22 23 private Image dbImage = null; 24 private Graphics dbGraphics = null; 25 26 27 private int timeRange = DEFAULT_TIME_RANGE; 28 private int[] times = null; 29 private int pointCounter = 1; 30 private boolean shiftLeftOnEnd = true; 31 private int lowerBorder = 15; 32 33 34 private int averageCount = 3; 35 private int minTime = 0x7FFFFFFF; 36 private int maxTime = 0; 37 private boolean[] showAverage = {true, true, true}; 38 private int[] lastPoint = {0, 0, 0}; 39 private int[] lastAverage = {0, 0, 0}; 40 private int[] averageRange = {1, 10, 30}; 41 private Color[] averageColor = {Color.black, Color.red, Color.blue}; 42 43 44 45 public ChartCanvas() { 46 setBackground( Color.white ); 47 addComponentListener( new ComponentAdapter() { 48 49 50 public void componentResized( ComponentEvent e ) { 51 clear(); 52 } 53 } ); 54 } 55 56 57 58 protected void clear() { 59 dbImage = null; 60 dbGraphics = null; 61 } 62 63 64 65 protected void shiftLeft() { 66 dbGraphics.copyArea( 0, 0, getSize().width, getSize().height - lowerBorder, -1, 0 ); 67 dbGraphics.setColor( getBackground() ); 68 dbGraphics.fillRect( getSize().width - 1, 0, 1, getSize().height ); 69 } 70 71 72 73 protected int timeToPixel( int time ) { 74 int height = getSize().height - lowerBorder; 75 return height - height * time / timeRange - 1; 76 } 77 78 79 80 protected void drawTimePoint( int timeIndex ) { 81 for (int i = 0; i < averageCount; i++) { 82 int average = lastAverage[i] - (timeIndex - averageRange[i] < 0 83 ? 0 84 : times[timeIndex - averageRange[i]]) + times[timeIndex]; 85 int newPoint = timeToPixel( average / averageRange[i] ); 86 if (showAverage[i]) { 87 dbGraphics.setColor( averageColor[i] ); 88 dbGraphics.drawLine( timeIndex - 1, lastPoint[i], timeIndex, newPoint ); 89 } 90 lastPoint[i] = newPoint; 91 lastAverage[i] = average; 92 } 93 } 94 95 96 97 protected void drawStatus() { 98 dbGraphics.setColor( getBackground() ); 99 dbGraphics.fillRect( 0, getSize().height - lowerBorder, getSize().width, lowerBorder ); 100 101 dbGraphics.setColor( Color.black ); 102 dbGraphics.drawLine( 0, getSize().height - lowerBorder, getSize().width, getSize().height - lowerBorder ); 103 dbGraphics.drawString( "t: " + lastAverage[0] / averageRange[0] + " am: " + lastAverage[1] / averageRange[1] 104 + " al: " + lastAverage[2] / averageRange[2] + " min: " + minTime + " max: " + maxTime, 5, 105 getSize().height - 4 ); 106 } 107 108 109 110 public void paint( Graphics g ) { 111 if (dbImage == null) { 112 dbImage = createImage( getSize().width, getSize().height ); 113 dbGraphics = dbImage.getGraphics(); 114 dbGraphics.setColor( getBackground() ); 115 dbGraphics.fillRect( 0, 0, getSize().width, getSize().height ); 116 Font fontNorm = dbGraphics.getFont(); 117 dbGraphics.setFont( new Font( "Courier", fontNorm.getStyle(), fontNorm.getSize() - 1 ) ); 118 } 119 g.drawImage( dbImage, 0, 0, this ); 120 } 121 122 123 124 public void update( Graphics g ) { 125 paint( g ); 126 } 127 128 129 130 public void appendTime( long time ) { 131 if (times == null) { 132 times = new int[getSize().width]; 133 lastPoint[0] = lastPoint[1] = lastPoint[2] = getSize().height; 134 } 135 136 if (pointCounter == times.length) { 137 if (shiftLeftOnEnd) { 138 int[] help = new int[times.length]; 139 System.arraycopy( times, 1, help, 0, help.length - 1 ); 140 times = help; 141 pointCounter--; 142 shiftLeft(); 143 } else { 144 pointCounter = 1; 145 clear(); 146 } 147 } 148 149 times[pointCounter] = (int)time; 150 minTime = Math.min( minTime, (int)time ); 151 maxTime = Math.max( maxTime, (int)time ); 152 drawTimePoint( pointCounter ); 153 drawStatus(); 154 paint( getGraphics() ); 155 pointCounter++; 156 } 157 158 159 160 public void redrawAll() { 161 paint( getGraphics() ); 162 dbGraphics.setColor( getBackground() ); 163 dbGraphics.fillRect( 0, 0, getSize().width, getSize().height ); 164 dbGraphics.setColor( Color.black ); 165 for (int i = 1; i < pointCounter; i++) { 166 drawTimePoint( i ); 167 } 168 drawStatus(); 169 } 170 171 172 173 public void reset() { 174 timeRange = DEFAULT_TIME_RANGE; 175 times = null; 176 pointCounter = 1; 177 shiftLeftOnEnd = true; 178 lowerBorder = 15; 179 minTime = 0x7FFFFFFF; 180 maxTime = 0; 181 showAverage = new boolean[] {true, true, true}; 182 lastPoint = new int[] {0, 0, 0}; 183 lastAverage = new int[] {0, 0, 0}; 184 averageRange = new int[] {1, 10, 30}; 185 averageColor = new Color[] {Color.black, Color.red, Color.blue}; 186 clear(); 187 paint( getGraphics() ); 188 } 189 190 191 192 public void setTimeRange( int time ) { 193 timeRange = time; 194 } 195 196 197 198 public int timeRange() { 199 return timeRange; 200 } 201 202 203 204 public void setShiftLeftOnEnd( boolean value ) { 205 shiftLeftOnEnd = value; 206 } 207 208 209 210 public boolean shiftLeftOnEnd() { 211 return shiftLeftOnEnd; 212 } 213 214 215 216 public void enableAverages( boolean[] values ) { 217 showAverage = values; 218 } 219 220 221 222 public boolean[] averageEnabled() { 223 return showAverage; 224 } 225 226 227 228 public void setAverageRanges( int[] values ) { 229 averageRange = values; 230 } 231 232 233 234 public int[] averageRanges() { 235 return averageRange; 236 } 237 238 239 240 public void setAverageColors( Color[] colors ) { 241 averageColor = colors; 242 } 243 244 245 246 public Color[] averageColors() { 247 return averageColor; 248 } 249 250 251 252 public String [] filterAndApplyArgs( String [] args ) { 253 Vector remainingArgs = new Vector(); 254 for (int i = 0; i < args.length; i++) { 255 if (args[i].startsWith( "-timeRange=" )) { 256 timeRange = new Integer ( args[i].substring( 11 ) ).intValue(); 257 } else if (args[i].startsWith( "-shortRange=" )) { 258 averageRange[0] = new Integer ( args[i].substring( 12 ) ).intValue(); 259 } else if (args[i].startsWith( "-mediumRange=" )) { 260 averageRange[1] = new Integer ( args[i].substring( 13 ) ).intValue(); 261 } else if (args[i].startsWith( "-longRange=" )) { 262 averageRange[2] = new Integer ( args[i].substring( 11 ) ).intValue(); 263 } else if (args[i].startsWith( "-shortAverage=" )) { 264 showAverage[0] = args[i].substring( 14 ).equals( "on" ); 265 } else if (args[i].startsWith( "-mediumAverage=" )) { 266 showAverage[1] = args[i].substring( 15 ).equals( "on" ); 267 } else if (args[i].startsWith( "-longAverage=" )) { 268 showAverage[2] = args[i].substring( 13 ).equals( "on" ); 269 } else if (args[i].startsWith( "-shiftLeft=" )) { 270 shiftLeftOnEnd = args[i].substring( 11 ).equals( "on" ); 271 } else { 272 remainingArgs.addElement( args[i] ); 273 } 274 } 275 276 redrawAll(); 277 278 String [] result = new String [remainingArgs.size()]; 279 for (int i = 0; i < result.length; i++) { 280 result[i] = (String )remainingArgs.elementAt( i ); 281 } 282 283 return result; 284 } 285 286 287 288 public static String availableArgs() { 289 return 290 "[-timeRange=<millisecs>] [-shortRange=<range>] [-mediumRange=<range>] [-longRange=<range>] " 291 + "[-shortAverage=(on|off)] [-mediumAverage=(on|off)] [-longAverage=(on|off)] [-shiftLeft=(on|off)]"; 292 } 293 294 295 296 public static String helpText() { 297 return 298 " -timeRange= : the maximum time to be shown on the y-axis in milli seconds, default: 20000\n" 299 + " -shortRange= : the range of the short average, default: last 1\n" 300 + " -mediumRange= : the range of the medium average, default: last 10\n" 301 + " -longRange= : the range of the long average, default: last 30\n" 302 + " -shortAverage= : switch the short average line on or off\n" 303 + "-mediumAverage= : switch the medium average line on or off\n" 304 + " -longAverage= : switch the long average line on or off\n" 305 + " -shiftLeft= : shift the chart left, if the line touchs the right border\n"; 306 307 } 308 } 309 | Popular Tags |