如何在面板上添加JFreechart? [英] How to add a JFreechart to a Panel?

查看:80
本文介绍了如何在面板上添加JFreechart?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找答案以使该功能正常运行,但没有成功,我将失去理智,所以我的问题就在这里.

我最近下载了JFreeChart来创建图表,并在我的GUI中实现它们. 这是要在其中插入图形(用白色标记)的GUI和面板:

报告1 http://www.freeimagehosting.net/newuploads/eff3r.png 报告2 http://www.freeimagehosting.net/newuploads/v5sty.png

我已经使用NetBeans编辑器构造了GUI和一个在线提供的JFreeChart示例. 下面是我的GUI的代码:

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;


public class Report extends JFrame implements Defaults {


public MMap j1map,j2map,j3map;
public int total_stations = 0, total_jobs = 0, total_parts = 0;
BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
             j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
             j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
public String sim_time;
BigDecimal systime = new BigDecimal(0);
BigDecimal worktime = new BigDecimal(0);
BigDecimal idletime = new BigDecimal(0);
BigDecimal queuetime = new BigDecimal(0);
/**
 * Creates new form Report
 */
public Report() {
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle(TITLE_Report);
    validate();
    setResizable(false);
    setVisible(true);
    //setLocationRelativeTo(null);
    initComponents();
    this.addWindowListener(new WindowAdapter() {
        @Override
       public void windowClosing(WindowEvent e) {
          e.getWindow().dispose();
          }
       }
    );
    centertables();
    //JLayeredPane layeredPane = new JLayeredPane(); 
    PieChart demo = new PieChart("Which operating system are you using?");
    demo.setOpaque(true); 
    demo.setBounds(0, 0, 300, 300);
    demo.setFocusable(true);
    demo.setBackground(Color.gray); 
    demo.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    //start();
    demo.setDoubleBuffered(true);        
    //demo.pack();
    //demo.setVisible(true);        
    //PieChart("teste");
    chartPanel1.add(demo, new Integer(0));
    //layeredPane.add(demo, new Integer(0));
    //this.getContentPane().add(jLayeredPane1); 
}

/* *************************************
 *          GRAPHICS
 *************************************/

public void PieChart(String chartTitle) {
    System.out.println("PieChart");
    // This will create the dataset 
    PieDataset dataset = createDataset();
    // based on the dataset we create the chart
    JFreeChart chart = createChart(dataset, chartTitle);
    // we put the chart into a panel
    ChartPanel chartPanel = new ChartPanel(chart);
    // default size
    chartPanel.setPreferredSize(new java.awt.Dimension(250, 270));
    // add it to our application
    //setContentPane(chartPanel);
    //StationsPanelGraph.add(chartPanel);
    //jLayeredPane1.add(chartPanel, new Integer(0), 0);

}

private  PieDataset createDataset() {
    System.out.println("PieDataset");
    DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("Linux", 29);
    result.setValue("Mac", 20);
    result.setValue("Windows", 51);
    return result;

}

private JFreeChart createChart(PieDataset dataset, String title) {
    System.out.println("Create Chart");
    JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
        dataset,                // data
        true,                   // include legend
        true,
        false);

    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setStartAngle(290);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);
    return chart;

}    

 /* *************************************
 *         END GRAPHICS
 *************************************/  

}

这是结果:

报告3 http://www.freeimagehosting.net/newuploads/unolz.png

我正在初始化图表并将其添加到主类构造函数中,下面是该图表的代码.我必须避免发布超过字符数限制的整个代码,但是这里是完整代码的URL(使用NetBeans初始化的Swing组件):解决方案

不要让GUI设计者来决定您的设计.根据建议此处使用它来管理各个面板.如果没有.form文件,则无法编辑GUI项目.我只是使用FlowLayout添加了图表,所以您可以看到它而无需更改initComponents().

import java.awt.FlowLayout;
import java.math.BigDecimal;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

public class Report extends JFrame {

    public int total_stations = 0, total_jobs = 0, total_parts = 0;
    BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
        j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
        j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
    public String sim_time;
    BigDecimal systime = new BigDecimal(0);
    BigDecimal worktime = new BigDecimal(0);
    BigDecimal idletime = new BigDecimal(0);
    BigDecimal queuetime = new BigDecimal(0);

    private static final String title = "Which operating system are you using?";

    /**
    * Creates new form Report
    */
    public Report() {
        initComponents();
        jPanel2.removeAll();
        jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        jPanel2.add(createPieChart(title));
        this.setLocationRelativeTo(null);
    }

    /*
    * *************************************
    * GRAPHICS ***********************************
    */
    private ChartPanel createPieChart(String chartTitle) {
        System.out.println("PieChart");
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        return chartPanel;
    }

    private PieDataset createDataset() {
        System.out.println("PieDataset");
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;

    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        System.out.println("Create Chart");
        JFreeChart chart = ChartFactory.createPieChart3D(
            title, dataset, true, true, false);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setCircular(true);
        return chart;

    }

