Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

Graph

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;
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!