1 34 35 package org.krysalis.jcharts.axisChart; 36 37 38 import java.awt.Graphics2D ; 39 import java.awt.geom.Rectangle2D ; 40 41 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.AxisValueRenderEvent; 42 import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet; 43 import org.krysalis.jcharts.chartText.TextTagGroup; 44 import org.krysalis.jcharts.imageMap.RectMapArea; 45 import org.krysalis.jcharts.properties.BarChartProperties; 46 47 48 52 abstract class BarChart 53 { 54 55 61 static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet ) 62 { 63 Graphics2D g2d = axisChart.getGraphics2D(); 64 BarChartProperties barChartProperties = (BarChartProperties) iAxisChartDataSet.getChartTypeProperties(); 65 float barWidth; 66 67 float startingX; 69 float startingY; 70 float width; 71 float height; 72 73 if( axisChart.getAxisProperties().isPlotHorizontal() ) 74 { 75 barWidth = axisChart.getYAxis().getScalePixelWidth() * barChartProperties.getPercentage(); 76 startingX = axisChart.getXAxis().getZeroLineCoordinate(); 77 startingY = axisChart.getYAxis().getLastTickY() - (barWidth / 2); 78 width = 0; 79 height = barWidth; 80 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 81 82 BarChart.horizontalPlot( axisChart, 83 iAxisChartDataSet, 84 barChartProperties, 85 g2d, 86 rectangle, 87 startingX ); 88 } 89 else 90 { 91 barWidth = axisChart.getXAxis().getScalePixelWidth() * barChartProperties.getPercentage(); 92 startingX = axisChart.getXAxis().getTickStart() - (barWidth / 2); 93 startingY = axisChart.getYAxis().getZeroLineCoordinate(); 94 width = barWidth; 95 height = 0; 96 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 97 98 BarChart.verticalPlot( axisChart, 99 iAxisChartDataSet, 100 barChartProperties, 101 g2d, 102 rectangle, 103 startingY ); 104 } 105 } 106 107 108 116 private static void horizontalPlot( AxisChart axisChart, 117 IAxisChartDataSet iAxisChartDataSet, 118 BarChartProperties barChartProperties, 119 Graphics2D g2d, 120 Rectangle2D.Float rectangle, 121 float startingX ) 122 { 123 int imageMapLabelIndex = axisChart.getYAxis().getNumberOfScaleItems() - 1; 124 125 Rectangle2D.Float totalItemArea = new Rectangle2D.Float (); 127 totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1; 128 totalItemArea.height = axisChart.getYAxis().getScalePixelWidth() - 1; 129 totalItemArea.x = axisChart.getXAxis().getOrigin() + 1; 130 totalItemArea.width = axisChart.getXAxis().getPixelLength() - 1; 131 132 AxisValueRenderEvent axisValueRenderEvent = new AxisValueRenderEvent( axisChart, 134 iAxisChartDataSet, 135 g2d, 136 totalItemArea, 137 axisChart.getXAxis().getZeroLineCoordinate() ); 138 139 axisValueRenderEvent.setDataSetIndex( 0 ); 141 142 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 144 { 145 g2d.setPaint( iAxisChartDataSet.getPaint( 0 ) ); 147 148 axisValueRenderEvent.setValueX( axisChart.getXAxis().getZeroLineCoordinate() ); 150 axisValueRenderEvent.setValueY( (float) rectangle.getCenterY() ); 151 axisValueRenderEvent.setValueIndex( i ); 152 153 barChartProperties.firePreRender( axisValueRenderEvent ); 155 156 if( iAxisChartDataSet.getValue( 0, i ) != 0.0d ) 158 { 159 if( iAxisChartDataSet.getValue( 0, i ) < 0 ) 160 { 161 rectangle.x = axisChart.getXAxis().computeAxisCoordinate( axisChart.getXAxis().getOrigin(), 162 iAxisChartDataSet.getValue( 0, i ), 163 axisChart.getXAxis().getScaleCalculator().getMinValue() ); 164 rectangle.width = startingX - rectangle.x; 165 166 axisValueRenderEvent.setValueX( rectangle.x ); 168 } 169 else 170 { 171 rectangle.x = startingX; 172 rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( 0, i ), 173 axisChart.getXAxis().getOneUnitPixelSize() ); 174 175 axisValueRenderEvent.setValueX( rectangle.x + rectangle.width ); 177 } 178 179 if( rectangle.width != 0 ) 181 { 182 g2d.fill( rectangle ); 183 184 if( barChartProperties.getShowOutlinesFlag() ) 185 { 186 barChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 187 } 188 189 if( axisChart.getGenerateImageMapFlag() ) 191 { 192 RectMapArea rectMapArea= createImageMapArea( axisChart.getYAxis().getAxisLabelsGroup(), 193 imageMapLabelIndex, 194 rectangle, 195 iAxisChartDataSet.getValue( 0, i ), 196 iAxisChartDataSet.getLegendLabel( 0 ) ); 197 axisChart.getImageMap().addImageMapArea( rectMapArea ); 198 199 imageMapLabelIndex--; 200 } 201 } 202 } 203 else 204 { 205 207 if( axisChart.getGenerateImageMapFlag() ) 209 { 210 if( axisChart.getXAxis().getScaleCalculator().getMaxValue() >= 0 214 && axisChart.getXAxis().getScaleCalculator().getMinValue() <= 0 ) 215 { 216 rectangle.x = startingX; 217 rectangle.width = 1; 218 219 RectMapArea rectMapArea = createImageMapArea( axisChart.getYAxis().getAxisLabelsGroup(), 220 imageMapLabelIndex, 221 rectangle, 222 iAxisChartDataSet.getValue( 0, i ), 223 iAxisChartDataSet.getLegendLabel( 0 ) ); 224 225 axisChart.getImageMap().addImageMapArea( rectMapArea ); 226 } 227 228 imageMapLabelIndex--; 229 } 230 } 231 232 barChartProperties.firePostRender( axisValueRenderEvent ); 234 totalItemArea.y += axisChart.getYAxis().getScalePixelWidth(); 235 236 rectangle.y += axisChart.getYAxis().getScalePixelWidth(); 237 } 238 } 239 240 241 249 private static void verticalPlot( AxisChart axisChart, 250 IAxisChartDataSet iAxisChartDataSet, 251 BarChartProperties barChartProperties, 252 Graphics2D g2d, 253 Rectangle2D.Float rectangle, 254 float startingY ) 255 { 256 Rectangle2D.Float totalItemArea = new Rectangle2D.Float (); 258 totalItemArea.x = axisChart.getXAxis().getOrigin() + 1; 259 totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1; 260 totalItemArea.width = axisChart.getXAxis().getScalePixelWidth() - 1; 261 totalItemArea.height = axisChart.getYAxis().getPixelLength() - 1; 262 263 AxisValueRenderEvent axisValueRenderEvent = new AxisValueRenderEvent( axisChart, 265 iAxisChartDataSet, 266 g2d, 267 totalItemArea, 268 axisChart.getYAxis().getZeroLineCoordinate() ); 269 270 axisValueRenderEvent.setDataSetIndex( 0 ); 272 273 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 275 { 276 g2d.setPaint( iAxisChartDataSet.getPaint( 0 ) ); 278 279 axisValueRenderEvent.setValueX( (float) rectangle.getCenterX() ); 281 axisValueRenderEvent.setValueY( axisChart.getYAxis().getZeroLineCoordinate() ); 282 axisValueRenderEvent.setValueIndex( i ); 283 284 barChartProperties.firePreRender( axisValueRenderEvent ); 286 287 if( iAxisChartDataSet.getValue( 0, i ) != 0.0d ) 289 { 290 if( iAxisChartDataSet.getValue( 0, i ) < 0 ) 291 { 292 rectangle.y = startingY; 293 rectangle.height = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( 0, i ), axisChart.getYAxis().getOneUnitPixelSize() ); 294 295 axisValueRenderEvent.setValueY( rectangle.y + rectangle.height ); 296 } 297 else 298 { 299 rectangle.y = axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(), 300 iAxisChartDataSet.getValue( 0, i ), 301 axisChart.getYAxis().getScaleCalculator().getMinValue() ); 302 rectangle.height = startingY - rectangle.y; 303 304 axisValueRenderEvent.setValueY( rectangle.y ); 305 } 306 307 308 if( rectangle.height != 0 ) 310 { 311 g2d.fill( rectangle ); 312 313 if( barChartProperties.getShowOutlinesFlag() ) 314 { 315 barChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 316 g2d.setPaint( iAxisChartDataSet.getPaint( 0 ) ); 317 } 318 } 319 320 if( axisChart.getGenerateImageMapFlag() ) 322 { 323 RectMapArea rectMapArea = createImageMapArea( axisChart.getXAxis().getAxisLabelsGroup(), 324 i, 325 rectangle, 326 iAxisChartDataSet.getValue( 0, i ), 327 iAxisChartDataSet.getLegendLabel( 0 ) ); 328 329 axisChart.getImageMap().addImageMapArea( rectMapArea ); 330 } 331 } 332 else 333 { 334 if( axisChart.getGenerateImageMapFlag() ) 336 { 337 if( axisChart.getYAxis().getScaleCalculator().getMaxValue() >= 0 340 && axisChart.getYAxis().getScaleCalculator().getMinValue() <= 0 ) 341 { 342 rectangle.y = startingY; 343 rectangle.height = 1; 344 345 RectMapArea rectMapArea = createImageMapArea( axisChart.getXAxis().getAxisLabelsGroup(), 346 i, 347 rectangle, 348 iAxisChartDataSet.getValue( 0, i ), 349 iAxisChartDataSet.getLegendLabel( 0 ) ); 350 351 axisChart.getImageMap().addImageMapArea( rectMapArea ); 352 } 353 } 354 } 355 356 barChartProperties.firePostRender( axisValueRenderEvent ); 358 totalItemArea.x += axisChart.getXAxis().getScalePixelWidth(); 359 360 rectangle.x += axisChart.getXAxis().getScalePixelWidth(); 361 } 362 } 363 364 365 374 static float computeScaleHeightOfValue( double value, double oneUnitPixelSize ) 375 { 376 return (float) Math.abs( (value) * oneUnitPixelSize ); 377 } 378 379 380 388 private static RectMapArea createImageMapArea( TextTagGroup axisLabelsGroup, 389 int labelIndex, 390 Rectangle2D.Float rectangle, 391 double value, 392 String legendLabel ) 393 { 394 String label; 395 if( axisLabelsGroup != null ) 396 { 397 label = axisLabelsGroup.getTextTag( labelIndex ).getText(); 398 } 399 else 400 { 401 label = ""; 402 } 403 404 return new RectMapArea( rectangle, value, label, legendLabel ); 405 } 406 } | Popular Tags |