How to add two columns to the chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

How to add two columns to the chart

Postby 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
msksanthu
 
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm

Re: How to add two columns to the chart

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

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

Re: How to add two columns to the chart

Postby 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
msksanthu
 
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm

Re: How to add two columns to the chart

Postby 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.
John Matthews
 
Posts: 367
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to add two columns to the chart

Postby msksanthu » Mon Aug 07, 2017 2:29 pm

Thanks for your information
msksanthu
 
Posts: 3
Joined: Fri Aug 04, 2017 4:07 pm

Re: How to add two columns to the chart

Postby vincymol » Wed Aug 09, 2017 12:04 pm

Thanks for sharing this information..
vincymol
 
Posts: 1
Joined: Wed Aug 09, 2017 11:46 am


Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests