1 34 35 36 package org.krysalis.jcharts.demo.swing; 37 38 39 import org.krysalis.jcharts.chartData.ChartDataException; 40 import org.krysalis.jcharts.chartData.PieChartDataSet; 41 import org.krysalis.jcharts.properties.PropertyException; 42 import org.krysalis.jcharts.properties.PieChart2DProperties; 43 import org.krysalis.jcharts.properties.ChartProperties; 44 import org.krysalis.jcharts.properties.LegendProperties; 45 import org.krysalis.jcharts.nonAxisChart.PieChart2D; 46 47 import javax.swing.*; 48 import java.awt.*; 49 import java.awt.event.WindowEvent ; 50 51 52 57 public class SwingDemo extends JFrame 58 { 59 private JPanel panel; 60 61 private PieChart2DProperties pieChart2DProperties; 62 private LegendProperties legendProperties; 63 private ChartProperties chartProperties; 64 65 66 69 public SwingDemo() throws ChartDataException, PropertyException 70 { 71 initComponents(); 72 } 73 74 75 78 private void initComponents() 79 { 80 this.setSize( 500, 500 ); 81 this.panel = new JPanel( true ); 82 this.panel.setSize( 500, 500 ); 83 this.getContentPane().add( this.panel ); 84 85 this.pieChart2DProperties = new PieChart2DProperties(); 86 this.legendProperties= new LegendProperties(); 87 this.chartProperties= new ChartProperties(); 88 89 this.setVisible( true ); 90 91 addWindowListener( new java.awt.event.WindowAdapter () 92 { 93 public void windowClosing( WindowEvent windowEvent ) 94 { 95 exitForm( windowEvent ); 96 } 97 } 98 ); 99 } 100 101 102 106 public void paint( Graphics graphics ) 107 { 108 try { 109 String [] labels = {"BMW", "Audi", "Lexus"}; 110 String title = "Cars that Own"; 111 Paint[] paints = {Color.blue, Color.gray, Color.red}; 112 double[] data = {50d, 30d, 20d}; 113 PieChartDataSet pieChartDataSet = new PieChartDataSet( title, 114 data, 115 labels, 116 paints, 117 this.pieChart2DProperties ); 118 119 Dimension dimension= this.panel.getSize(); 120 PieChart2D pieChart2D = new PieChart2D( pieChartDataSet, 121 this.legendProperties, 122 this.chartProperties, 123 (int) dimension.getWidth(), 124 (int) dimension.getHeight() ); 125 pieChart2D.setGraphics2D( (Graphics2D) this.panel.getGraphics() ); 126 pieChart2D.render(); 127 } 128 catch( ChartDataException chartDataException ) { 129 chartDataException.printStackTrace(); 130 } 131 catch( PropertyException propertyException ) { 132 propertyException.printStackTrace(); 133 } 134 } 135 136 137 142 private void exitForm( WindowEvent windowEvent ) 143 { 144 System.exit( 0 ); 145 } 146 147 148 151 public static void main( String args[] ) throws ChartDataException, PropertyException 152 { 153 new SwingDemo(); 154 } 155 156 157 } 158 | Popular Tags |