简体   繁体   中英

JFree Chart with data from MySQL Database into a JPanel

I am trying to implement a JFree Chart into my program, I am currently developing a Java Swing application. I want to display the chart into a panel inside the application. So, the table which I want to extract the data for the graph is as follows:


 -----------------------
| daily_total_statements|
 -----------------------
| Reference ID (PK)     |
| Value                 |
| Date                  |
 -----------------------

and the code i have used to implement the chart is:

    public void buidGraph(JPanel jp) {

        DefaultCategoryDataset dataset = createDataset();
        JFreeChart chart = ChartFactory.createLineChart(
                "Daily Progress",
                "Date", // X-Axis Label
                "Number of Members", // Y-Axis Label
                dataset
        );

        ChartPanel panel = new ChartPanel(chart);
        jp.add(panel);
    }

The dataset code i thought that would work and which i am a bit confused by it:

private DefaultCategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        try {
            String query = "SELECT `Value`, `Date` FROM `daily_total_statements` 
                            ORDER BY `Date` ASC";
            Connection c = MySQL_Database.getInstance().getConnection();
            PreparedStatement ps = c.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            String series2 = "Daily progress";
            while (rs.next()) {
                dataset.addValue(rs.getInt(1), series2, rs.getString(2));
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Problem creating chart! " + e.getMessage());
        }
        return dataset;
    }

I am using a singleton class for the database as you can see. The problem is that the chart is not displayed at all it's just an empty panel. Please if you could help! Sorry for the bad formatting and language!

Because a ChartPanel is a JPanel , you can simply add it to your frame, as shown below. The frame's default layout is BorderLayout and the default constraint is CENTER . Moreover,

  • Use one of the approaches seen here to establish the chart's initial size.
  • Use an appropriate layout to control resize behavior.
  • Consider using JDBCXYDataset , illustrated here

图形

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class GraphTest extends JFrame {

    public GraphTest() {
        initComponents();
    }

    private void initComponents() {
        add(buildLineGraph());
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(GraphTest::new);
    }

    public ChartPanel buildLineGraph() {
        DefaultCategoryDataset dataset = createLineGraphDataset();
        JFreeChart chart = ChartFactory.createLineChart("Daily Progress",
            "Day", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
        return new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
    }

    private DefaultCategoryDataset createLineGraphDataset() {
        // TODO: use JDBCXYDataset
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1, "Result", "Mon");
        dataset.addValue(5, "Result", "Tue");
        dataset.addValue(7, "Result", "Wed");
        return dataset;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM