• Post Reply Bookmark Topic Watch Topic
  • New Topic

Couldn't get the values in Bar Chart using JFreechart  RSS feed

 
preetha gayathri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I am using a servlet to generate a chart using JFreechart. I get the values from the databse which has to get into the bar charts.
But i am not able to implement the logic. I checked few sites, everywhere the values has been hardcoded.
Are there any ways to get the data loaded dynamically in the chart?
Please help me out, I am really stuck up!!!

Thanks
preetha
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is your question how to get the data from the database, or how to use it to create a chart once you have the data? What do you have so far?
 
preetha gayathri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the data in my code. I need to show those values in the chart.
For sample -

I am using a bar chart to display and it takes two lables on the two axis.
I am getting the values from the database which has two columns and storing it in a list.

while (rs.next())
{
s1 = rs.getString(2);
s2 = rs.getString(3);
list.add(s1);
list1.add(s2);
}
And getting the string array from the list like this

s5 = (String[])(list.toArray(new String[list.size()]));
s6 = (String[])(list1.toArray(new String[list.size()]));

Now, to populate the datas stored in s5 and s6 in the bar chart, I am using the following code

for(int i = 0; i < s5.length;i++)
{
c.xAxis().setLabelFormat(s5[i]);
}
for(int i = 0; i < s6.length;i++)
{
c.yAxis().setLabelFormat(s6[i]);
}

But I cudn't set the values. I could get the datas in the chart if i hard code the data
Please suggest!!!

Thanks
Preetha
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read your private messages as Ben asked you to, and fix your display name. That is not optional. Please do that prior to your next post.

Here's some code that creates a chart. Instead of the repeated addValue calls you would do that in a loop that iterates through the elements in your list. You may need to tweak it if you're using a different kind of chart (and thus a different kind of Dataset implementation).
 
preetha gayathri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per your reply, i coded in this way for a sample

s5 = (String[])(list.toArray(new String[list.size()]));
s6 = (String[])(list1.toArray(new String[list.size()]));
for(int i = 0 ; i < (s5.length & s6.length) ;i++)
{
dataset.addValue(1.0,"s1[i]","s2[i]");
}
JFreeChart chart = ChartFactory.createBarChart("Test Chart","Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
BufferedImage image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle(500, 300));
ChartUtilities.writeBufferedImageAsJPEG(outter,image);



But i couldn't see any output. I dont know where i am going wrong.
Please suggest

Thanks
preetha
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dataset.addValue(1.0,"s1[i]","s2[i]");

This doesn't look right. All three value should vary, and not be constant as they are here.
 
preetha gayathri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... I have corrected that.
Now my code looks like this

s5 = (String[])(list.toArray(new String[list.size()]));
s6 = (String[])(list1.toArray(new String[list.size()]));

for(int i = 0 ; i < (s5.length & s6.length) ;i++)
{
double d =Double.parseDouble(s6[i]);
final String series1 = "First";
dateset.addValue(d,series1,s5[i]);
}

This works fine. But the data is not displayed in the format as i require.
I need to display the values of s6 to be displayed in y -axis and s5 to be displayed in x - axis.
Are there any other method which adds values to the dataset by taking only two arguments?

Thanks
Preetha
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the basic usage of JFreeChart seem to turn into a FAQ, I've put up a simple example at http://faq.javaranch.com/java/JFreeChartDemo. It creates an X/Y chart with two curves. (If you have just a single curve, remove everything that has "cosine" in it.) There's also a commented out section that creates a bar chart instead of an X/Y line chart.
[ March 20, 2008: Message edited by: Ulf Dittmer ]
 
preetha gayathri
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the desired output in the chart.
Thank you so much for your help!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!