1 34 35 36 package org.krysalis.jcharts.axisChart; 37 38 39 import org.krysalis.jcharts.chartData.ChartDataException; 40 import org.krysalis.jcharts.chartData.interfaces.IAxisChartDataSet; 41 import org.krysalis.jcharts.imageMap.RectMapArea; 42 import org.krysalis.jcharts.properties.DataAxisProperties; 43 import org.krysalis.jcharts.properties.StackedBarChartProperties; 44 45 import java.awt.*; 46 import java.awt.geom.Rectangle2D ; 47 48 49 54 abstract class StackedBarChart 55 { 56 57 62 static void render( AxisChart axisChart, IAxisChartDataSet iAxisChartDataSet ) throws ChartDataException 63 { 64 Graphics2D g2d = axisChart.getGraphics2D(); 65 StackedBarChartProperties stackedBarChartProperties = (StackedBarChartProperties) iAxisChartDataSet.getChartTypeProperties(); 66 67 68 float barWidth; 69 70 float startingX; 72 float startingY; 73 float width; 74 float height; 75 76 77 DataAxisProperties dataAxisProperties; 78 79 80 if( axisChart.getAxisProperties().isPlotHorizontal() ) 81 { 82 dataAxisProperties = (DataAxisProperties) axisChart.getAxisProperties().getXAxisProperties(); 83 barWidth = axisChart.getYAxis().getScalePixelWidth() * stackedBarChartProperties.getPercentage(); 84 85 startingX = axisChart.getXAxis().getZeroLineCoordinate(); 86 startingY = axisChart.getYAxis().getLastTickY() - (barWidth / 2); 87 width = 0; 88 height = barWidth; 89 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 90 91 StackedBarChart.horizontalPlot( axisChart, iAxisChartDataSet, stackedBarChartProperties, dataAxisProperties, g2d, rectangle, startingX ); 92 } 93 else 94 { 95 dataAxisProperties = (DataAxisProperties) axisChart.getAxisProperties().getYAxisProperties(); 96 barWidth = axisChart.getXAxis().getScalePixelWidth() * stackedBarChartProperties.getPercentage(); 97 98 startingX = axisChart.getXAxis().getTickStart() - (barWidth / 2); 99 startingY = axisChart.getYAxis().getZeroLineCoordinate(); 100 width = barWidth; 101 height = 0; 102 Rectangle2D.Float rectangle = new Rectangle2D.Float ( startingX, startingY, width, height ); 103 104 StackedBarChart.verticalPlot( axisChart, iAxisChartDataSet, stackedBarChartProperties, dataAxisProperties, g2d, rectangle, startingY ); 105 } 106 } 107 108 109 119 private static void horizontalPlot( AxisChart axisChart, 120 IAxisChartDataSet iAxisChartDataSet, 121 StackedBarChartProperties stackedBarChartProperties, 122 DataAxisProperties dataAxisProperties, 123 Graphics2D g2d, 124 Rectangle2D.Float rectangle, 125 float startingX ) throws ChartDataException 126 { 127 int imageMapLabelIndex = axisChart.getYAxis().getNumberOfScaleItems() - 1; 128 129 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 132 { 133 for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ ) 135 { 136 if( iAxisChartDataSet.getValue( j, i ) == 0 ) 138 { 139 continue; 140 } 141 else if( iAxisChartDataSet.getValue( j, i ) < 0 ) 142 { 143 throw new ChartDataException( "Negative values in Stacked Bar charts are not supported yet... Coming soon..." ); 145 151 } 152 else 153 { 154 155 rectangle.width = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getXAxis().getOneUnitPixelSize() ); 156 157 158 } 161 162 163 g2d.setPaint( iAxisChartDataSet.getPaint( j ) ); 164 g2d.fill( rectangle ); 165 166 if( stackedBarChartProperties.getShowOutlinesFlag() ) 167 { 168 stackedBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 169 } 170 171 172 if( axisChart.getGenerateImageMapFlag() ) 174 { 175 String label = null; 176 if( axisChart.getYAxis().getAxisLabelsGroup() != null ) 177 { 178 label = axisChart.getYAxis().getAxisLabelsGroup().getTextTag( imageMapLabelIndex ).getText(); 179 } 180 axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle, 181 iAxisChartDataSet.getValue( j, i ), 182 label, 183 iAxisChartDataSet.getLegendLabel( j ) ) ); 184 } 185 186 rectangle.x += rectangle.width; 187 } 188 189 imageMapLabelIndex--; 190 191 rectangle.y += axisChart.getYAxis().getScalePixelWidth(); 192 rectangle.x = startingX; 193 } 194 } 195 196 197 207 private static void verticalPlot( AxisChart axisChart, 208 IAxisChartDataSet iAxisChartDataSet, 209 StackedBarChartProperties stackedBarChartProperties, 210 DataAxisProperties dataAxisProperties, 211 Graphics2D g2d, 212 Rectangle2D.Float rectangle, 213 float startingY ) 214 { 215 217 for( int i = 0; i < iAxisChartDataSet.getNumberOfDataItems(); i++ ) 220 { 221 for( int j = 0; j < iAxisChartDataSet.getNumberOfDataSets(); j++ ) 223 { 224 if( iAxisChartDataSet.getValue( j, i ) == 0 ) 226 { 227 continue; 228 } 229 230 rectangle.height = BarChart.computeScaleHeightOfValue( iAxisChartDataSet.getValue( j, i ), axisChart.getYAxis().getOneUnitPixelSize() ); 231 rectangle.y -= rectangle.height; 232 233 234 g2d.setPaint( iAxisChartDataSet.getPaint( j ) ); 235 g2d.fill( rectangle ); 236 237 if( stackedBarChartProperties.getShowOutlinesFlag() ) 238 { 239 stackedBarChartProperties.getBarOutlineStroke().draw( g2d, rectangle ); 240 } 241 242 243 if( axisChart.getGenerateImageMapFlag() ) 245 { 246 String label = null; 247 if( axisChart.getXAxis().getAxisLabelsGroup() != null ) 248 { 249 label = axisChart.getXAxis().getAxisLabelsGroup().getTextTag( i ).getText(); 250 } 251 axisChart.getImageMap().addImageMapArea( new RectMapArea( rectangle, 252 iAxisChartDataSet.getValue( j, i ), 253 label, 254 iAxisChartDataSet.getLegendLabel( j ) ) ); 255 } 256 257 } 258 259 rectangle.x += axisChart.getXAxis().getScalePixelWidth(); 260 rectangle.y = startingY; 261 } 262 } 263 264 } 265 | Popular Tags |