Jump to content

create pie chart with variables from report


Recommended Posts

Hi friends,

I want to create a pie chart with calculated variables from ireport jrxml file. I created bar char succesfully with 

series expression : "1.Budget Sales
Category expression : new Integer(1)
Value expression : $V{ptd_budget_sales}
like that I added four seris for four bars.
I need to use same way to create pie char. but in pie chart have only one section value 
key expression
value expression
label expression
cant give more than one.
I usually pass pie char value from java datasource but now i need to  take values from calculated variables only. I googled but found similar questions without answer. Please anybody help me or any link...
I read somewhere that can create from csv file too.In that case how to create a csv file from report? I know to create from java but not from ireport ...any suggestion is appreciated..
Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Hi ,

Let me tell you one by one what does each parameter mean for pie chart:

  1. Key Expression:  This will represent the expression which will make up the slices of the pie, or could be categories  "1.Budget Sales in your scenario,  it will depend on your choice which suits your dataset
  2. Value ExpressionThis expression produces the values that correspond to each category/key in the dataset. This will signify the value for each slice or the count in each category . This will take only numbers or integer value. In you case it would be $V{ptd_budget_sales}
  3. Label Expression :  This expression is responsible for the label of each slice or category. It is an optional parameter , if you do not specify then it would take the default layout of the labels. It will take String type. In your scenario it would be new Integer(1).

Hope that this clears your doubts.





Link to comment
Share on other sites

Thanks Ankur for reply

Actually what I need is

I have three calculated variables let us say A1,A2,A3 in ireport. I want to create pie chart with A1 in one piece of pie ,A2 in second piece of pie and A3 in third piece of pie.


In bar chart we can add more than one category series(Chart properties --> chart data  -->Details --> category Series then use add button more than one time) but in pie chart( chart properties --> chart Data -->Details) don't have add button for add more than one.


Else can say how to create pie chart with report variables ?


Link to comment
Share on other sites

Actually I am using iReport 2.0.2 for generating reports. The below link


at page 60 for creating pie chart(Chart details)there is button Use more series to give more values .But in my chart it is missing ...I need to give more than one series in pie chart ireport...

Link to comment
Share on other sites

  • 5 years later...


there is a solution based on using a JRMapCollectionDataSource for your Pie Chart.

  1. You need to add a dataset to your report (add empty data source). (in ireport right clik in your report name and click on add Dataset)

  2. in the added source add tow fields for the values and for the label/name for example key an val, you will use them in you pie chart

  3. create a new pie char and choose your dataset the new adder dataset. this will allow you to find your variable key and val, to use them as key expression $F{key} and value expression $F{val)

  4. Now open you pie chart properties by righ click on the pie object => pie Data.

go to : connection / Datasource expression, and choose use data source expression 

Edit the expression to use a JRMapCollectionDataSource with your own calculated variables (in the example we uses var1, var2, var3 an var4)


new net.sf.jasperreports.engine.data.JRMapCollectionDataSource ()     [        ["key" : "a", val: $V{var1}],        ["key" : "b", val: $V{var2}],        ["key" : "c", val: $V{var3}],        ["key" : "d", val: $V{var4}]    ])[/code]

The default label uses the key, you can change it in char Data - > Details 

you can fin more details on this link :


Below the JRXML file using ireport 5.6 of the example :

<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="pie chart variables" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="0a8e5b1c-8d35-4290-a283-c7fb4e197131">    <property name="ireport.zoom" value="1.0"/>    <property name="ireport.x" value="0"/>    <property name="ireport.y" value="0"/>    <subDataset name="mydataset" uuid="37fb9ab4-ec80-4095-9857-01219a239b9b">        <field name="key" class="java.lang.String"/>        <field name="val" class="java.lang.Integer"/>    </subDataset>    <variable name="var1" class="java.lang.Integer">        <variableExpression><![CDATA[35*3]]></variableExpression>        <initialValueExpression><![CDATA[]]></initialValueExpression>    </variable>    <variable name="var2" class="java.lang.Integer">        <variableExpression><![CDATA[20*1.5]]></variableExpression>        <initialValueExpression><![CDATA[]]></initialValueExpression>    </variable>    <variable name="var3" class="java.lang.Integer">        <variableExpression><![CDATA[25*5]]></variableExpression>        <initialValueExpression><![CDATA[]]></initialValueExpression>    </variable>    <variable name="var4" class="java.lang.Integer">        <variableExpression><![CDATA[40]]></variableExpression>        <initialValueExpression><![CDATA[]]></initialValueExpression>    </variable>    <background>        <band splitType="Stretch"/>    </background>    <title>        <band height="79" splitType="Stretch"/>    </title>    <pageHeader>        <band height="35" splitType="Stretch"/>    </pageHeader>    <columnHeader>        <band height="61" splitType="Stretch"/>    </columnHeader>    <detail>        <band height="162" splitType="Stretch">            <pie3DChart>                <chart evaluationTime="Report">                    <reportElement x="135" y="16" width="238" height="125" uuid="71a39643-6af3-4124-977a-16a74385968c"/>                    <chartTitle/>                    <chartSubtitle/>                    <chartLegend/>                </chart>                <pieDataset>                    <dataset>                        <datasetRun subDataset="mydataset" uuid="a159ae9c-736c-447c-9909-f883471c59bf">                            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRMapCollectionDataSource (    [        ["key" : "a", val: $V{var1}],        ["key" : "b", val: $V{var2}],        ["key" : "c", val: $V{var3}],        ["key" : "d", val: $V{var4}]    ])]]></dataSourceExpression>                        </datasetRun>                    </dataset>                    <keyExpression><![CDATA[$F{key}]]></keyExpression>                    <valueExpression><![CDATA[$F{val}]]></valueExpression>                    <labelExpression><![CDATA[$F{key}+":"+$F{val}]]></labelExpression>                </pieDataset>                <pie3DPlot>                    <plot/>                    <itemLabel/>                </pie3DPlot>            </pie3DChart>        </band>    </detail>    <columnFooter>        <band height="45" splitType="Stretch"/>    </columnFooter>    <pageFooter>        <band height="54" splitType="Stretch"/>    </pageFooter>    <summary>        <band height="42" splitType="Stretch"/>    </summary></jasperReport>[/code]


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...