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