mario.borg
mario.borg

Reputation: 141

Adding JFreeChart to a JPanel

I am creating a swing application and would like to add a JfreeChart to a Jpanel. I have created a method where I am created the Chart as one can see below:

Created a global variable:

JFreeChart chart;

Method for creating the chart:

public void createChart(){
        XYSeries series = new XYSeries("XYGraph");
           series.add(1, 1);
           series.add(1, 2);
           series.add(2, 1);
           series.add(3, 9);
           series.add(4, 10);

        // Add the series to your data set
           XYSeriesCollection dataset = new XYSeriesCollection();
           dataset.addSeries(series);

        // Generate the graph
           chart = ChartFactory.createXYLineChart(
           "XY Chart", // Title
           "x-axis", // x-axis Label
           "y-axis", // y-axis Label
           dataset, // Dataset
           PlotOrientation.VERTICAL, // Plot Orientation
           true, // Show Legend
           true, // Use tooltips
           false // Configure chart to generate URLs?
        );

    }

I then created the ChartPanel and added it to the JPanel which is attached to a JFrame:

JPanel reporting = new JPanel();
ChartPanel CP = new ChartPanel(chart);
reporting.add(CP,BorderLayout.CENTER);
reporting.validate();
reporting.setBounds(47, 59, 921, 439);
frame.getContentPane().add(reporting);
reporting.setLayout(new BorderLayout(0, 0));

I also created a button with an action listener to it where I call the method as one see below:

JButton btnReporting = new JButton("Reporting");
        btnReporting.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                createChart();
            }
        });

For some reason the chart isn't showing when I click on the button.

Upvotes: 0

Views: 1220

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

A bunch of out of context code does not make it easy to understand exactly what you are doing, but something like...

First, you need to remove anything that was previously displayed from the UI, other wise you will end up with multiple charts on screen which might cause other issues.

Next, when you create the chart, you need to actually add to the screen. It won't "magically" update just because you create a new chart

// import free chart classes
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JPanel reportingPane;

        public TestPane() {
            setLayout(new BorderLayout());
            reportingPane = new JPanel(new BorderLayout());
            JButton btnReporting = new JButton("Reporting");
            btnReporting.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    JFreeChart chart = createChart();
                    reportingPane.removeAll();
                    reportingPane.add(new ChartPanel(chart));
                    revalidate();
                    repaint();
                }
            });
            add(reportingPane);
            add(btnReporting, BorderLayout.SOUTH);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

    public JFreeChart createChart() {
        XYSeries series = new XYSeries("XYGraph");
        series.add(1, 1);
        series.add(1, 2);
        series.add(2, 1);
        series.add(3, 9);
        series.add(4, 10);

        // Add the series to your data set
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);

        // Generate the graph
        JFreeChart chart = ChartFactory.createXYLineChart(
                        "XY Chart", // Title
                        "x-axis", // x-axis Label
                        "y-axis", // y-axis Label
                        dataset, // Dataset
                        PlotOrientation.VERTICAL, // Plot Orientation
                        true, // Show Legend
                        true, // Use tooltips
                        false // Configure chart to generate URLs?
        );

        return chart;

    }

}

Should at least demonstrate the basic concept...

Upvotes: 2

Related Questions