Results 1 to 3 of 3
  1. #1
    Ypsilon IV is offline Member
    Join Date
    Apr 2009
    Location
    Earth
    Posts
    14
    Rep Power
    0

    Default How to add jfreechart to jPanel?

    Hello everyone,
    The question is, how to add graph generated by JFreeChart to a jPanel?
    I have a Graph class, that generates graph:
    Java Code:
    package epidemics;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    
    public class Graph {
    	public Graph(Data data) {
    		//XYSeries series1 = new XYSeries("Healthy People");
    		//XYSeries series2 = new XYSeries("Sick People");
    		//XYSeries series3 = new XYSeries("Recovered People");
    		//System.out.println(data.getDuration());
    		for (int i = 0; i < data.getD(); i++) {
    			//System.out.println(data.getHppl(i) + " " + data.getSppl(i) + " " + data.getMonth(i));
    			series1.add((i), data.getS(i));
    			series2.add((i), data.getI(i));
    			series3.add((i), data.getR(i));			
    		}
    		
    		dataset.addSeries(series1);
    		dataset.addSeries(series2);
    		dataset.addSeries(series3);
    		//JFreeChart chart = ChartFactory.createXYLineChart ("Graph", "time", "individuals", dataset, PlotOrientation.VERTICAL, true, true, false);
                    chart = ChartFactory.createXYLineChart ("Graph", "time", "individuals", dataset, PlotOrientation.VERTICAL, true, true, false);
    		
                    try {
    			ChartUtilities.saveChartAsJPEG(new File("d:\\graph.jpg"), chart, 500, 300);
    		} 
    		catch (IOException e) {
    			System.err.println("Problem occurred creating chard.");
    		}
    	}
            public JFreeChart drawChart() {
                return chart;
            }
    
    	private XYSeries series1 = new XYSeries("Healthy People");
            private XYSeries series2 = new XYSeries("Sick People");
            private XYSeries series3 = new XYSeries("Recovered People");
            private XYSeriesCollection dataset = new XYSeriesCollection();
            JFreeChart chart;
    }
    When I create an object of Graph, it creates a picture in a file. I want to add this graph to a jPanel, not save it as a picture.
    I tried to do it, but it does not seem to work:
    Java Code:
    Graph graph = new Graph(data);
            JFreeChart chart;
            chart = graph.drawChart();
            ChartPanel chartPanel = new ChartPanel(chart);
            jPanel1.add(chartPanel);
    Could someone please advise me how to solve this problem?
    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. JFreeChart 1.0.11
    By Java Tip in forum Java Software
    Replies: 3
    Last Post: Yesterday, 08:27 PM
  2. JfreeChart
    By puk284 in forum Advanced Java
    Replies: 1
    Last Post: 05-12-2009, 04:52 PM
  3. Replies: 5
    Last Post: 02-25-2009, 08:01 AM
  4. JFreeChart 1.0.8
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 11-27-2007, 08:27 PM
  5. JFreeChart 1.0.5
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-10-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •