0

I know this question has been asked earlier (here ,here and here), but the offered solutions are not working. Hence, please don't mark it as duplicate. I am drawing a line chart using the JFreeChart. Then I put this chart inside a JPanel which is then put in a tabbedPane. I know directly putting the JPanel in a JFrame makes it resizable but how can I do it this way?

public class DynamicLineAndTimeSeriesChart extends JPanel implements ActionListener {
    private ArrayList<TimeSeries> Series = new ArrayList<TimeSeries>();
    private ArrayList<Double> Last = new ArrayList<Double>();
    private String Id1;
    private Timer timer = new Timer(250, this);
    public DynamicLineAndTimeSeriesChart(String id) {

        Runtime runtime = Runtime.getRuntime();
        int NoOfProc = runtime.availableProcessors();
        for (int i = 0; i < NoOfProc; i++) {
            Last.add(new Double(100.0));
            Series.add(new TimeSeries("Core" + i, Millisecond.class));
        }
        Id1 = id;
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        for (int i = 0; i < NoOfProc; i++) {
            dataset.addSeries(this.Series.get(i));
        }
        final JFreeChart chart = createChart(dataset);
        timer.setInitialDelay(1000);
        chart.setBackgroundPaint(Color.LIGHT_GRAY);

        //Created JPanel to show graph on screen
        final JPanel content = new JPanel(new GridLayout());
        final ChartPanel chartPanel = new ChartPanel(chart);
        content.add(chartPanel, BorderLayout.CENTER);
        content.revalidate();
        this.add(content);

        timer.start();
    }

createChart is a method of type JFreeChart. This class is called in another class

JPanel main = new JPanel(new BorderLayout());
main.add(demo);
jTabbedPane1.add("Core", main);
demo.setVisible(true);

The frame designing was done using NetBeans. I have tried all the solution like changing the layouts from BorderLayout to GridBagLayout and even the one mentioned here.

Community
  • 1
  • 1
sol
  • 95
  • 1
  • 12

3 Answers3

1

BorderLayout.CENTER should let the ChartPanel grow as the frame is resized. I'm guessing that you have a JPanel with a FlowLayout, which keeps on using the chart panel's preferred size. Since FlowLayout is the default, you may have to look for it.

Edit: There's a complete example here that adds a line chart to a tabbed pane.

Edit: Your DynamicLineAndTimeSeriesChart has the default FlowLayout; I think you want a GridLayout.

public DynamicLineAndTimeSeriesChart(String id) {
    this.setLayout(new GridLayout());
    …
}
Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • Can you please specify? I have used two JPanels and both their layouts have been changed to borderlayout. – sol Mar 20 '14 at 03:58
  • I don't know why that wouldn't work; can you edit your question to show a complete example? – Catalina Island Mar 20 '14 at 11:47
  • Have done the edits, but posting the complete code was not possible so I posted the relevant part. Thanks for helping. – sol Mar 21 '14 at 03:50
0

Without seeing all of your code it can only be speculation. But, as a workaround, you could make your JPanel listen for resize events with a ComponentListener, something like this :

 content.addComponentListener(new ComponentAdapter(){
        public void componentResized(ActionEvent e){
            chartPanel.setSize(content.getSize());
        }
    });
Jannis Alexakis
  • 1,279
  • 4
  • 19
  • 38
  • Still no change, the graph is not resizing. – sol Mar 21 '14 at 03:52
  • Have you tried monitoring the veriables ? What´s the size of the chartpanel and it´s container right after chartPanel.setSize(content.getSize()); I can´t think of a reason why that shouldn´t work. – Jannis Alexakis Mar 21 '14 at 06:56
0

I had the same problem and I managed to solve it like this: (1) create a custom class which extends JPanel (2) get the size somehow, that you would like to pass to your chart (3) create a method which returns a "ChartPanel" object like this:

ChartPanel chart() {
    //... custom code here
    JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false );`enter code here`
    // Now: this is the trick to manage setting the size of a chart into a panel!:
    return new ChartPanel(chart) { 
        public Dimension getPreferredSize() {
            return new Dimension(width, height);
        }
    };
}

