1 34 35 36 package org.krysalis.jcharts.demo.userGuide; 37 38 39 import org.krysalis.jcharts.chartData.DataSeries; 40 import org.krysalis.jcharts.chartData.ChartDataException; 41 import org.krysalis.jcharts.chartData.AxisChartDataSet; 42 import org.krysalis.jcharts.axisChart.AxisChart; 43 import org.krysalis.jcharts.properties.AxisProperties; 44 import org.krysalis.jcharts.properties.AreaChartProperties; 45 import org.krysalis.jcharts.properties.ChartProperties; 46 import org.krysalis.jcharts.properties.LegendProperties; 47 import org.krysalis.jcharts.properties.PropertyException; 48 import org.krysalis.jcharts.properties.BarChartProperties; 49 import org.krysalis.jcharts.properties.DataAxisProperties; 50 import org.krysalis.jcharts.properties.AxisTypeProperties; 51 import org.krysalis.jcharts.properties.ChartTypeProperties; 52 import org.krysalis.jcharts.properties.util.ChartStroke; 53 import org.krysalis.jcharts.properties.util.ChartFont; 54 import org.krysalis.jcharts.types.ChartType; 55 import org.krysalis.jcharts.test.TestDataGenerator; 56 57 import java.awt.*; 58 59 60 65 public class AxisChartsGuide extends UserGuideBase 66 { 67 public static final int width = 500; 68 public static final int height = 320; 69 70 71 75 private AxisChart getChart( AxisProperties axisProperties ) throws ChartDataException 76 { 77 String [] xAxisLabels = {"1998", "1999", "2000", "2001", "2002", "2003", "2004"}; 78 String xAxisTitle = "Years"; 79 String yAxisTitle = "Problems"; 80 String title = "Micro$oft at Work"; 81 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 82 83 dataSeries.addIAxisPlotDataSet( AxisChartsGuide.createAxisChartDataSet( ChartType.AREA, new AreaChartProperties(), 200, 5000 ) ); 84 85 ChartProperties chartProperties = new ChartProperties(); 86 LegendProperties legendProperties = new LegendProperties(); 87 88 AxisChart axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, legendProperties, AxisChartsGuide.width, AxisChartsGuide.height ); 89 90 return axisChart; 91 } 92 93 94 99 public void run() throws ChartDataException, PropertyException 100 { 101 this.horizontalPlots(); 102 103 104 this.rounding(); 105 this.userDefinedScale(); 106 107 this.numberOfItemsOnYAxis(); 108 this.gridLines(); 109 110 this.dollarSigns(); 111 this.commas(); 112 113 this.axisBackgroundPaint(); 114 this.axisTitles(); 115 116 this.zeroLine(); 117 this.scaleFont(); 118 119 this.tickMarks(); 120 121 this.verticalXAxisLabels(); 122 } 123 124 125 126 private void horizontalPlots() throws ChartDataException 127 { 128 AxisProperties axisProperties = new AxisProperties( true ); 129 130 double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d}}; 131 String [] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August"}; 132 String [] legendLabels = {"New Bugs in Windows Per Month"}; 133 Paint[] paints = {Color.blue}; 134 String xAxisTitle = "Months"; 135 String yAxisTitle = "Bugs"; 136 String title = "Micro$oft At Work"; 137 138 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 139 AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.BAR, new BarChartProperties() ); 140 141 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 142 143 AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height ); 144 super.exportImage( axisChart, "horizontalPlots" ); 145 } 146 147 148 149 private void rounding() throws ChartDataException 150 { 151 AxisProperties axisProperties = new AxisProperties(); 152 153 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 154 dataAxisProperties.setRoundToNearest( -2 ); 155 156 super.exportImage( this.getChart( axisProperties ), "axisRounding" ); 157 } 158 159 160 161 private void userDefinedScale() throws ChartDataException, PropertyException 162 { 163 AxisProperties axisProperties = new AxisProperties(); 164 165 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 166 dataAxisProperties.setUserDefinedScale( -2000, 850 ); 167 dataAxisProperties.setRoundToNearest( 0 ); 168 169 super.exportImage( this.getChart( axisProperties ), "userDefinedScale" ); 170 } 171 172 174 175 private void numberOfItemsOnYAxis() throws ChartDataException 176 { 177 AxisProperties axisProperties = new AxisProperties(); 178 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 179 dataAxisProperties.setNumItems( 8 ); 180 181 super.exportImage( this.getChart( axisProperties ), "numberOfItemsOnYAxis" ); 182 } 183 184 185 186 private void gridLines() throws ChartDataException 187 { 188 AxisProperties axisProperties = new AxisProperties(); 189 190 ChartStroke xAxisGridLines = new ChartStroke( new BasicStroke( 1.0f ), Color.red ); 191 axisProperties.getXAxisProperties().setGridLineChartStroke( xAxisGridLines ); 192 axisProperties.getXAxisProperties().setShowGridLines( AxisTypeProperties.GRID_LINES_ONLY_WITH_LABELS ); 193 194 axisProperties.getYAxisProperties().setShowGridLines( AxisTypeProperties.GRID_LINES_NONE ); 195 196 197 double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d, 3000.4354d, 1268.0001d, 2444.432d, 5003d}}; 198 String [] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 199 String [] legendLabels = {"New Bugs in Windows Per Month"}; 200 Paint[] paints = {Color.blue}; 201 String xAxisTitle = "Months"; 202 String yAxisTitle = "Bugs"; 203 String title = "Micro$oft At Work"; 204 205 DataSeries dataSeries = new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, title ); 206 AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.AREA, new AreaChartProperties() ); 207 208 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 209 210 AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height ); 211 super.exportImage( axisChart, "gridLines" ); 212 } 213 214 215 216 private void dollarSigns() throws ChartDataException 217 { 218 AxisProperties axisProperties = new AxisProperties(); 219 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 220 dataAxisProperties.setUseDollarSigns( true ); 221 super.exportImage( this.getChart( axisProperties ), "dollarSigns" ); 222 } 223 224 225 226 private void commas() throws ChartDataException 227 { 228 AxisProperties axisProperties = new AxisProperties(); 229 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 230 dataAxisProperties.setUseCommas( false ); 231 super.exportImage( this.getChart( axisProperties ), "commas" ); 232 } 233 234 235 236 private void axisBackgroundPaint() throws ChartDataException 237 { 238 AxisProperties axisProperties = new AxisProperties(); 239 axisProperties.setBackgroundPaint( Color.yellow ); 240 super.exportImage( this.getChart( axisProperties ), "axisBackground" ); 241 } 242 243 244 245 private void axisTitles() throws ChartDataException 246 { 247 double[][] data = {{3444d, 1506.3d, 2777d, 2550.345d, 659.667d, 950.6644d, 4500.3453d, 1200.67583d, 3000.4354d, 1268.0001d, 2444.432d, 5003d}}; 248 String [] xAxisLabels = {"January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; 249 String [] legendLabels = {"New Bugs in Windows Per Month"}; 250 Paint[] paints = {Color.blue}; 251 String xAxisTitle = "Months"; 252 String yAxisTitle = "Bugs"; 253 String title = "Micro$oft At Work"; 254 AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, paints, ChartType.AREA, new AreaChartProperties() ); 255 256 257 DataSeries dataSeries = new DataSeries( xAxisLabels, null, yAxisTitle, title ); 258 dataSeries.addIAxisPlotDataSet( axisChartDataSet ); 259 260 AxisChart axisChart = new AxisChart( dataSeries, new ChartProperties(), new AxisProperties(), new LegendProperties(), width, height ); 261 super.exportImage( axisChart, "axisTitles" ); 262 263 AxisProperties axisProperties = new AxisProperties(); 264 ChartFont yAxisFont = new ChartFont( new Font( "Arial Narrow", Font.BOLD, 14 ), Color.blue ); 265 axisProperties.getYAxisProperties().setAxisTitleChartFont( yAxisFont ); 266 267 axisChart = new AxisChart( dataSeries, new ChartProperties(), axisProperties, new LegendProperties(), width, height ); 268 super.exportImage( axisChart, "axisTitleFont" ); 269 } 270 271 272 273 private void zeroLine() throws ChartDataException, PropertyException 274 { 275 AxisProperties axisProperties = new AxisProperties(); 276 277 DataAxisProperties dataAxisProperties = (DataAxisProperties) axisProperties.getYAxisProperties(); 278 dataAxisProperties.setUserDefinedScale( -2000, 1200 ); 279 dataAxisProperties.setShowZeroLine( false ); 280 super.exportImage( this.getChart( axisProperties ), "noZeroLine" ); 281 282 dataAxisProperties.setShowZeroLine( true ); 283 284 BasicStroke stroke = new BasicStroke( 1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 5f, new float[]{5f, 5f, 10f, 5f}, 4f ); 285 ChartStroke zeroLineChartStroke = new ChartStroke( stroke, Color.red ); 286 dataAxisProperties.setZeroLineChartStroke( zeroLineChartStroke ); 287 super.exportImage( this.getChart( axisProperties ), "zeroLinePaintStroke" ); 288 } 289 290 291 292 private void scaleFont() throws ChartDataException 293 { 294 AxisProperties axisProperties = new AxisProperties(); 295 ChartFont xScaleChartFont = new ChartFont( new Font( "Georgia Negreta cursiva", Font.PLAIN, 13 ), Color.blue ); 296 axisProperties.getXAxisProperties().setScaleChartFont( xScaleChartFont ); 297 super.exportImage( this.getChart( axisProperties ), "scaleFont" ); 298 } 299 300 301 302 private void verticalXAxisLabels() throws ChartDataException 303 { 304 AxisProperties axisProperties = new AxisProperties(); 305 axisProperties.setXAxisLabelsAreVertical( true ); 306 super.exportImage( this.getChart( axisProperties ), "verticalXAxisLabels" ); 307 } 308 309 310 311 private void tickMarks() throws ChartDataException 312 { 313 AxisProperties axisProperties = new AxisProperties(); 314 axisProperties.getXAxisProperties().setShowTicks( AxisTypeProperties.TICKS_ONLY_WITH_LABELS ); 315 super.exportImage( this.getChart( axisProperties ), "ticksWithLabels" ); 316 317 318 axisProperties = new AxisProperties(); 319 axisProperties.getXAxisProperties().setShowTicks( AxisTypeProperties.TICKS_NONE ); 320 super.exportImage( this.getChart( axisProperties ), "noTicks" ); 321 322 323 axisProperties = new AxisProperties(); 324 ChartStroke xTicks = new ChartStroke( new BasicStroke( 1.0f ), Color.blue ); 325 axisProperties.getXAxisProperties().setTickChartStroke( xTicks ); 326 327 ChartStroke yTicks = new ChartStroke( new BasicStroke( 1.0f ), Color.green ); 328 axisProperties.getYAxisProperties().setTickChartStroke( yTicks ); 329 330 super.exportImage( this.getChart( axisProperties ), "tickColors" ); 331 332 333 axisProperties = new AxisProperties(); 334 ChartStroke xTickStroke = new ChartStroke( new BasicStroke( 2.0f ), Color.blue ); 335 axisProperties.getXAxisProperties().setTickChartStroke( xTickStroke ); 336 super.exportImage( this.getChart( axisProperties ), "tickStroke" ); 337 } 338 339 340 344 public static final DataSeries createDataSeries() 345 { 346 String [] xAxisLabels = { "1998", "1999", "2000", "2001", "2002", "2003", "2004" }; 347 String xAxisTitle = "Years"; 348 String yAxisTitle = "Problems"; 349 String title = null; 350 351 return new DataSeries( xAxisLabels, xAxisTitle, yAxisTitle, "Micro$oft At Work" ); 352 } 353 354 355 362 public static final AxisChartDataSet createAxisChartDataSet( ChartType chartType, 363 ChartTypeProperties chartTypeProperties, 364 int numberOfDataSets, 365 String [] legendLabels, 366 int minValue, 367 int maxValue ) throws ChartDataException 368 { 369 double[][] data = TestDataGenerator.getRandomNumbers( numberOfDataSets, 7, minValue, maxValue ); 370 Paint[] paints = TestDataGenerator.getRandomPaints( numberOfDataSets ); 371 372 return new AxisChartDataSet( data, legendLabels, paints, chartType, chartTypeProperties ); 373 } 374 375 376 383 public static final AxisChartDataSet createAxisChartDataSet( ChartType chartType, 384 ChartTypeProperties chartTypeProperties, 385 int minValue, 386 int maxValue ) throws ChartDataException 387 { 388 double[][] data = TestDataGenerator.getRandomNumbers( 1, 7, minValue, maxValue ); 389 String [] legendLabels = { "Bugs" }; 390 Paint[] paints = TestDataGenerator.getRandomPaints( 1 ); 391 392 return new AxisChartDataSet( data, legendLabels, paints, chartType, chartTypeProperties ); 393 } 394 395 } 396 397 398 399 400 401 | Popular Tags |