JPanel中可调整大小的JFreeChart

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPanel中可调整大小的JFreeChart相关的知识,希望对你有一定的参考价值。

我知道这个问题早先被问过(hereherehere),但提供的解决方案不起作用。因此,请不要将其标记为重复。我正在使用JFreeChart绘制折线图。然后我将这个图表放在一个JPanel中,然后放入tabbedPane。我知道将JPanel直接放在JFrame中可以调整大小,但我怎么能这样做呢?

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是JFreeChart类型的方法。这个类在另一个类中调用

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

框架设计是使用NetBeans完成的。我已经尝试了所有解决方案,例如将布局从BorderLayout更改为GridBagLayout,甚至是提到的here

答案

当框架调整大小时,BorderLayout.CENTER应该让ChartPanel成长。我猜你有一个带有JPanelFlowLayout,它继续使用图表面板的首选大小。由于FlowLayout是默认值,您可能需要查找它。

编辑:有一个完整的示例here,它将一个折线图添加到选项卡式窗格。

编辑:你的DynamicLineAndTimeSeriesChart有默认的FlowLayout;我想你想要一个GridLayout

public DynamicLineAndTimeSeriesChart(String id) {
    this.setLayout(new GridLayout());
    …
}
另一答案

没有看到你的所有代码,它只能是猜测。但是,作为一种解决方法,您可以让您的JPanel使用ComponentListener监听resize事件,如下所示:

 content.addComponentListener(new ComponentAdapter(){
        public void componentResized(ActionEvent e){
            chartPanel.setSize(content.getSize());
        }
    });
另一答案

我有同样的问题,我设法解决这个问题:(1)创建一个自定义类,扩展JPanel(2)以某种方式得到大小,你想传递给你的图表(3)创建一个返回一个方法“ChartPanel”对象是这样的:

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);
        }
    };
}

我准备了一个SSCCE让你知道它是如何工作的:

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);
            }
        };
    }
}

我真的希望它有所帮助!

以上是关于JPanel中可调整大小的JFreeChart的主要内容,如果未能解决你的问题,请参考以下文章

如何将调整大小箭头添加到JPanel?

如何在这样的代码中绘制可调整大小的 UIImage?

如何使 iframe 可调整大小?

electron.js中可调整大小的窗口

为啥手动调整 Visjs 时间线图的高度(可调整大小)不起作用?

可调整大小视图的圆角