I prepared a SSCCE to let you know how it works:

import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class MyPieChart extends JPanel {

    public static void main(String[] args) {
        example1();
        example2();
        example3();
    }

    public static void example1() {
        JPanel panel = new JPanel();
        panel.setBounds(50, 80, 100, 100);
        MyPieChart piePanel = new MyPieChart("Example 1", dataset(), panel);
        panel.add(piePanel);
        JFrame frame = new JFrame();
        frame.setLayout(null); 
        frame.setBounds(10, 10, 200, 300);
        frame.add(panel);
        frame.setVisible(true);
    }

    public static void example2() {
        MyPieChart piePanel = new MyPieChart("Example 2", dataset(), 30, 50, 100, 100);
        JFrame frame = new JFrame();
        frame.setLayout(null); 
        frame.setBounds(210, 10, 200, 300);
        frame.add(piePanel);
        frame.setVisible(true);
    }

    public static void example3() {
        MyPieChart piePanel = new MyPieChart("Example 3", dataset(), 100, 100);
        piePanel.setLocation(0,0);
        JFrame frame = new JFrame();
        frame.setLayout(null); 
        frame.setBounds(410, 10, 200, 300);
        frame.add(piePanel);
        frame.setVisible(true);
    }

    static ArrayList<ArrayList<String>> dataset() {
        ArrayList<ArrayList<String>> dataset = new ArrayList<ArrayList<String>>();
        dataset.add(row( "Tom", "LoggedIn", "Spain" ));
        dataset.add(row( "Jerry", "LoggedOut", "England" ));
        dataset.add(row( "Gooffy", "LoggedOut", "France" ));
        return dataset;
    }

    static ArrayList<String> row(String name, String actualState, String country) {
        ArrayList<String> row = new ArrayList<String>();
        row.add(name); row.add(actualState); row.add(country); 
        return row;
    }

    ArrayList<ArrayList<String>> dataset;
    DefaultPieDataset pieDataset = new DefaultPieDataset(); 
    int width, height, posX, posY;
    int colState = 1;
    String title;
    String LoggedIn = "LoggedIn";
    String LoggedOut = "LoggedOut";

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, int...args) {

        if(args.length==2) {
            this.width = args[0];
            this.height = args[1];
            this.setSize(width, height);
        }
        else if(args.length==4) {
            this.posX = args[0];
            this.posY = args[1];
            this.width = args[2];
            this.height = args[3];
            this.setBounds(posX, posY, width, height);
        }
        else {
            System.err.println("Error: wrong number of size/position arguments");
            return;
        }

        this.title = title;
        this.dataset = dataset;
        this.add(chart());
    }

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, JPanel panel) {
        this.title = title;
        this.dataset = dataset;
        this.width = panel.getWidth();
        this.height = panel.getHeight();
        this.setBounds(panel.getBounds());
        this.add(chart());
    }

    ChartPanel chart() {

        int totalLoggedIn = 0;
        int totalLoggedOut = 0;

        for(ArrayList<String> user : dataset) {
            if(user.get(colState).equals(LoggedIn)) totalLoggedIn++;
            else totalLoggedOut++;
        }
        pieDataset.clear();
        pieDataset.setValue(LoggedIn +": "+ totalLoggedIn, totalLoggedIn);
        pieDataset.setValue(LoggedOut +": "+ totalLoggedOut, totalLoggedOut);

        JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false );

        return new ChartPanel(chart) { // this is the trick to manage setting the size of a chart into a panel!
            public Dimension getPreferredSize() {
                return new Dimension(width, height);
            }
        };
    }
}

I really hope it helps!

Paul Efford
  • 261
  • 4
  • 12