2

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.

1
  • 1
    Please take a loot at the samples of JFreeChart and their documentation to get a feel of how it works. Also, can you show us what you tried yourself? Where's your code? Commented Apr 27, 2015 at 12:18

1 Answer 1

4

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) {

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.