CHARTING WITH JASPER REPORTS

 This is a tutorial on how to create charts (Pie and bar Charts) in Jasper Reports using  a Java Object ( with fields, cname and mark)  as a datasoure

We will be using ireport 3.0 and Netbeans 6.1  as the development environment. Make sure to add the jasper report libraries to your projects.

Design your report by creating the necessary fields (cname, mark) in the ireport.  Make sure the names of the field are the same as they appear in the entity class (the datasource) including capitalization

Drag a chart (using the chart tool on the tool bar) onto your report (make sure to place a bar chart at the summary band on the report designer, a pie chart can be placed in the pageHeader band). For a bar chart , below is a picture showing how to set the properties (the series expression uses the field that should be on the horizontal axis, the category expression is a field to group the fields on the horizontal axis, eg if subjects are on the horizontal axis, but you want to group them into 1st term , 2nd term, then a field “term”  will be used as the category expression. The value expression will be the field to be displayed vertically and the label expression will be the label names for your bars)

 

 

 

 

 

 

 

 With a pie Chart, for example if you are to create a pie chart with these values (maths:30, science:20), the key expression is the name field and the value expression been the values field. Below is a picture showing this.

 Setting pie chart properties

Then you go to netbeans to do some small coding.

Create a class with field cname and mark, where cname as of type sting and mark is double

Write the code below under an actionevent

    Map<String, Object> param = new HashMap<String, Object>();

    List<CourseMarks> clist = new ArrayList<CourseMarks>();

 //CourseMArks  is the class with the two files   

   CourseMarks c1 = new CourseMarks();

    c1.setCname(“English”);

    c1.setMark(58.00);

    clist.add(c1);

   

    c1 = new CourseMarks();

    c1.setCname(“Social  Studies”);

    c1.setMark(68.00);

    clist.add(c1);

   

    c1 = new CourseMarks();

    c1.setCname(“Culture”);

    c1.setMark(78.00);

    clist.add(c1);

   

    c1 = new CourseMarks();

    c1.setCname(“Maths”);

    c1.setMark(38.00);

    clist.add(c1);

           

    c1 = new CourseMarks();

    c1.setCname(“Twi”);

    c1.setMark(88.00);

    clist.add(c1);  

 generateReports(“charting”, clist, param);

 

Below is the detail of the generateReports Method

private void generateReports(String name, Collection data, Map param) {

        try {

            String source = “C:/sabonay/jasperreports/” + name + “.jrxml”;

            if (new File(source).exists() == false) {

                xputils.showMessage(“Please go to setting and Choose report Source”);

                return;

            }

            JRBeanCollectionDataSource jrbc = new JRBeanCollectionDataSource(data);

            JasperReport jasperReport = JasperCompileManager.compileReport(source);

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

            JasperViewer.viewReport(jasperPrint, false);

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println(“reports Error  ” + e.toString());

        }

    }

Below is a sample report when the app is run

After running the App
After running the App
Advertisements

10 thoughts on “CHARTING WITH JASPER REPORTS

  1. Your bar chart appears to have Cname used for both the series and the category. It should be set as one or the other but not both. The other can be set as a constant in this case.

    • thanks, the cname was just there for the explanation of the category field ( thus showing the series fields in groups, but as u rightly said a constant should have been better

  2. Hi Gilbert Adjin Frimpong ,I tried the process u given in net beans and also i included the jfreechart.jar file to my application .but it giving the error
    java.lang.NoClassDefFoundError: Lorg/jfree/chart/plot/PlotOrientation;

    • Hi ,

      To be on the safe side make sure you add the all libraries in the “dist” folder and the ones in the “lib” folder of the jasper reports folder.

      From the error message, it seems some library is missing.

  3. Hi.. I am getting the following error
    net.sf.jasperreports.engine.JRException: Error retrieving field value from bean :

    Caused by: java.lang.NoSuchMethodException: Unknown property ”

    Some thought – From the code above the Map variable ‘param’ has not been put any value.

  4. Hello,

    I’m looking for some more tutorial for creating xy and bar charts but can’t find over the internet, somebody have a good reference?

    thanks!
    Assaf

  5. Hello,
    Im having problem displaying the charts. If I have 2 items in the clist then it is displaying two piecharts for me.
    Following is my jrxml.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s