1 11 package com.tonbeller.jpivot.chart; 12 13 import org.jfree.chart.JFreeChart; 14 import org.jfree.chart.axis.CategoryAxis; 15 import org.jfree.chart.axis.CategoryAxis3D; 16 import org.jfree.chart.axis.DateAxis; 17 import org.jfree.chart.axis.NumberAxis; 18 import org.jfree.chart.axis.NumberAxis3D; 19 import org.jfree.chart.axis.ValueAxis; 20 import org.jfree.chart.labels.CategoryToolTipGenerator; 21 import org.jfree.chart.labels.PieToolTipGenerator; 22 import org.jfree.chart.labels.StandardCategoryToolTipGenerator; 23 import org.jfree.chart.labels.StandardPieToolTipGenerator; 24 import org.jfree.chart.labels.StandardXYToolTipGenerator; 25 import org.jfree.chart.labels.XYToolTipGenerator; 26 import org.jfree.chart.plot.CategoryPlot; 27 import org.jfree.chart.plot.MultiplePiePlot; 28 import org.jfree.chart.plot.PiePlot; 29 import org.jfree.chart.plot.PiePlot3D; 30 import org.jfree.chart.plot.PlotOrientation; 31 import org.jfree.chart.plot.XYPlot; 32 import org.jfree.chart.renderer.category.AreaRenderer; 33 import org.jfree.chart.renderer.category.BarRenderer; 34 import org.jfree.chart.renderer.category.BarRenderer3D; 35 import org.jfree.chart.renderer.category.CategoryItemRenderer; 36 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 37 import org.jfree.chart.renderer.category.StackedAreaRenderer; 38 import org.jfree.chart.renderer.category.StackedBarRenderer; 39 import org.jfree.chart.renderer.category.StackedBarRenderer3D; 40 import org.jfree.chart.renderer.xy.StandardXYItemRenderer; 41 import org.jfree.chart.urls.CategoryURLGenerator; 42 import org.jfree.chart.urls.PieURLGenerator; 43 import org.jfree.chart.urls.StandardXYURLGenerator; 44 import org.jfree.chart.urls.XYURLGenerator; 45 import org.jfree.data.category.CategoryDataset; 46 import org.jfree.data.xy.XYDataset; 47 import org.jfree.util.TableOrder; 48 53 public class ChartFactory { 54 55 static String urlPrefix = ""; 57 58 71 public static JFreeChart createPieChart(String title, 72 java.awt.Font titleFont, 73 CategoryDataset data, 74 TableOrder order, 75 boolean legend, 76 boolean tooltips, 77 boolean urls, 78 PieURLGenerator urlGenerator 79 ) { 80 81 MultiplePiePlot plot = new MultiplePiePlot(data); 82 plot.setDataExtractOrder(order); 83 84 PiePlot pp= (PiePlot) plot.getPieChart().getPlot(); 85 pp.setBackgroundPaint(null); 87 pp.setOutlineStroke(null); 89 PieToolTipGenerator tooltipGenerator = null; 91 if (tooltips) { 92 tooltipGenerator = new StandardPieToolTipGenerator(); 93 } 94 95 if (!urls) { 97 urlGenerator = null; 98 } 99 100 pp.setToolTipGenerator(tooltipGenerator); 101 pp.setLabelGenerator(null); 102 pp.setURLGenerator(urlGenerator); 103 104 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 105 106 return chart; 107 108 } 109 114 115 public static JFreeChart create3DPieChart(String title, 116 java.awt.Font titleFont, 117 CategoryDataset data, 118 TableOrder order, 119 boolean legend, 120 boolean tooltips, 121 boolean urls, 122 PieURLGenerator urlGenerator 123 ) { 124 125 MultiplePiePlot plot = new MultiplePiePlot(data); 126 plot.setDataExtractOrder(order); 127 128 130 JFreeChart pieChart = new JFreeChart(new PiePlot3D(null)); 131 pieChart.setBackgroundPaint(null); 132 plot.setPieChart(pieChart); 133 134 PiePlot3D pp = (PiePlot3D) plot.getPieChart().getPlot(); 135 pp.setBackgroundPaint(null); 136 138 pp.setOutlineStroke(null); 140 141 PieToolTipGenerator tooltipGenerator = null; 142 if (tooltips) { 143 tooltipGenerator = new StandardPieToolTipGenerator(); 144 } 145 146 if (!urls) { 147 urlGenerator = null; 148 } 149 150 pp.setToolTipGenerator(tooltipGenerator); 151 pp.setLabelGenerator(null); 152 pp.setURLGenerator(urlGenerator); 153 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 154 155 return chart; 156 157 } 158 171 public static JFreeChart createBarChart(String title, 172 java.awt.Font titleFont, 173 String categoryAxisLabel, 174 String valueAxisLabel, 175 CategoryDataset data, 176 PlotOrientation orientation, 177 boolean legend, 178 boolean tooltips, 179 boolean urls, 180 CategoryURLGenerator urlGenerator 181 ) { 182 183 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 184 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 185 BarRenderer renderer = new BarRenderer(); 186 187 if (tooltips) { 188 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 189 } 190 if (urls) { 191 renderer.setItemURLGenerator(urlGenerator); 192 } 193 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 194 plot.setOrientation(orientation); 195 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 196 197 return chart; 198 199 } 200 201 214 public static JFreeChart createBarChart3D(String title, 215 java.awt.Font titleFont, 216 String categoryAxisLabel, 217 String valueAxisLabel, 218 CategoryDataset data, 219 PlotOrientation orientation, 220 boolean legend, 221 boolean tooltips, 222 boolean urls, 223 CategoryURLGenerator urlGenerator) { 224 225 CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel); 226 ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel); 227 228 BarRenderer3D renderer = new BarRenderer3D(); 229 230 if (tooltips) { 232 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 233 } 234 if (urls) { 235 renderer.setItemURLGenerator(urlGenerator); 236 } 237 238 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 239 plot.setOrientation(orientation); 240 plot.setForegroundAlpha(0.75f); 241 242 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 243 244 return chart; 245 246 } 247 248 261 public static JFreeChart createStackedBarChart(String title, 262 java.awt.Font titleFont, 263 String domainAxisLabel, 264 String rangeAxisLabel, 265 CategoryDataset data, 266 PlotOrientation orientation, 267 boolean legend, 268 boolean tooltips, 269 boolean urls, 270 CategoryURLGenerator urlGenerator) { 271 272 CategoryAxis categoryAxis = new CategoryAxis(domainAxisLabel); 273 ValueAxis valueAxis = new NumberAxis(rangeAxisLabel); 274 275 StackedBarRenderer renderer = new StackedBarRenderer(); 277 if (tooltips) { 278 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 279 } 280 if (urls) { 281 renderer.setItemURLGenerator(urlGenerator); 282 } 283 284 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 285 plot.setOrientation(orientation); 286 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 287 288 return chart; 289 290 } 291 292 305 public static JFreeChart createStackedBarChart3D(String title, 306 java.awt.Font titleFont, 307 String categoryAxisLabel, 308 String valueAxisLabel, 309 CategoryDataset data, 310 PlotOrientation orientation, 311 boolean legend, 312 boolean tooltips, 313 boolean urls, 314 CategoryURLGenerator urlGenerator) { 315 316 CategoryAxis categoryAxis = new CategoryAxis3D(categoryAxisLabel); 318 ValueAxis valueAxis = new NumberAxis3D(valueAxisLabel); 319 320 CategoryItemRenderer renderer = new StackedBarRenderer3D(); 322 CategoryToolTipGenerator toolTipGenerator = null; 323 if (tooltips) { 324 toolTipGenerator = new StandardCategoryToolTipGenerator(); 325 } 326 if ( urls ) { 327 renderer.setItemURLGenerator(urlGenerator); 328 } 329 renderer.setToolTipGenerator(toolTipGenerator); 330 331 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 333 plot.setOrientation(orientation); 334 335 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 337 338 return chart; 339 340 } 341 342 343 356 public static JFreeChart createLineChart(String title, 357 java.awt.Font titleFont, 358 String categoryAxisLabel, 359 String valueAxisLabel, 360 CategoryDataset data, 361 PlotOrientation orientation, 362 boolean legend, 363 boolean tooltips, 364 boolean urls, 365 CategoryURLGenerator urlGenerator) { 366 367 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 368 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 369 370 LineAndShapeRenderer renderer = new LineAndShapeRenderer(); 371 renderer.setLinesVisible(true); 372 renderer.setShapesVisible(false); 373 if (tooltips) { 374 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 375 } 376 if (urls) { 377 renderer.setItemURLGenerator(urlGenerator); 378 } 379 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 380 plot.setOrientation(orientation); 381 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 382 383 return chart; 384 385 } 386 387 400 public static JFreeChart createAreaChart(String title, 401 java.awt.Font titleFont, 402 String categoryAxisLabel, 403 String valueAxisLabel, 404 CategoryDataset data, 405 PlotOrientation orientation, 406 boolean legend, 407 boolean tooltips, 408 boolean urls, 409 CategoryURLGenerator urlGenerator) { 410 411 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 412 categoryAxis.setCategoryMargin(0.0); 413 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 414 AreaRenderer renderer = new AreaRenderer(); 415 if (tooltips) { 416 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 417 } 418 if (urls) { 419 renderer.setItemURLGenerator(urlGenerator); 420 } 421 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 422 plot.setOrientation(orientation); 423 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 424 425 return chart; 426 427 } 428 429 442 public static JFreeChart createStackedAreaChart(String title, 443 java.awt.Font titleFont, 444 String categoryAxisLabel, 445 String valueAxisLabel, 446 CategoryDataset data, 447 PlotOrientation orientation, 448 boolean legend, 449 boolean tooltips, 450 boolean urls, 451 CategoryURLGenerator urlGenerator) { 452 453 CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel); 454 ValueAxis valueAxis = new NumberAxis(valueAxisLabel); 455 456 StackedAreaRenderer renderer = new StackedAreaRenderer(); 457 if (tooltips) { 458 renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); 459 } 460 if (urls) { 461 renderer.setItemURLGenerator(urlGenerator); 462 } 463 464 CategoryPlot plot = new CategoryPlot(data, categoryAxis, valueAxis, renderer); 465 plot.setOrientation(orientation); 466 JFreeChart chart = new JFreeChart(title, titleFont, plot, legend); 467 468 return chart; 469 470 } 471 472 473 492 public static JFreeChart createTimeSeriesChart(String title, 493 java.awt.Font titleFont, 494 String timeAxisLabel, 495 String valueAxisLabel, 496 XYDataset data, 497 boolean legend, 498 boolean tooltips, 499 boolean urls) { 500 501 ValueAxis timeAxis = new DateAxis(timeAxisLabel); 502 timeAxis.setLowerMargin(0.02); timeAxis.setUpperMargin(0.02); 504 NumberAxis valueAxis = new NumberAxis(valueAxisLabel); 505 valueAxis.setAutoRangeIncludesZero(false); XYPlot plot = new XYPlot(data, timeAxis, valueAxis, null); 507 508 XYToolTipGenerator tooltipGenerator = null; 509 if (tooltips) { 510 tooltipGenerator = StandardXYToolTipGenerator.getTimeSeriesInstance(); 511 } 513 514 XYURLGenerator urlGenerator = null; 515 if (urls) { 516 urlGenerator = new StandardXYURLGenerator(); 517 } 518 519 plot.setRenderer(new StandardXYItemRenderer(StandardXYItemRenderer.LINES, tooltipGenerator, urlGenerator)); 520 521 JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend); 522 523 return chart; 524 525 } 526 527 } 528 | Popular Tags |