| 1 34 35 36 package org.krysalis.jcharts.axisChart.axis; 37 38 39 import org.krysalis.jcharts.axisChart.AxisChart; 40 import org.krysalis.jcharts.properties.*; 41 import org.krysalis.jcharts.properties.util.ChartFont; 42 import org.krysalis.jcharts.test.HTMLGenerator; 43 import org.krysalis.jcharts.test.HTMLTestable; 44 45 import java.awt.*; 46 import java.awt.geom.Line2D ; 47 import java.lang.reflect.Field ; 48 49 50 55 public class YAxis extends Axis implements HTMLTestable 56 { 57 58 private float lastTickY; 59 60 61 66 public YAxis( AxisChart axisChart, int numberOfScaleItems ) 67 { 68 super( axisChart, numberOfScaleItems ); 69 } 70 71 72 77 public float getLastTickY() 78 { 79 return lastTickY; 80 } 81 82 83 88 public void computeMinimumWidthNeeded( String yAxisTitle ) 89 { 90 AxisTypeProperties axisTypeProperties= super.getAxisChart().getAxisProperties().getYAxisProperties(); 91 92 93 float widthNeeded = 0; 94 95 96 if( axisTypeProperties.showAxisLabels() ) 98 { 99 if ( axisTypeProperties.getShowRightAxis() ) 102 { 103 if ( super.getAxisLabelsGroupRight().getWidestLabel()>super.getAxisLabelsGroup().getWidestLabel()) 104 { 105 widthNeeded+= super.getAxisLabelsGroupRight().getWidestLabel(); 106 } 107 else 108 { 109 widthNeeded+= super.getAxisLabelsGroup().getWidestLabel(); 110 } 111 } 112 else 113 { 114 widthNeeded+= super.getAxisLabelsGroup().getWidestLabel(); 115 } 116 } 117 118 119 if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE ) 120 { 121 widthNeeded += axisTypeProperties.getPaddingBetweenLabelsAndTicks(); 123 124 widthNeeded += axisTypeProperties.getAxisTickMarkPixelLength(); 126 } 127 else 128 { 129 if( axisTypeProperties.showAxisLabels() ) 131 { 132 widthNeeded += axisTypeProperties.getPaddingBetweenAxisAndLabels(); 133 } 134 } 135 136 137 if( yAxisTitle != null ) 140 { 141 super.computeAxisTitleDimensions( yAxisTitle, axisTypeProperties.getTitleChartFont() ); 142 widthNeeded += super.getTitleHeight(); 143 widthNeeded += axisTypeProperties.getPaddingBetweenAxisTitleAndLabels(); 144 } 145 146 super.setMinimumWidthNeeded( widthNeeded ); 147 } 148 149 150 156 private void renderAxisTitle( String axisTitle, Graphics2D graphics2D, AxisTypeProperties axisTypeProperties ) 157 { 158 if( axisTitle != null ) 159 { 160 float titleY; float titleX = super.getAxisChart().getXAxis().getOrigin() - super.getMinimumWidthNeeded() + super.getTitleHeight(); 162 163 if( super.getTitleWidth() > super.getPixelLength() ) 165 { 166 titleY= super.getAxisChart().getImageHeight() - ( ( super.getAxisChart().getImageHeight() - super.getTitleWidth() ) / 2 ); 168 } 169 else 171 { 172 titleY = this.getOrigin() - ( ( super.getPixelLength() - super.getTitleWidth() ) / 2 ); 173 } 174 175 graphics2D.setFont( axisTypeProperties.getAxisTitleChartFont().getFont().deriveFont( ChartFont.VERTICAL_ROTATION ) ); 177 graphics2D.setPaint( axisTypeProperties.getAxisTitleChartFont().getPaint() ); 178 graphics2D.drawString( axisTitle, titleX, titleY ); 179 } 180 } 181 182 183 190 public void render( Graphics2D graphics2D, AxisProperties axisProperties, String yAxisTitle ) 191 { 192 AxisTypeProperties axisTypeProperties = axisProperties.getYAxisProperties(); 193 194 195 this.renderAxisTitle( yAxisTitle, graphics2D, axisProperties.getYAxisProperties() ); 197 198 199 Line2D.Float line2D; 200 float stringY= 0; 201 202 203 if( axisTypeProperties instanceof DataAxisProperties ) 204 { 205 line2D = new Line2D.Float ( 0.0f, super.getOrigin(), 0.0f, super.getOrigin() ); 207 208 if( axisTypeProperties.showAxisLabels() ) 209 { 210 stringY = super.getOrigin() + ( super.getAxisLabelsGroup().getTallestLabel() / 4 ); 211 } 212 } 213 else 214 { 215 float y = super.getOrigin() - ( super.getScalePixelWidth() / 2 ); 217 line2D = new Line2D.Float ( 0.0f, y, 0.0f, y ); 218 219 if( axisTypeProperties.showAxisLabels() ) 220 { 221 stringY = y + ( super.getAxisLabelsGroup().getTallestLabel() / 4 ); 222 223 super.getAxisLabelsGroup().reverse(); 225 } 226 } 227 228 229 float tickX1 = super.getAxisChart().getXAxis().getOrigin() - axisTypeProperties.getAxisTickMarkPixelLength(); 230 float tickX2 = super.getAxisChart().getXAxis().getOrigin(); 231 float gridLineX1 = super.getAxisChart().getXAxis().getOrigin() + 1; 232 float gridLineX2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength(); 233 234 235 float stringX = super.getAxisChart().getXAxis().getOrigin() - axisTypeProperties.getAxisTickMarkPixelLength(); 236 237 if( axisTypeProperties.showAxisLabels() ) 238 { 239 stringX-= axisTypeProperties.getPaddingBetweenLabelsAndTicks(); 240 } 241 242 243 for( int i = 0; i < super.getNumberOfScaleItems(); i++ ) 244 { 245 if( axisTypeProperties.getShowGridLines() != AxisTypeProperties.GRID_LINES_NONE ) 247 { 248 if( i > 0 || ( i == 0 && !( axisTypeProperties instanceof DataAxisProperties ) ) ) 250 { 251 line2D.x1 = gridLineX1; 252 line2D.x2 = gridLineX2; 253 254 256 if( i < super.getAxisLabelsGroup().size() 257 || ( i == super.getAxisLabelsGroup().size() && !axisTypeProperties.getShowEndBorder() ) ) 258 259 { 261 axisTypeProperties.getGridLineChartStroke().draw( graphics2D, line2D ); 262 } 263 else 264 { 265 axisProperties.getXAxisProperties().getAxisStroke().draw( graphics2D, line2D ); 267 } 268 } 269 } 270 271 if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE ) 273 { 274 line2D.x1 = tickX1; 275 line2D.x2 = tickX2; 276 axisTypeProperties.getTickChartStroke().draw( graphics2D, line2D ); 277 } 278 279 this.lastTickY= line2D.y1; 282 283 line2D.y1 -= super.getScalePixelWidth(); 284 line2D.y2 = line2D.y1; 285 286 287 if( axisTypeProperties.showAxisLabels() ) 289 { 290 super.getAxisLabelsGroup().render( i, graphics2D, stringX - super.getAxisLabelsGroup().getTextTag( i ).getWidth(), stringY ); 291 } 294 295 stringY -= super.getScalePixelWidth(); 296 } 297 298 299 line2D.x1 = super.getAxisChart().getXAxis().getOrigin(); 301 line2D.x2 = line2D.x1; 302 line2D.y1 = super.getOrigin() - super.getPixelLength(); 303 line2D.y2 = super.getOrigin(); 304 axisTypeProperties.getAxisStroke().draw( graphics2D, line2D ); 305 306 307 if( axisTypeProperties.getShowEndBorder() ) 309 { 310 line2D.x1 = super.getAxisChart().getXAxis().getOrigin(); 311 line2D.x2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength(); 312 line2D.y1 = super.getOrigin() - super.getPixelLength(); 313 line2D.y2 = line2D.y1; 314 axisProperties.getXAxisProperties().getAxisStroke().draw( graphics2D, line2D ); 315 } 316 317 318 if( axisTypeProperties instanceof DataAxisProperties ) 320 { 321 DataAxisProperties dataAxisProperties = ( DataAxisProperties ) axisTypeProperties; 322 323 if( dataAxisProperties.showZeroLine() 324 && super.getScaleCalculator().getMinValue() < 0.0d 325 && super.getScaleCalculator().getMaxValue() > 0.0d ) 326 { 327 line2D.y1 = super.getZeroLineCoordinate(); 328 line2D.y2 = line2D.y1; 329 line2D.x1 = super.getAxisChart().getXAxis().getOrigin(); 330 line2D.x2 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength(); 331 dataAxisProperties.getZeroLineChartStroke().draw( graphics2D, line2D ); 332 } 333 } 334 335 338 if (axisTypeProperties.getShowRightAxis()) 339 { 340 341 line2D.x1 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength(); 342 line2D.x2 = line2D.x1; 343 line2D.y1 = super.getOrigin() - super.getPixelLength() - 10 ; 344 line2D.y2 = super.getOrigin(); 345 axisTypeProperties.getAxisStroke().draw( graphics2D, line2D ); 346 347 float tickRightX1 = super.getAxisChart().getXAxis().getOrigin() + super.getAxisChart().getXAxis().getPixelLength(); 348 float tickRightX2 = tickRightX1 + axisTypeProperties.getAxisTickMarkPixelLength(); 349 line2D.y1 = super.getOrigin(); 350 line2D.y2 = line2D.y1; 351 352 stringY = super.getOrigin() + ( super.getAxisLabelsGroupRight().getTallestLabel() / 4 ); 353 stringX = super.getAxisChart().getXAxis().getOrigin() + axisTypeProperties.getAxisTickMarkPixelLength() 354 + super.getAxisChart().getXAxis().getPixelLength(); 355 356 stringX+= axisTypeProperties.getPaddingBetweenLabelsAndTicks(); 357 358 for( int i = 0; i < super.getNumberOfScaleItems(); i++ ) 359 { 360 if( axisTypeProperties.getShowTicks() != AxisTypeProperties.TICKS_NONE ) 362 { 363 line2D.x1 = tickRightX1; 364 line2D.x2 = tickRightX2; 365 axisTypeProperties.getTickChartStroke().draw( graphics2D, line2D ); 366 } 367 368 line2D.y1 -= super.getScalePixelWidth(); 369 line2D.y2 = line2D.y1; 370 371 372 if( axisTypeProperties.showAxisLabels() ) 374 { 375 376 super.getAxisLabelsGroupRight().render( i, graphics2D, stringX , stringY ); 380 } 381 stringY -= super.getScalePixelWidth(); 382 } 383 } 384 } 385 386 387 397 public float computeAxisCoordinate( float origin, double value, double axisMinValue ) 398 { 399 double returnValue = origin - ( value - axisMinValue ) * this.getOneUnitPixelSize(); 400 return ( float ) returnValue; 401 } 402 403 404 409 public void toHTML( HTMLGenerator htmlGenerator ) 411 { 412 htmlGenerator.propertiesTableStart( this.getClass().getName() ); 413 414 super.toHTML( htmlGenerator ); 415 416 Field [] fields = this.getClass().getDeclaredFields(); 417 for( int i = 0; i < fields.length; i++ ) 418 { 419 try 420 { 421 htmlGenerator.addField( fields[ i ].getName(), fields[ i ].get( this ) ); 422 } 423 catch( IllegalAccessException illegalAccessException ) 424 { 425 illegalAccessException.printStackTrace(); 426 } 427 } 428 429 htmlGenerator.propertiesTableEnd(); 430 } 431 432 } 433 | Popular Tags |