    /*
    * *************************************
    * END GRAPHICS ***********************************
    */
    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        // code elided
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Report().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    public javax.swing.JPanel chartPanel1;
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel10;
    public javax.swing.JLabel jLabel11;
    public javax.swing.JLabel jLabel12;
    public javax.swing.JLabel jLabel13;
    public javax.swing.JLabel jLabel14;
    public javax.swing.JLabel jLabel15;
    public javax.swing.JLabel jLabel16;
    public javax.swing.JLabel jLabel17;
    public javax.swing.JLabel jLabel18;
    public javax.swing.JLabel jLabel19;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel20;
    public javax.swing.JLabel jLabel21;
    public javax.swing.JLabel jLabel22;
    public javax.swing.JLabel jLabel23;
    public javax.swing.JLabel jLabel24;
    public javax.swing.JLabel jLabel25;
    public javax.swing.JLabel jLabel26;
    public javax.swing.JLabel jLabel27;
    public javax.swing.JLabel jLabel28;
    public javax.swing.JLabel jLabel29;
    public javax.swing.JLabel jLabel30;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel jLabel7;
    public javax.swing.JLabel jLabel8;
    public javax.swing.JLabel jLabel9;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    public javax.swing.JPanel jPanel3;
    public javax.swing.JPanel jPanel4;
    public javax.swing.JPanel jPanel6;
    public javax.swing.JPanel jPanel7;
    public javax.swing.JPanel jPanel8;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JScrollPane jScrollPane3;
    public javax.swing.JScrollPane jScrollPane5;
    public javax.swing.JScrollPane jScrollPane6;
    public javax.swing.JSeparator jSeparator1;
    public javax.swing.JSeparator jSeparator3;
    public javax.swing.JSeparator jSeparator4;
    public javax.swing.JSeparator jSeparator5;
    public javax.swing.JSeparator jSeparator6;
    public javax.swing.JTabbedPane jTabbedPane1;
    public javax.swing.JTable jTable1;
    public javax.swing.JTable jTable2;
    public javax.swing.JTable jTable3;
    public javax.swing.JTable jTable4;
    // End of variables declaration
}

I've tried looking for answers to get this working but haven't managed to do so and I am about to lose my mind so here goes my question.

I've recently downloaded JFreeChart to create so charts and implement them in the GUI I had. Here's the GUI and the panel where I want to insert the graph (marked in white):

reports1 http://www.freeimagehosting.net/newuploads/eff3r.png reports2 http://www.freeimagehosting.net/newuploads/v5sty.png

I have used NetBeans editor to construct the GUI and a JFreeChart example available online. Below is the code for my GUI:

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;


public class Report extends JFrame implements Defaults {


public MMap j1map,j2map,j3map;
public int total_stations = 0, total_jobs = 0, total_parts = 0;
BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
             j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
             j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
public String sim_time;
BigDecimal systime = new BigDecimal(0);
BigDecimal worktime = new BigDecimal(0);
BigDecimal idletime = new BigDecimal(0);
BigDecimal queuetime = new BigDecimal(0);
/**
 * Creates new form Report
 */
public Report() {
    //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle(TITLE_Report);
    validate();
    setResizable(false);
    setVisible(true);
    //setLocationRelativeTo(null);
    initComponents();
    this.addWindowListener(new WindowAdapter() {
        @Override
       public void windowClosing(WindowEvent e) {
          e.getWindow().dispose();
          }
       }
    );
    centertables();
    //JLayeredPane layeredPane = new JLayeredPane(); 
    PieChart demo = new PieChart("Which operating system are you using?");
    demo.setOpaque(true); 
    demo.setBounds(0, 0, 300, 300);
    demo.setFocusable(true);
    demo.setBackground(Color.gray); 
    demo.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    //start();
    demo.setDoubleBuffered(true);        
    //demo.pack();
    //demo.setVisible(true);        
    //PieChart("teste");
    chartPanel1.add(demo, new Integer(0));
    //layeredPane.add(demo, new Integer(0));
    //this.getContentPane().add(jLayeredPane1); 
}

/* *************************************
 *          GRAPHICS
 *************************************/

public void PieChart(String chartTitle) {
    System.out.println("PieChart");
    // This will create the dataset 
    PieDataset dataset = createDataset();
    // based on the dataset we create the chart
    JFreeChart chart = createChart(dataset, chartTitle);
    // we put the chart into a panel
    ChartPanel chartPanel = new ChartPanel(chart);
    // default size
    chartPanel.setPreferredSize(new java.awt.Dimension(250, 270));
    // add it to our application
    //setContentPane(chartPanel);
    //StationsPanelGraph.add(chartPanel);
    //jLayeredPane1.add(chartPanel, new Integer(0), 0);

}

private  PieDataset createDataset() {
    System.out.println("PieDataset");
    DefaultPieDataset result = new DefaultPieDataset();
    result.setValue("Linux", 29);
    result.setValue("Mac", 20);
    result.setValue("Windows", 51);
    return result;

}

private JFreeChart createChart(PieDataset dataset, String title) {
    System.out.println("Create Chart");
    JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
        dataset,                // data
        true,                   // include legend
        true,
        false);

