简体   繁体   English

JFree Chart 将 MySQL 数据库中的数据放入 JPanel

[英]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.我正在尝试在我的程序中实现一个 JFree Chart,我目前正在开发一个 Java Swing 应用程序。 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.如您所见,我正在为数据库使用 singleton class。 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.因为ChartPanelJPanel ,所以您可以简单地将其添加到您的框架中,如下所示。 The frame's default layout is BorderLayout and the default constraint is CENTER .框架的默认布局是BorderLayout并且默认约束是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考虑使用JDBCXYDataset ,如图所示

图形

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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