1 34 35 package org.krysalis.jcharts.demo.userGuide; 36 37 38 import org.krysalis.jcharts.chartData.ChartDataException; 39 import org.krysalis.jcharts.chartData.DataSeries; 40 import org.krysalis.jcharts.chartData.AxisChartDataSet; 41 import org.krysalis.jcharts.axisChart.AxisChart; 42 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelPosition; 43 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.ValueLabelRenderer; 44 import org.krysalis.jcharts.axisChart.customRenderers.axisValue.renderers.BackgroundRenderer; 45 import org.krysalis.jcharts.properties.BarChartProperties; 46 import org.krysalis.jcharts.properties.LegendProperties; 47 import org.krysalis.jcharts.properties.AxisProperties; 48 import org.krysalis.jcharts.properties.ChartProperties; 49 import org.krysalis.jcharts.properties.ClusteredBarChartProperties; 50 import org.krysalis.jcharts.properties.StackedBarChartProperties; 51 import org.krysalis.jcharts.properties.util.ChartStroke; 52 import org.krysalis.jcharts.types.ChartType; 53 import org.krysalis.jcharts.test.TestDataGenerator; 54 55 import java.awt.*; 56 57 58 63 public class BarChartsGuide extends AxisChartsGuide 64 { 65 66 71 public void run() throws ChartDataException 72 { 73 this.barChart(); 74 this.stackedBars(); 75 this.clusteredBars(); 76 77 this.barLabels(); 78 this.barWidths(); 79 this.barOutlines(); 80 this.barBackground(); 81 } 82 83 84 88 private AxisChart getBarChart( BarChartProperties barChartProperties ) throws ChartDataException 89 { 90 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 91 String xAxisTitle= "Years"; 92 String yAxisTitle= "Problems"; 93 String title= "Micro$oft at Work"; 94 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 95 96 double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } }; 97 String [] legendLabels= { "Bugs" }; 98 Paint[] paints= TestDataGenerator.getRandomPaints( 1 ); 99 100 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties ); 101 102 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 103 104 ChartProperties chartProperties= new ChartProperties(); 105 AxisProperties axisProperties= new AxisProperties(); 106 LegendProperties legendProperties= new LegendProperties(); 107 108 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 109 110 return axisChart; 111 } 112 113 114 147 private void barChart() throws ChartDataException 148 { 149 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 150 String xAxisTitle= "Years"; 151 String yAxisTitle= "Problems"; 152 String title= "Micro$oft at Work"; 153 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 154 155 double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } }; 156 String [] legendLabels= { "Bugs" }; 157 Paint[] paints= new Paint[]{ Color.blue.darker() }; 158 BarChartProperties barChartProperties= new BarChartProperties(); 159 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties ); 160 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 161 162 ChartProperties chartProperties= new ChartProperties(); 163 AxisProperties axisProperties= new AxisProperties(); 164 LegendProperties legendProperties= new LegendProperties(); 165 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 166 167 super.exportImage( axisChart, "barChart" ); 168 } 169 170 171 172 private void barLabels() throws ChartDataException 173 { 174 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 175 String xAxisTitle= "Years"; 176 String yAxisTitle= "Problems"; 177 String title= "Micro$oft at Work"; 178 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 179 180 double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } }; 181 String [] legendLabels= { "Bugs" }; 182 Paint[] paints= new Paint[]{ Color.yellow }; 183 BarChartProperties barChartProperties= new BarChartProperties(); 184 185 ValueLabelRenderer valueLabelRenderer = new ValueLabelRenderer( false, false, true, -1 ); 186 valueLabelRenderer.setValueLabelPosition( ValueLabelPosition.AT_TOP ); 187 valueLabelRenderer.useVerticalLabels( false ); 188 barChartProperties.addPostRenderEventListener( valueLabelRenderer ); 189 190 191 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties ); 192 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 193 194 ChartProperties chartProperties= new ChartProperties(); 195 AxisProperties axisProperties= new AxisProperties( true ); 196 LegendProperties legendProperties= new LegendProperties(); 197 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 198 199 super.exportImage( axisChart, "barLabels" ); 200 } 201 202 203 204 205 private void barBackground() throws ChartDataException 206 { 207 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 208 String xAxisTitle= "Years"; 209 String yAxisTitle= "Problems"; 210 String title= "Micro$oft at Work"; 211 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 212 213 double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 } }; 214 String [] legendLabels= { "Bugs" }; 215 Paint[] paints= new Paint[]{ Color.blue.darker() }; 216 BarChartProperties barChartProperties= new BarChartProperties(); 217 218 BackgroundRenderer backgroundRenderer = new BackgroundRenderer( new Color( 20, 20, 20, 50 ) ); 220 barChartProperties.addPreRenderEventListener( backgroundRenderer ); 221 222 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, barChartProperties ); 223 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 224 225 ChartProperties chartProperties= new ChartProperties(); 226 AxisProperties axisProperties= new AxisProperties(); 227 LegendProperties legendProperties= new LegendProperties(); 228 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 229 230 super.exportImage( axisChart, "barBackground" ); 231 } 232 233 234 235 private void stackedBars() throws ChartDataException 236 { 237 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 238 String xAxisTitle= "Years"; 239 String yAxisTitle= "Problems"; 240 String title= "Micro$oft at Work"; 241 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 242 243 double[][] data= new double[][]{ { 250, 45, 36, 66, 145, 80, 55 }, { 150, 15, 6, 62, 54, 10, 84 }, { 250, 45, 36, 66, 145, 80, 55 } }; 244 String [] legendLabels= { "Bugs", "Security Holes", "Backdoors" }; 245 Paint[] paints= TestDataGenerator.getRandomPaints( 3 ); 246 StackedBarChartProperties stackedBarChartProperties= new StackedBarChartProperties(); 247 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_STACKED, stackedBarChartProperties ); 248 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 249 250 ChartProperties chartProperties= new ChartProperties(); 251 AxisProperties axisProperties= new AxisProperties(); 252 LegendProperties legendProperties= new LegendProperties(); 253 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 254 255 super.exportImage( axisChart, "stackedBarChart" ); 256 } 257 258 259 260 private void clusteredBars() throws ChartDataException 261 { 262 String [] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 263 String xAxisTitle= "Years"; 264 String yAxisTitle= "Problems"; 265 String title= "Micro$oft at Work"; 266 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 267 268 double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55 }, { 150, 15, 6, 62, -54, 10, 84 }, { 20, 145, 36, 6, 45, 18, 5 } }; 269 String [] legendLabels= { "Bugs", "Security Holes", "Backdoors" }; 270 Paint[] paints= TestDataGenerator.getRandomPaints( 3 ); 271 ClusteredBarChartProperties clusteredBarChartProperties= new ClusteredBarChartProperties(); 272 AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR_CLUSTERED, clusteredBarChartProperties ); 273 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 274 275 ChartProperties chartProperties= new ChartProperties(); 276 AxisProperties axisProperties= new AxisProperties(); 277 LegendProperties legendProperties= new LegendProperties(); 278 AxisChart axisChart= new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 279 280 super.exportImage( axisChart, "clusteredBarChart" ); 281 } 282 283 284 285 286 private void barWidths() throws ChartDataException 287 { 288 BarChartProperties barChartProperties= new BarChartProperties(); 289 barChartProperties.setWidthPercentage( 1f ); 290 291 AxisChart axisChart= this.getBarChart( barChartProperties ); 292 293 super.exportImage( axisChart, "barChartWidths" ); 294 } 295 296 297 private void barOutlines() throws ChartDataException 298 { 299 BarChartProperties barChartProperties= new BarChartProperties(); 300 barChartProperties.setShowOutlinesFlag( true ); 301 ChartStroke outlineChartStroke= new ChartStroke( new BasicStroke( 2.0f ), Color.red ); 302 barChartProperties.setBarOutlineStroke( outlineChartStroke ); 303 304 AxisChart axisChart= this.getBarChart( barChartProperties ); 305 306 super.exportImage( axisChart, "barChartOutlines" ); 307 } 308 309 310 311 } 312 313 | Popular Tags |