    PiePlot3D plot = (PiePlot3D) chart.getPlot();
    plot.setStartAngle(290);
    plot.setDirection(Rotation.CLOCKWISE);
    plot.setForegroundAlpha(0.5f);
    return chart;

}    

 /* *************************************
 *         END GRAPHICS
 *************************************/  

}

And this is the result:

reports3 http://www.freeimagehosting.net/newuploads/unolz.png

I am initializing and adding the chart in the main class constructor and right below is the code for the graph. I had to refrain from posting the entire code as it went above the character limits, however here is an url for the complete code (with the Swing components initialization by NetBeans): http://tny.cz/8a4e8b2f

Any hint for solutions?

THanks in advance and let me know if anything else is needed.

解决方案

Don't let the GUI designer dictate your design. Use it, if at all, to manage individual panels, as suggested here. It's not possible to edit a GUI project without the .form file; I just added your chart using a FlowLayout so you can see it, without altering initComponents().

import java.awt.FlowLayout;
import java.math.BigDecimal;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

public class Report extends JFrame {

    public int total_stations = 0, total_jobs = 0, total_parts = 0;
    BigDecimal[] j1_systimebig, j1_worktimebig, j1_idletimebig, j1_queuetimebig,
        j2_systimebig, j2_worktimebig, j2_idletimebig, j2_queuetimebig,
        j3_systimebig, j3_worktimebig, j3_idletimebig, j3_queuetimebig;
    public String sim_time;
    BigDecimal systime = new BigDecimal(0);
    BigDecimal worktime = new BigDecimal(0);
    BigDecimal idletime = new BigDecimal(0);
    BigDecimal queuetime = new BigDecimal(0);

    private static final String title = "Which operating system are you using?";

    /**
    * Creates new form Report
    */
    public Report() {
        initComponents();
        jPanel2.removeAll();
        jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
        jPanel2.add(createPieChart(title));
        this.setLocationRelativeTo(null);
    }

    /*
    * *************************************
    * GRAPHICS ***********************************
    */
    private ChartPanel createPieChart(String chartTitle) {
        System.out.println("PieChart");
        PieDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset, chartTitle);
        ChartPanel chartPanel = new ChartPanel(chart);
        return chartPanel;
    }

    private PieDataset createDataset() {
        System.out.println("PieDataset");
        DefaultPieDataset result = new DefaultPieDataset();
        result.setValue("Linux", 29);
        result.setValue("Mac", 20);
        result.setValue("Windows", 51);
        return result;

    }

    private JFreeChart createChart(PieDataset dataset, String title) {
        System.out.println("Create Chart");
        JFreeChart chart = ChartFactory.createPieChart3D(
            title, dataset, true, true, false);
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        plot.setCircular(true);
        return chart;

    }

    /*
    * *************************************
    * END GRAPHICS ***********************************
    */
    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        // code elided
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Report().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    public javax.swing.JPanel chartPanel1;
    public javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel10;
    public javax.swing.JLabel jLabel11;
    public javax.swing.JLabel jLabel12;
    public javax.swing.JLabel jLabel13;
    public javax.swing.JLabel jLabel14;
    public javax.swing.JLabel jLabel15;
    public javax.swing.JLabel jLabel16;
    public javax.swing.JLabel jLabel17;
    public javax.swing.JLabel jLabel18;
    public javax.swing.JLabel jLabel19;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel20;
    public javax.swing.JLabel jLabel21;
    public javax.swing.JLabel jLabel22;
    public javax.swing.JLabel jLabel23;
    public javax.swing.JLabel jLabel24;
    public javax.swing.JLabel jLabel25;
    public javax.swing.JLabel jLabel26;
    public javax.swing.JLabel jLabel27;
    public javax.swing.JLabel jLabel28;
    public javax.swing.JLabel jLabel29;
    public javax.swing.JLabel jLabel30;
    public javax.swing.JLabel jLabel4;
    public javax.swing.JLabel jLabel7;
    public javax.swing.JLabel jLabel8;
    public javax.swing.JLabel jLabel9;
    public javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    public javax.swing.JPanel jPanel3;
    public javax.swing.JPanel jPanel4;
    public javax.swing.JPanel jPanel6;
    public javax.swing.JPanel jPanel7;
    public javax.swing.JPanel jPanel8;
    public javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JScrollPane jScrollPane3;
    public javax.swing.JScrollPane jScrollPane5;
    public javax.swing.JScrollPane jScrollPane6;
    public javax.swing.JSeparator jSeparator1;
    public javax.swing.JSeparator jSeparator3;
    public javax.swing.JSeparator jSeparator4;
    public javax.swing.JSeparator jSeparator5;
    public javax.swing.JSeparator jSeparator6;
    public javax.swing.JTabbedPane jTabbedPane1;
    public javax.swing.JTable jTable1;
    public javax.swing.JTable jTable2;
    public javax.swing.JTable jTable3;
    public javax.swing.JTable jTable4;
    // End of variables declaration
}

这篇关于如何在面板上添加JFreechart?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