1 34 35 36 package org.krysalis.jcharts.axisChart; 37 38 39 import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet; 40 import org.krysalis.jcharts.imageMap.RectMapArea; 41 import org.krysalis.jcharts.properties.*; 42 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.AxisValueRenderEvent; 43 44 import java.awt.*; 45 import java.awt.geom.Rectangle2D ; 46 47 48 53 abstract class ClusteredBarChart 54 { 55 56 63 static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet ) 64 { 65 Graphics2D g2d = axisChart.getGraphics2D(); 66 ClusteredBarChartProperties clusteredBarChartProperties = ( ClusteredBarChartProperties ) iAxisChartDataSet.getChartTypeProperties(); 67 68 69 float barGroupWidth; 70 float barWidth; 71 72 float startingY; 74 float startingX; 75 float width; 76 float height; 77 78 DataAxisProperties dataAxisProperties; 79 if( axisChart.getAxisProperties().isPlotHorizontal() ) 80 { 81 dataAxisProperties = ( DataAxisProperties ) axisChart.getAxisProperties().getXAxisProperties(); 82 83 barGroupWidth = axisChart.getYAxis().getScalePixelWidth() * clusteredBarChartProperties.getPercentage(); 84 barWidth = barGroupWidth / iAxisChartDataSet.getNumberOfDataSets(); 85 86 startingX = axisChart.getXAxis().getZeroLineCoordinate(); 88 startingY = axisChart.getYAxis().getLastTickY() - ( barGroupWidth / 2 ); 89 width = 0; 90 height = barWidth; 91 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 92 93 horizontalPlot( axisChart, 94 iAxisChartDataSet, 95 clusteredBarChartProperties, 96 dataAxisProperties, 97 g2d, 98 rectangle, 99 startingX, 100 startingY, 101 barWidth ); 102 } 103 else 104 { 105 dataAxisProperties = ( DataAxisProperties ) axisChart.getAxisProperties().getYAxisProperties(); 106 107 barGroupWidth = axisChart.getXAxis().getScalePixelWidth() * clusteredBarChartProperties.getPercentage(); 108 barWidth = barGroupWidth / iAxisChartDataSet.getNumberOfDataSets(); 109 110 startingX = axisChart.getXAxis().getTickStart() - ( barGroupWidth / 2 ); 112 startingY = axisChart.getYAxis().getZeroLineCoordinate(); 113 width = barWidth; 114 height = 0; 115 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 116 117 verticalPlot( axisChart, 118 iAxisChartDataSet, 119 clusteredBarChartProperties, 120 dataAxisProperties, 121 g2d, 122 rectangle, 123 startingX, 124 startingY, 125 barWidth ); 126 } 127 } 128 129 130 143 private static void horizontalPlot( AxisChart axisChart, 144 IAxisChartDataSet iAxisChartDataSet, 145 ClusteredBarChartProperties clusteredBarChartProperties, 146 DataAxisProperties dataAxisProperties, 147 Graphics2D g2d, 148 Rectangle2D.Float rectangle, 149 float startingX, 150 float startingY, 151 float barWidth ) 152 { 153 int imageMapLabelIndex = axisChart.getYAxis().getNumberOfScaleItems() - 1; 154 155 Rectangle2D.Float totalItemArea= new Rectangle2D.Float (); 157 totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1; 158 totalItemArea.height= axisChart.getYAxis().getScalePixelWidth() - 1; 159 totalItemArea.x= axisChart.getXAxis().getOrigin() + 1; 160 totalItemArea.width= axisChart.getXAxis().getPixelLength() - 1; 161 162 163 AxisValueRenderEvent axisValueRenderEvent= new AxisValueRenderEvent( axisChart, 165 iAxisChartDataSet, 166 g2d, 167 totalItemArea, 168 axisChart.getXAxis().getZeroLineCoordinate() ); 169 170 171 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 173 { 174 for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ ) 175 { 176 g2d.setPaint( iAxisChartDataSet.getPaint( j ) ); 177 178 axisValueRenderEvent.setDataSetIndex( j ); 180 181 axisValueRenderEvent.setValueX( axisChart.getXAxis().getZeroLineCoordinate() ); 183 axisValueRenderEvent.setValueY( (float) rectangle.getCenterY() ); 184 axisValueRenderEvent.setValueIndex( i ); 185 186 clusteredBarChartProperties.firePreRender( axisValueRenderEvent ); 188 189 190 if( iAxisChartDataSet.getValue( j, i ) != 0.0d ) 192 { 193 194 if( iAxisChartDataSet.getValue( j, i ) < 0 ) 195 { 196 rectangle.x = axisChart.getXAxis().computeAxisCoordinate( axisChart.getXAxis().getOrigin(), 197 iAxisChartDataSet.getValue( j, i ), 198 axisChart.getXAxis().getScaleCalculator().getMinValue() ); 199 rectangle.width = startingX - rectangle.x; 200 201 axisValueRenderEvent.setValueX( rectangle.x ); 203 } 204 else 205 { 206 rectangle.x = startingX; 207 rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), 208 axisChart.getXAxis().getOneUnitPixelSize() ); 209 210 axisValueRenderEvent.setValueX( rectangle.x + rectangle.width ); 212 } 213 214 215 216 if( rectangle.width != 0 ) 218 { 219 g2d.fill( rectangle ); 220 221 if( clusteredBarChartProperties.getShowOutlinesFlag() ) 222 { 223 clusteredBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 224 } 225 226 227 if( axisChart.getGenerateImageMapFlag() ) 229 { 230 String label; 231 if( axisChart.getYAxis().getAxisLabelsGroup() != null ) 232 { 233 label= axisChart.getYAxis().getAxisLabelsGroup().getTextTag( imageMapLabelIndex ).getText(); 234 } 235 else 236 { 237 label= null; 238 } 239 240 axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle, 241 iAxisChartDataSet.getValue( j, i ), 242 label, 243 iAxisChartDataSet.getLegendLabel( j ) ) ); 244 } 245 } 246 } 247 248 clusteredBarChartProperties.firePostRender( axisValueRenderEvent ); 250 251 rectangle.y += barWidth; 252 } 253 254 imageMapLabelIndex--; 255 256 startingY += axisChart.getYAxis().getScalePixelWidth(); 257 rectangle.y = startingY; 258 259 totalItemArea.y+= axisChart.getYAxis().getScalePixelWidth(); 260 } 261 } 262 263 264 274 private static void verticalPlot( AxisChart axisChart, 275 IAxisChartDataSet iAxisChartDataSet, 276 BarChartProperties barChartProperties, 277 DataAxisProperties dataAxisProperties, 278 Graphics2D g2d, 279 Rectangle2D.Float rectangle, 280 float barGroupStartingX, 281 float startingY, 282 float barWidth ) 283 { 284 285 Rectangle2D.Float totalItemArea= new Rectangle2D.Float (); 287 totalItemArea.x= axisChart.getXAxis().getOrigin() + 1; 288 totalItemArea.y = axisChart.getYAxis().getOrigin() - axisChart.getYAxis().getPixelLength() + 1; 289 totalItemArea.width= axisChart.getXAxis().getScalePixelWidth() - 1; 290 totalItemArea.height= axisChart.getYAxis().getPixelLength() - 1; 291 292 293 AxisValueRenderEvent axisValueRenderEvent= new AxisValueRenderEvent( axisChart, 295 iAxisChartDataSet, 296 g2d, 297 totalItemArea, 298 axisChart.getYAxis().getZeroLineCoordinate() ); 299 300 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 302 { 303 for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ ) 304 { 305 g2d.setPaint( iAxisChartDataSet.getPaint( j ) ); 306 307 308 axisValueRenderEvent.setDataSetIndex( j ); 310 311 312 if( iAxisChartDataSet.getValue( j, i ) != 0.0d ) 314 { 315 axisValueRenderEvent.setValueX( (float) rectangle.getCenterX() ); 317 axisValueRenderEvent.setValueY( axisChart.getYAxis().getZeroLineCoordinate() ); 318 axisValueRenderEvent.setValueIndex( i ); 319 320 barChartProperties.firePreRender( axisValueRenderEvent ); 322 323 324 if( iAxisChartDataSet.getValue( j, i ) < 0 ) 325 { 326 rectangle.y = startingY; 327 rectangle.height = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getYAxis().getOneUnitPixelSize() ); 328 329 axisValueRenderEvent.setValueY( rectangle.y + rectangle.height ); 331 } 332 else 333 { 334 rectangle.y = axisChart.getYAxis().computeAxisCoordinate( axisChart.getYAxis().getOrigin(), 335 iAxisChartDataSet.getValue( j, i ), 336 axisChart.getYAxis().getScaleCalculator().getMinValue() ); 337 rectangle.height = startingY - rectangle.y; 338 339 axisValueRenderEvent.setValueY( rectangle.y ); 341 } 342 343 if( rectangle.height != 0 ) 345 { 346 g2d.fill( rectangle ); 347 348 if( barChartProperties.getShowOutlinesFlag() ) 349 { 350 barChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 351 } 352 353 if( axisChart.getGenerateImageMapFlag() ) 355 { 356 String label; 357 if( axisChart.getXAxis().getAxisLabelsGroup() != null ) 358 { 359 label= axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText(); 360 } 361 else 362 { 363 label= null; 364 } 365 366 axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle, 367 iAxisChartDataSet.getValue( j, i ), 368 label, 369 iAxisChartDataSet.getLegendLabel( j ) ) ); 370 } 371 } 372 373 barChartProperties.firePostRender( axisValueRenderEvent ); 375 } 376 377 rectangle.x += barWidth; 378 } 379 380 totalItemArea.x+= axisChart.getXAxis().getScalePixelWidth(); 381 382 barGroupStartingX += axisChart.getXAxis().getScalePixelWidth(); 383 rectangle.x = barGroupStartingX; 384 } 385 } 386 387 } 388 | Popular Tags |