Rajeev
Rajeev

Reputation: 120

Create bar chart with database values using JFreeChart

I am fetching values from my database which has two columns. I wanted to create a bar chart on click of a button and display the bar chart in a panel. How do I do it using JFreeChart? I'm using mysql database.

Upvotes: 2

Views: 3895

Answers (1)

Rajeev
Rajeev

Reputation: 120

I solved my problem. Just had to read a little about JFreechart. Here's the solution :

    String toc = "";
    int summary = 0;
    try {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Class.forName(JDBC_DRIVER);

        con = DriverManager.getConnection(DB_URL, USER, PASS);

        String query = "Select toc as TypeOfCall,Sum(toc) as SummaryOfCalls from processeddata_table group by toc";

        ps = con.prepareStatement(query);
        rs = ps.executeQuery();
        while (rs.next()) {
            toc = rs.getString("TypeOfCall");
            summary = rs.getInt("SummaryOfCalls");
            dataset.setValue(summary, toc, toc);
        }
        JFreeChart chart = ChartFactory.createBarChart("Call cost", "TypeOfCall", "SummaryOfCalls", dataset, PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot p = chart.getCategoryPlot();

         //p.setRangeGridlinePaint(Color.BLUE);

        ChartPanel panel = new ChartPanel(chart);
        panel.setVisible(true);
        panel.setSize(200, 200);
        display_graph.add(panel);

    } catch (ClassNotFoundException | SQLException e) {

    }

Upvotes: 4

Related Questions