How do I change a JFreeChart's size

前端 未结 4 1958
后悔当初
后悔当初 2020-11-22 09:06

I\'ve added a JFreeChart to a JPanel (using a BorderLayout), and it\'s huge. Is there something I can do to make it smaller?<

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 09:34

    In addition to answer "4" of @trashgod, I had the same problem and 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> dataset() {
            ArrayList> dataset = new ArrayList>();
            dataset.add(row( "Tom", "LoggedIn", "Spain" ));
            dataset.add(row( "Jerry", "LoggedOut", "England" ));
            dataset.add(row( "Gooffy", "LoggedOut", "France" ));
            return dataset;
        }
    
        static ArrayList row(String name, String actualState, String country) {
            ArrayList row = new ArrayList();
            row.add(name); row.add(actualState); row.add(country); 
            return row;
        }
    
        ArrayList> 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> 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> 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 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!

提交回复
热议问题