From: Nathaniel G. A. <nat...@ya...> - 2003-08-14 02:02:53
|
try this code. public class SwingDemo extends JFrame { private JPanel panel; private PieChart2DProperties pieChart2DProperties; private LegendProperties legendProperties; private ChartProperties chartProperties; /******************************************************************************* * ********************************************************************************/ public SwingDemo() throws ChartDataException, PropertyException { initComponents(); } /******************************************************************************* * ********************************************************************************/ private void initComponents() { this.setSize( 500, 500 ); this.panel = new JPanel( true ); this.panel.setSize( 500, 500 ); this.getContentPane().add( this.panel ); this.pieChart2DProperties = new PieChart2DProperties(); this.legendProperties= new LegendProperties(); this.chartProperties= new ChartProperties(); this.setVisible( true ); addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing( WindowEvent windowEvent ) { exitForm( windowEvent ); } } ); } /********************************************************************************* * * @param graphics ********************************************************************************/ public void paint( Graphics graphics ) { try { String[] labels = {"BMW", "Audi", "Lexus"}; String title = "Cars that Own"; Paint[] paints = {Color.blue, Color.gray, Color.red}; double[] data = {50d, 30d, 20d}; PieChartDataSet pieChartDataSet = new PieChartDataSet( title, data, labels, paints, this.pieChart2DProperties ); Dimension dimension= this.panel.getSize(); PieChart2D pieChart2D = new PieChart2D( pieChartDataSet, this.legendProperties, this.chartProperties, (int) dimension.getWidth(), (int) dimension.getHeight() ); //***** BEGIN SWING SPECIFIC CODE *********************************** pieChart2D.setGraphics2D( (Graphics2D) this.panel.getGraphics() ); pieChart2D.render(); //***** END SWING SPECIFIC CODE ************************************* } catch( ChartDataException chartDataException ) { chartDataException.printStackTrace(); } catch( PropertyException propertyException ) { propertyException.printStackTrace(); } } /******************************************************************************* * Exit the Application * * @param windowEvent ******************************************************************************/ private void exitForm( WindowEvent windowEvent ) { System.exit( 0 ); } /****************************************************************************** * ******************************************************************************/ public static void main( String args[] ) throws ChartDataException, PropertyException { new SwingDemo(); } } --- pve...@ic... wrote: > Thanks Nathaniel, but I haven't fix the problem yet. > > I'm using the code below as an example. The problem with this code is that, if > you maximize, minimize or resize the frame, the chart disappear. > > I guess the problem is with the paint(Graphics) method at Charts class, I tried > overwritting that method as you said, but it didn't work. > > I don't know what to do. Please give me some advises. > > pavel > > > import javax.swing.JPanel; > import javax.swing.*; > import java.awt.*; > import org.jCharts.chartData.*; > import org.jCharts.properties.*; > import org.jCharts.axisChart.*; > import org.jCharts.nonAxisChart.*; > import org.jCharts.types.ChartType; > import org.jCharts.test.TestDataGenerator; > import org.jCharts.axisChart.customRenderers.axisValue.renderers.*; > > > public class Charts extends JPanel { > > LegendProperties lp = null; > ChartProperties cp = null; > BarChartProperties ctp = null; > PieChart2D chart = null ; > AxisProperties ap = null; > > public Charts() { > > /// ************** PIE CHART DATASET ************************ > try { > double[] data = { > 81d, 55d, 39d, 20.6d}; > String[] labels = { > "BMW M5", "BMW M3", "Viper GTS-R", "Corvette Z06"}; > Paint[] paints = { > Color.lightGray, Color.green, Color.blue, Color.red}; > > PieChart2DProperties pieChart2DProperties = new PieChart2DProperties(); > PieChartDataSet pieChartDataSet = new PieChartDataSet("Cars That Own", > data, labels, paints, pieChart2DProperties); > > chart = new PieChart2D(pieChartDataSet, new LegendProperties(), > new ChartProperties(), 350, 350); > > } > catch(Exception e){ > System.out.println(e); > } > > } > > public static void main(String args[]) throws Exception { > JFrame frame = new JFrame("CHART TEST"); > frame.setSize( 400, 400 ); > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > //uncomment this line to do not lose the chart when you resize the frame > //frame.setResizable(false); > Charts pieChart = new Charts(); > frame.getContentPane().add(pieChart); > pieChart.setBackground(Color.white); > frame.setVisible(true); > pieChart.chart.setGraphics2D((Graphics2D)pieChart.getGraphics()); > pieChart.chart.render(); > } > > > } > > > ------------------------------------------------- > This mail sent through IMP: http://horde.org/imp/ > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > jCharts-users mailing list > jCh...@li... > https://lists.sourceforge.net/lists/listinfo/jcharts-users ===== http://nathaniel-auvil.blog-city.com/ __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com |