how to insert a JFreeChart object to make chart?

Hello all, i want to get an existed JFreeChart object from inputstream & then output chart in my report, rather then make it at jrxml file. how could I do it? please, it's urgent. Thanks in advance.
Kappa's picture
197
Joined: Oct 11 2006 - 11:32am
Last seen: 16 years 11 months ago

18 Answers:

You can easily do this by reading the JFreeChart object and wrapping it in a JFreeChartRenderer. Then all you need to do is to use the JFreeChartRenderer instance as image expression (the image expression should have net.sf.jasperreports.engine.JRRenderable as type).

HTH,
Lucian
lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 44 min ago
sorry, but i cannot find JFreeChartRenderer at JFreeChart's API, could you please show me more in detail, thanks.
Kappa's picture
197
Joined: Oct 11 2006 - 11:32am
Last seen: 16 years 11 months ago
JFreeChartRenderer is a JR class: net.sf.jasperreports.renderers.JFreeChartRenderer
lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 44 min ago
sorry I'm a newbie of it, but I still has trouble.
I added a new parameter "jfcRenderer", set image expression: $P{jfcRenderer}, both net.sf.jasperreports.engine.JRRenderable as type.
This is test code fragment:
JFreeChart jfreechart = createChart(createDataset());
JFreeChartRenderer jfcRenderer = new JFreeChartRenderer(jfreechart);

try{
String xmlPath = System.getProperty("user.dir")...;
JasperReport jasperReport = JasperCompileManager.compileReport(xmlPath);

Map param = new HashMap();
param.put("jfcRenderer" , jfcRenderer);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param);
...

but I got a blank export PDF file, no image in it. please tell me how to fix it, thanks.
Kappa's picture
197
Joined: Oct 11 2006 - 11:32am
Last seen: 16 years 11 months ago
The code looks fine.

Maybe you have a report layout problem. On which band did you put the image? If you change the image source to point to an image file or URL, does it get displayed?
lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 44 min ago
i've tried change its to an image file but didn't show it,neither. It's too strange, I've packed the test java and jrxml file as attachment, please help me check them. Sorry for my English, thanks again. [file name=src.zip size=2792]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/src...
Post edited by: Kappa, at: 2006/10/13 08:50
Kappa's picture
197
Joined: Oct 11 2006 - 11:32am
Last seen: 16 years 11 months ago
The image is on the detail band, but you don't supply any data to the report.

You can either
  • put the image in the title band and set whenNoDataType="AllSectionsNoDetail"

  • provide some data when filling the report, e.g. new JREmptyDataSource

  • etc
  • [/ul]

    HTH,
    Lucian
lucianc's picture
86885
Joined: Jul 17 2006 - 1:10am
Last seen: 2 hours 44 min ago
tons of great thanks, it works fine now. I'm a silly guy.:P
Kappa's picture
197
Joined: Oct 11 2006 - 11:32am
Last seen: 16 years 11 months ago

Hi!

I've tried to overtake the samples of the code posted before, but when running the report, no pages could be printed, although I have created a new dataset.

Thanks in advance,

Julia 

 



Post Edited by jkalten at 07/13/2009 09:27
jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

Hi,

 

Please create a new thread instead of posting to an existing thread which might not be related to your current problem.

You get an empty report because you do not provide any data source for the report and the report template has whenNoDataType="NoPages".

Set whenNoDataType="AllSectionsNoDetail" in your report and things should change.

 

I hope this helps.
Teodor

 

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 1 hour ago

Hi!

I'm sorry - I've messed it up. I wanted to delete my post and instead of beginning a new thread without noticing that you've already answered. 

               "whenNoDataType="AllSectionsNoDetail" in your report and things should change."

I've already done this: Abschnitte --> no Data -->  Anzeigen wenn "AllSectionsNoDetail", or is this not correct?

Thanks in advance!

 

 

jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

Hi!

I've tried to overtake the samples of the code posted before, but when running the report, no pages could be printed, although I have created a new dataset and tried to set whenNoDataType="AllSectionsNoDetail"

Thanks in advance,

Julia 



Post Edited by jkalten at 07/13/2009 09:29
jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

And where exactly did you set the whenNoDataType attribute?

If you open the files you sent, you can clearly see it being:

whenNoDataType="NoPages"

 

We are running in circles here...

 

Thanks,

Teodor

 

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 1 hour ago

Hi!

Thank you - I've found out how to set whenNoDataType="AllSectionsNoDetail".

One thing, I still don't understand is the fact, that I have already defined some data in the ChartTest.java:

public static void createDataset() {
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset = new JREmptyDataSource();
  dataset.setValue("One", new Double(43.2));
  dataset.setValue("Two", new Double(10.0));
  dataset.setValue("Three", new Double(27.5));
  dataset.setValue("Four", new Double(17.5));
  dataset.setValue("Five", new Double(1.8));
  return dataset;
 }
 

Isn't this already our datasource?

Thanks in advance - It's my first time to include an exteranal chart with JFreeChart into IReport. 

Julia



Post Edited by jkalten at 07/16/2009 08:18
jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

Hi,

 

You are mixing up things and you need to read the freely available JasperReports documentation to understand what reports are and how they work. Only then you should try put charts in those reports.

Your data represents the JFreeChart dataset required by your chart. It has nothing to do with the data source that the report expects. You are not passing any data source to the report, are you?

Please check the JR API and see there are many other fill methods in the JasperFillManager which take a data source or a connection as parameter. You are using the one that takes no data source or connection.

 

I'm not even sure why you create the chart object yourself. Didn't you see you can use a chart element and design it visually with the iReport designer? Check the /demo/samples/charts sample provided with the JR project package.

 

I hope this helps.

Teodor

 

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 1 hour ago

Hi!

Thank you - I've solved my problem!

Kind regards - Julia 

jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

Hi!

I have created a waterfallchart-diagram with JFreeChart and in connection with .jrxml (diagramm is inserted in the report as an image). I'm working with iReport 3.5.2 and Jasperserver 3.5.0. I know, that this isn't the Jasperserver-forum, but maybe you can give me a hint: I want to be able to execute this report with the appropriate java-File in Jasperserver from the Webbrowser. I have changed the java-file to an executeable .jar File and uploaded it to the repository of Jasperserver as a resource. Then I've connected this resource with that .jrxml File. When executing this report, I just get all the surroundigns, which I have formerly defined in iReports, without the diagram. Which further steps have to be taken, in order to be able to see the diagram there?

Hope you can help me.

Kind regards, Julia

jkalten's picture
400
Joined: Jul 1 2009 - 2:21am
Last seen: 14 years 2 months ago

Hi,

 

Have you tested the report with HTML preview in iReport? It could be that the chart does not appear in HTML because it overlaps with some other content and the HTML is a grid exporter with the limitations mentioned here:

http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/faq.html?group_id=252#FAQ26

I hope this helps.

Teodor

 

P.S. Even if it appears to be the same problem, it is always better to create a new thread on the forums, instead of adding to what is already a very long discussion.

 

teodord's picture
53198
Joined: Jun 30 2006 - 9:00am
Last seen: 1 day 1 hour ago
Feedback