JDBCCategoryDataset and more series within the same BarChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Krac
Posts: 17
Joined: Thu Dec 07, 2006 2:26 pm
Location: Baia Mare, Romania

JDBCCategoryDataset and more series within the same BarChart

Post by Krac » Thu Jan 18, 2007 4:06 pm

Hello, everybody :)

I try to create a BarChart using a JDBCCategoryDataset and enrich it with the results of another JDBCCategoryDataset, as follows, but without any success:

-------------------------------------------------------------
String sql1="select label, value from table where condition1";
String sql2="select value from table where condition2";
con = DriverManager.getConnection(url, "schema", "password");
JDBCCategoryDataset data1 = new JDBCCategoryDataset(con);
JDBCCategoryDataset data2 = new JDBCCategoryDataset(con);

data1.executeQuery(sql1);
data2.executeQuery(sql2);
// this creates a chart with the category=label and value=value from data1
JFreeChart chart = ChartFactory.createBarChart(
"Vertical Bar Chart", // chart title
"Category", // domain axis label
"Value", // range axis label
data, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// how can I put in the same chart also the values from the 2nd dataset?
// because I want to compare the values of the 2 series on the same chart
// tried the following but no result...

cplot = (CategoryPlot) chart.getCategoryPlot(); // the plot
cplot.setDataset(0,data1);
cplot.setDataset(1,data2);
-----------------------------------------

Any hint would be appreciated,

TIA

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Jan 18, 2007 5:54 pm

You should just write some JDBC code to query the database, and populate a DefaultCategoryDataset instance as necessary - it's more flexible that way.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Krac
Posts: 17
Joined: Thu Dec 07, 2006 2:26 pm
Location: Baia Mare, Romania

Post by Krac » Fri Jan 19, 2007 10:27 am

Thanks for the swift reply, Dave.

I understand that using JDBCCategoryDataset is not very much encouraged :), at least not for complicated stuff.

Wanted also to ask the following:

If a query uses GROUP BY statements, is there a fast solution to have as many SERIES for a chart as GROUPS in the result set (if still wanting to use JDBCCategoryDataset) ? Or just use the more flexible way with populating a DefaultCategoryDataset?

Thanks again.

Locked