1 38 39 package org.jfree.chart.demo; 40 41 import java.awt.Color ; 42 import java.awt.GradientPaint ; 43 44 import javax.swing.JFrame ; 45 46 import org.jfree.chart.ChartFrame; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.axis.NumberAxis; 49 import org.jfree.chart.axis.SymbolicAxis; 50 import org.jfree.chart.axis.ValueAxis; 51 import org.jfree.chart.labels.SymbolicXYToolTipGenerator; 52 import org.jfree.chart.plot.CombinedDomainXYPlot; 53 import org.jfree.chart.plot.CombinedRangeXYPlot; 54 import org.jfree.chart.plot.XYPlot; 55 import org.jfree.chart.renderer.StandardXYItemRenderer; 56 import org.jfree.chart.renderer.XYItemRenderer; 57 import org.jfree.data.CombinedDataset; 58 import org.jfree.data.SubSeriesDataset; 59 import org.jfree.data.XYDataset; 60 import org.jfree.data.YisSymbolic; 61 import org.jfree.ui.RefineryUtilities; 62 63 68 public class SymbolicYPlotDemo { 69 70 81 90 private static void displayYSymbolic(String frameTitle, 91 XYDataset data, String chartTitle, 92 String xAxisLabel, String yAxisLabel) { 93 94 JFreeChart chart = createYSymbolicPlot(chartTitle, xAxisLabel, yAxisLabel, data, true); 95 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 1000, 0, Color.green)); 96 97 JFrame frame = new ChartFrame(frameTitle, chart); 98 frame.pack(); 99 RefineryUtilities.positionFrameRandomly(frame); 100 frame.show(); 101 102 } 103 104 111 private static void displayYSymbolicOverlaid(String frameTitle, 112 XYDataset data1, XYDataset data2) { 113 114 String title = "Animals Overlaid"; 115 String xAxisLabel = "Miles"; 116 String yAxisLabel = "Animal"; 117 118 String [] combinedYSymbolicValues 120 = SampleYSymbolicDataset.combineYSymbolicDataset((YisSymbolic) data1, 121 (YisSymbolic) data2); 122 123 CombinedDataset data = new CombinedDataset(); 125 data.add(data1); 126 data.add(data2); 127 128 XYDataset series0 = new SubSeriesDataset(data, 0); 130 XYDataset series1 = new SubSeriesDataset(data, 1); 131 XYDataset series2 = new SubSeriesDataset(data, 2); 132 XYDataset series3 = new SubSeriesDataset(data, 3); 133 XYDataset series4 = new SubSeriesDataset(data, 4); 134 XYDataset series5 = new SubSeriesDataset(data, 5); 135 XYDataset series6 = new SubSeriesDataset(data, 6); 136 XYDataset series7 = new SubSeriesDataset(data, 7); 137 138 ValueAxis valueAxis = new NumberAxis(xAxisLabel); 140 SymbolicAxis symbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues); 141 XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 142 XYPlot plot = new XYPlot(series0, valueAxis, symbolicAxis, renderer); 143 144 plot.setSecondaryDataset(0, series1); 145 XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 146 plot.setSecondaryRenderer(0, renderer1); 147 148 plot.setSecondaryDataset(1, series2); 149 XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 150 plot.setSecondaryRenderer(1, renderer2); 151 152 plot.setSecondaryDataset(2, series3); 153 XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 154 plot.setSecondaryRenderer(2, renderer3); 155 156 plot.setSecondaryDataset(3, series4); 157 XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 158 plot.setSecondaryRenderer(3, renderer4); 159 160 plot.setSecondaryDataset(4, series5); 161 XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 162 plot.setSecondaryRenderer(4, renderer5); 163 164 plot.setSecondaryDataset(5, series6); 165 XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 166 plot.setSecondaryRenderer(5, renderer6); 167 168 plot.setSecondaryDataset(6, series7); 169 XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 170 plot.setSecondaryRenderer(6, renderer7); 171 172 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true); 174 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 175 176 JFrame frame = new ChartFrame(frameTitle, chart); 178 frame.pack(); 179 RefineryUtilities.positionFrameRandomly(frame); 180 frame.show(); 181 182 } 183 184 191 private static void displayYSymbolicCombinedHorizontally(String frameTitle, 192 SampleYSymbolicDataset data1, 193 SampleYSymbolicDataset data2) { 194 195 String title = "Animals Horizontally Combined"; 196 String xAxisLabel = "Miles"; 197 String yAxisLabel = null; 198 199 String [] combinedYSymbolicValues 201 = SampleYSymbolicDataset.combineYSymbolicDataset(data1, data2); 202 203 CombinedDataset data = new CombinedDataset(); 205 data.add(data1); 206 data.add(data2); 207 208 XYDataset series0 = new SubSeriesDataset(data, 0); 210 XYDataset series1 = new SubSeriesDataset(data, 1); 211 XYDataset series2 = new SubSeriesDataset(data, 2); 212 XYDataset series3 = new SubSeriesDataset(data, 3); 213 XYDataset series4 = new SubSeriesDataset(data, 4); 214 XYDataset series5 = new SubSeriesDataset(data, 5); 215 XYDataset series6 = new SubSeriesDataset(data, 6); 216 XYDataset series7 = new SubSeriesDataset(data, 7); 217 218 ValueAxis valueAxis0 = new NumberAxis(xAxisLabel); 220 ValueAxis valueAxis1 = new NumberAxis(xAxisLabel); 221 ValueAxis valueAxis2 = new NumberAxis(xAxisLabel); 222 ValueAxis valueAxis3 = new NumberAxis(xAxisLabel); 223 ValueAxis valueAxis4 = new NumberAxis(xAxisLabel); 224 ValueAxis valueAxis5 = new NumberAxis(xAxisLabel); 225 ValueAxis valueAxis6 = new NumberAxis(xAxisLabel); 226 ValueAxis valueAxis7 = new NumberAxis(xAxisLabel); 227 SymbolicAxis symbolicAxis = new SymbolicAxis(yAxisLabel, combinedYSymbolicValues); 228 229 CombinedRangeXYPlot mainPlot = new CombinedRangeXYPlot(symbolicAxis); 231 232 XYItemRenderer renderer0 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 234 XYPlot subplot0 = new XYPlot(series0, valueAxis0, null, renderer0); 235 XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 236 XYPlot subplot1 = new XYPlot(series1, valueAxis1, null, renderer1); 237 XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 238 XYPlot subplot2 = new XYPlot(series2, valueAxis2, null, renderer2); 239 XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 240 XYPlot subplot3 = new XYPlot(series3, valueAxis3, null, renderer3); 241 XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 242 XYPlot subplot4 = new XYPlot(series4, valueAxis4, null, renderer4); 243 XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 244 XYPlot subplot5 = new XYPlot(series5, valueAxis5, null, renderer5); 245 XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 246 XYPlot subplot6 = new XYPlot(series6, valueAxis6, null, renderer6); 247 XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 248 XYPlot subplot7 = new XYPlot(series7, valueAxis7, null, renderer7); 249 250 mainPlot.add(subplot0, 1); 251 mainPlot.add(subplot1, 1); 252 mainPlot.add(subplot2, 1); 253 mainPlot.add(subplot3, 1); 254 mainPlot.add(subplot4, 1); 255 mainPlot.add(subplot5, 1); 256 mainPlot.add(subplot6, 1); 257 mainPlot.add(subplot7, 1); 258 259 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true); 261 262 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 264 265 JFrame ySymbolicFrame = new ChartFrame(frameTitle, chart); 267 ySymbolicFrame.pack(); 268 RefineryUtilities.positionFrameRandomly(ySymbolicFrame); 269 ySymbolicFrame.show(); 270 271 } 272 273 280 private static void displayYSymbolicCombinedVertically(String frameTitle, 281 SampleYSymbolicDataset data1, 282 SampleYSymbolicDataset data2) { 283 284 String title = "Animals Vertically Combined"; 285 String xAxisLabel = "Miles"; 286 String yAxisLabel = null; 287 288 CombinedDataset data = new CombinedDataset(); 290 data.add(data1); 291 data.add(data2); 292 293 XYDataset series0 = new SubSeriesDataset(data, 0); 295 XYDataset series1 = new SubSeriesDataset(data, 1); 296 XYDataset series2 = new SubSeriesDataset(data, 2); 297 XYDataset series3 = new SubSeriesDataset(data, 3); 298 XYDataset series4 = new SubSeriesDataset(data, 4); 299 XYDataset series5 = new SubSeriesDataset(data, 5); 300 XYDataset series6 = new SubSeriesDataset(data, 6); 301 XYDataset series7 = new SubSeriesDataset(data, 7); 302 303 ValueAxis valueAxis = new NumberAxis(xAxisLabel); 305 SymbolicAxis symbolicAxis1 306 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data1).getYSymbolicValues()); 307 SymbolicAxis symbolicAxis2 308 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data2).getYSymbolicValues()); 309 310 CombinedDomainXYPlot mainPlot = new CombinedDomainXYPlot(valueAxis); 312 313 XYItemRenderer renderer0 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 315 XYPlot subplot0 = new XYPlot(series0, null, symbolicAxis1, renderer0); 316 XYItemRenderer renderer1 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 317 XYPlot subplot1 = new XYPlot(series1, null, symbolicAxis1, renderer1); 318 XYItemRenderer renderer2 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 319 XYPlot subplot2 = new XYPlot(series2, null, symbolicAxis1, renderer2); 320 XYItemRenderer renderer3 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 321 XYPlot subplot3 = new XYPlot(series3, null, symbolicAxis1, renderer3); 322 XYItemRenderer renderer4 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 323 XYPlot subplot4 = new XYPlot(series4, null, symbolicAxis2, renderer4); 324 XYItemRenderer renderer5 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 325 XYPlot subplot5 = new XYPlot(series5, null, symbolicAxis2, renderer5); 326 XYItemRenderer renderer6 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 327 XYPlot subplot6 = new XYPlot(series6, null, symbolicAxis2, renderer6); 328 XYItemRenderer renderer7 = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null); 329 XYPlot subplot7 = new XYPlot(series7, null, symbolicAxis2, renderer7); 330 331 mainPlot.add(subplot0, 1); 333 mainPlot.add(subplot1, 1); 334 mainPlot.add(subplot2, 1); 335 mainPlot.add(subplot3, 1); 336 mainPlot.add(subplot4, 1); 337 mainPlot.add(subplot5, 1); 338 mainPlot.add(subplot6, 1); 339 mainPlot.add(subplot7, 1); 340 341 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true); 343 chart.setBackgroundPaint(new GradientPaint (0, 0, Color.white, 0, 1000, Color.blue)); 344 345 JFrame frame = new ChartFrame(frameTitle, chart); 347 frame.pack(); 348 RefineryUtilities.positionFrameRandomly(frame); 349 frame.show(); 350 351 } 352 353 364 public static JFreeChart createYSymbolicPlot(String title, String xAxisLabel, 365 String yAxisLabel, XYDataset data, 366 boolean legend) { 367 368 ValueAxis valueAxis = new NumberAxis(xAxisLabel); 369 SymbolicAxis symbolicAxis 370 = new SymbolicAxis(yAxisLabel, ((YisSymbolic) data).getYSymbolicValues()); 371 372 XYPlot plot = new XYPlot(data, valueAxis, symbolicAxis, null); 373 XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, 374 new SymbolicXYToolTipGenerator()); 375 plot.setRenderer(renderer); 376 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 377 return chart; 378 379 } 380 381 386 public static SampleYSymbolicDataset createYSymbolicSample1() { 387 388 String [] sData = {"Lion", "Elephant", "Monkey", "Hippopotamus", "Giraffe"}; 389 SampleYSymbolicDataset data 390 = new SampleYSymbolicDataset("AY Sample", 20, sData, 4, 20, 391 new String [] {"A Fall", "A Spring", "A Summer", "A Winter"}); 392 return data; 393 394 } 395 396 401 public static SampleYSymbolicDataset createYSymbolicSample2() { 402 403 String [] sData = {"Giraffe", "Gazelle", "Zebra", "Gnu"}; 404 SampleYSymbolicDataset data 405 = new SampleYSymbolicDataset("BY Sample", 40, sData, 4, 20, 406 new String [] {"B Fall", "B Spring", "B Summer", "B Winter"}); 407 return data; 408 409 } 410 411 416 public static void main(String [] args) { 417 418 SampleYSymbolicDataset s1 = createYSymbolicSample1(); 419 SampleYSymbolicDataset s2 = createYSymbolicSample2(); 420 421 displayYSymbolic("Example 1", s1, "Animal A", "Miles", "Animal"); 422 423 displayYSymbolic("Example 2", s2, "Animal B", "Miles", "Animal"); 424 425 displayYSymbolicCombinedHorizontally("Example 3", (SampleYSymbolicDataset) s1.clone(), 426 (SampleYSymbolicDataset) s2.clone()); 427 428 displayYSymbolicCombinedVertically("Example 4", (SampleYSymbolicDataset) s1.clone(), 429 (SampleYSymbolicDataset) s2.clone()); 430 431 displayYSymbolicOverlaid("Example 5", (SampleYSymbolicDataset) s1.clone(), 432 (SampleYSymbolicDataset) s2.clone()); 433 } 434 435 } 436 | Popular Tags |