JasperReport chart example

I am new to JasperReports and slowly struggling with my fundamentals. I have a situation where I do not want to fill the pie chart with database-driven data (through a so-called data source). I want to provide all the information needed to populate a pie chart from a Java hash map passed to JasperFillManager at runtime.

This will include parameters for marking slices in a pie chart, setting their colors and determining their weights / values ​​(slicing size). So, at some point in my Java code, I would write 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 is to have a 2-sliced ​​pie chart; the red slice of the Red Team, which occupies 67% of the pie, and the blue “blue team”, which gained 33%.

Now I need help connecting the dots between my hashmap and JRXML / JasperDesign.

Can someone show me (or just help me help) regarding which <pieChart>JRXML I need to write so that my hash file jrParamfills the pie chart with runtime parameters? I made a better attempt below, but I'm just struggling to fully understand all this.

<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 your help / clarification!

+5
source share
2 answers

zharvey,

JasperReport, , iReport Designer. , iReport, JRXML. , iReport- > Help- > Samples- > Charts

- java beans POJO ( , ). , , " ". , java bean ? , bean, JRBeanDataSource. , . !

+5

, ! , , javabean . jrml, .

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!" );
    }
}
0

All Articles