Reputation: 56944
I am brand new to JasperReports and am slowly fighting my way through the basics. I have a situation where I do not want to fill a pie chart with DB-driven data (through a so-called datasource). I want to supply all the information necessary to fill the pie chart from a Java hashmap passed into the JasperFillManager at runtime.
This would include parameters to label the slices in the pie chart, set their colors, and define their weights/values (size of the slices). So, at some point in my Java code, I would be writing something like:
HashMap<String,Object> jrParams = new HashMap<String,Object>();
jpParams.put("slice_1_label", "Red Team");
jpParams.put("slice_1_color", Color.RED);
jpParams.put("slice_1_value", 67.0);
jpParams.put("slice_2_label", "Blue Team");
jpParams.put("slice_2_color", Color.BLUE);
jpParams.put("slice_2_value", 33.0);
// ... some other code
JasperFillManager.fillReport(jasperDesign, jrParams);
The goal I am trying to achieve here would be to have a pie chart with 2 slices; a red "Red Team" slice taking up 67% of the pie, and a blue "Blue Team" slice takig up 33%.
I now need help "connecting the dots" between my hashmap and the JRXML/JasperDesign.
Can someone either show me (or just help guide me) towards what sort of <pieChart>
JRXML I would need to write in order to have my jrParam
hashmap fill the pie chart with runtime parameters? I have made a best-attempt below but am just struggling on making total sense of it all.
<pieChart>
<chart isShowLegend="true">
<reportElement x="10" y="10" width="300" height="300"/>
<chartTitle>
<titleExpression><![CDATA[My First JR Pie Chart]]></titleExpression>
</chartTitle>
</chart>
<pieDataset>
<!-- Here is where I believe I need to put my two slices; not sure how -->
</pieDataset>
<piePlot>
<plot backcolor="#8BA870"/>
<itemLabel color="#000000"/>
</piePlot>
</pieChart>
Thanks in advance for any help/clarification!
Upvotes: 5
Views: 12572
Reputation: 2021
See whether this code helps you! I've created a bar graph using the below code which uses javabean as data source. If you want to go through my jrml I can provide that also.
public class App{
String cname;
int mark;
public void setCname(String cname){
this.cname = cname;
}
public void setMark(int mark){
this.mark = mark;
}
public String getCname(){
return cname;
}
public int getMark(){
return mark;
}
public static ArrayList<App> getDetails() {
ArrayList<App> clist= new ArrayList<App>();
App c1 = new App();
c1.setCname("English");
c1.setMark(58);
clist.add(c1);
c1 = new App();
c1.setCname("Social Studies");
c1.setMark(68);
clist.add(c1);
c1 = new App();
c1.setCname("Culture");
c1.setMark(78);
clist.add(c1);
c1 = new App();
c1.setCname("Maths");
c1.setMark(78);
clist.add(c1);
c1 = new App();
c1.setCname("Physics");
c1.setMark(100);
clist.add(c1);
return(clist);
}
public static void main( String[] args ) {
JasperReport jasperReport;
JasperPrint jasperPrint;
Map<String, Object> param = new HashMap<String, Object>();
try {
String sourceFileName = ".jrxml";
jasperReport = JasperCompileManager.compileReport(sourceFileName);
jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JRBeanCollectionDataSource(getDetails()));
JasperExportManager.exportReportToPdfFile(jasperPrint, ".pdf");
}
catch(Exception e){
}
System.out.println( "Hello World!" );
}
}
Upvotes: 0
Reputation: 4324
zharvey,
Since you are pretty new to JasperReport i am hoping you are using the iReport Designer tool. Design a pie chart from one of the sample templates that the iReport designer has and then try to study the generated JRXML. There is a very easy sample you can look at when going to iReport->Help->Samples->Charts
My second tip for you is to use java beans or POJOs as a datasource (simply based on the code you posted). I am looking at your sample code where you are creating a Map of String label, color and values. Wouldn't be nice and easy if you had a plain old java bean class with all these three properties as instance variable? It would be easy for your report as well, because you can create a collection of bean objects which can easily be accessed and used via the JRBeanDataSource. I feel it will make your code a little cleaner and easier to maintain. Regards!
Upvotes: 5