How to add two columns to the chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
msksanthu
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm
antibot: No, of course not.

How to add two columns to the chart

Post by msksanthu » Fri Aug 04, 2017 4:20 pm

Hi All,
I need to add 2 columns to the chat in x axis by grouping using database . I am new to Jfree chart . Please help to add two columns in x axis by grouping .

my query will display data like the below table ... i need to do group by in chart for business and status columns in x axis & count should display in y axis .
business status count
a test1 23
a test2 45
b test1 66
b test3 70
c test2 82
c test3 99
d test4 100



Pivot table

Row Labels Sum of count
a 68
test1 23
test2 45
b 136
test1 66
test3 70
c 181
test2 82
test3 99
d 100
test4 100
Grand Total 485

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to add two columns to the chart

Post by paradoxoff » Fri Aug 04, 2017 5:43 pm

Something like this:

Code: Select all

public class SimpleBarChart {

    public static void main(String[] args) {
        DefaultCategoryDataset dcd = new DefaultCategoryDataset();
        dcd.addValue(23, "A", "Test1");
        dcd.addValue(10, "A", "Test2");
        dcd.addValue(66, "B", "Test1");
        dcd.addValue(70, "B", "Test3");
        dcd.addValue(82, "C", "Test2");
        dcd.addValue(99, "C", "Test3");
        dcd.addValue(100, "D", "Test4");
        BarRenderer br = new BarRenderer();
        CategoryPlot plot = new CategoryPlot(dcd, new CategoryAxis("Category"), new NumberAxis("Values"), br);
        JFreeChart chart = new JFreeChart(plot);
        ChartPanel cp = new ChartPanel(chart);
        JFrame f = new JFrame("Bar Chart");
        f.getContentPane().add(cp);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

}

msksanthu
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm
antibot: No, of course not.

Re: How to add two columns to the chart

Post by msksanthu » Mon Aug 07, 2017 10:19 am

Thanks For your reply ...these values are coming from database .. i need to connect with database and prepare the chart ... Please help me on that ... I not need of DefaultCategoryDataset() instead i need to connect to database and display chart

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to add two columns to the chart

Post by John Matthews » Mon Aug 07, 2017 12:02 pm

msksanthu wrote: I do not need DefaultCategoryDataset; instead, I need to connect to database and display chart
.

You may want to look at JDBCCategoryDataset, which is "A CategoryDataset implementation over a database JDBC result set." Although off-topic, this related example may offer some insight in formulating your query.

msksanthu
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm
antibot: No, of course not.

Re: How to add two columns to the chart

Post by msksanthu » Mon Aug 07, 2017 2:29 pm

Thanks for your information

Locked