Chart.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Chart.java
  3. *
  4. * Created on 04 August 2006, 11:14
  5. *
  6. * To change this template, choose Tools | Template Manager
  7. * and open the template in the editor.
  8. */
  9. package com.cloudsoft.utils;
  10. // Import the Swing classes
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.*;
  14. // Import the JFreeChart classes
  15. import org.jfree.chart.*;
  16. import org.jfree.chart.plot.*;
  17. import org.jfree.data.*;
  18. import org.jfree.data.general.*;
  19. /**
  20. *
  21. * @author pyl38997
  22. */
  23. public class Chart extends JPanel
  24. {
  25. // Holds the data
  26. private DefaultPieDataset dataset = new DefaultPieDataset();
  27. // Create a set of charts
  28. private JFreeChart chart1;
  29. private JFreeChart chart2;
  30. private JFreeChart chart3;
  31. private JFreeChart chart4;
  32. // Create a set of panels that can show charts
  33. private ChartPanel panel1;
  34. private ChartPanel panel2;
  35. private ChartPanel panel3;
  36. private ChartPanel panel4;
  37. /** Creates a new instance of Chart */
  38. public Chart()
  39. {
  40. // Initialize the dataset
  41. dataset.setValue( "California", new Double( 10.0 ) );
  42. dataset.setValue( "Arizona", new Double( 8.0 ) );
  43. dataset.setValue( "New Mexico", new Double( 8.0 ) );
  44. dataset.setValue( "Texas", new Double( 40.0 ) );
  45. dataset.setValue( "Louisiana", new Double( 8.0 ) );
  46. dataset.setValue( "Mississippi", new Double( 4.0 ) );
  47. dataset.setValue( "Alabama", new Double( 2.0 ) );
  48. dataset.setValue( "Florida", new Double( 20.0 ) );
  49. // Create the charts
  50. chart1 = ChartFactory.createPieChart(
  51. "Driving Time Spent Per State (Flat Pie Chart)", // The chart title
  52. dataset, // The dataset for the chart
  53. true, // Is a legend required?
  54. true, // Use tooltips
  55. false // Configure chart to generate URLs?
  56. );
  57. chart2 = ChartFactory.createPieChart(
  58. "Driving Time Spent Per State (Exploded Pie Chart)", // The chart title
  59. dataset, // The dataset for the chart
  60. true, // Is a legend required?
  61. true, // Use tooltips
  62. false // Configure chart to generate URLs?
  63. );
  64. PiePlot plot = ( PiePlot )chart2.getPlot();
  65. plot.setExplodePercent( "Texas", 0.25 );
  66. chart3 = ChartFactory.createPieChart3D(
  67. "Driving Time Spent Per State (3D Pie Chart)", // The chart title
  68. dataset, // The dataset for the chart
  69. true, // Is a legend required?
  70. true, // Use tooltips
  71. false // Configure chart to generate URLs?
  72. );
  73. chart4 = ChartFactory.createPieChart3D(
  74. "Driving Time Spent Per State (3D with Transparency)", // The chart title
  75. dataset, // The dataset for the chart
  76. true, // Is a legend required?
  77. true, // Use tooltips
  78. false // Configure chart to generate URLs?
  79. );
  80. PiePlot3D plot4 = ( PiePlot3D )chart4.getPlot();
  81. plot4.setForegroundAlpha( 0.6f );
  82. // Create this panel
  83. this.setLayout( new GridLayout( 2, 2 ) );
  84. this.panel1 = new ChartPanel( chart1 );
  85. this.panel2 = new ChartPanel( chart2 );
  86. this.panel3 = new ChartPanel( chart3 );
  87. this.panel4 = new ChartPanel( chart4 );
  88. this.add( panel1 );
  89. this.add( panel2 );
  90. this.add( panel3 );
  91. this.add( panel4 );
  92. }
  93. public static void main( String[] args )
  94. {
  95. JFrame frame = new JFrame( "My Trip Driving From CA to FL..." );
  96. Chart chart = new Chart();
  97. frame.getContentPane().add( chart, BorderLayout.CENTER );
  98. frame.setSize( 640, 480 );
  99. frame.setVisible( true );
  100. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  101. }
  102. }