【问题标题】:Resizable JFreeChart inside a JPanelJPanel 中可调整大小的 JFreeChart
【发布时间】:2014-03-19 07:41:09
【问题描述】:

我知道之前有人问过这个问题(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

【问题讨论】:

标签: java swing netbeans jfreechart


【解决方案1】:

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

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

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

public DynamicLineAndTimeSeriesChart(String id) {
    this.setLayout(new GridLayout());
    …
}

【讨论】:

  • 您能具体说明一下吗?我使用了两个 JPanel,它们的布局都已更改为borderlayout。
  • 我不知道为什么这行不通;你能编辑你的问题来展示一个完整的例子吗?
  • 已完成编辑,但无法发布完整代码,因此我发布了相关部分。感谢您的帮助。
【解决方案2】:

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

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

【讨论】:

  • 仍然没有变化,图表没有调整大小。
  • 您是否尝试过监控veriables?在 chartPanel.setSize(content.getSize()); 之后的图表面板及其容器的大小是多少?我想不出为什么这不起作用的原因。
【解决方案3】:

我遇到了同样的问题,我设法解决了这个问题: (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);
            }
        };
    }
}

我真的希望它有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2015-02-20
    • 2012-09-01
    相关资源
    最近更新 更多