Pie chart is showing wrong value for key
I am developing a program that create a report with data of diferents persons stored in a database.
The report is created fine, but the chart isn't correct. I'm trying to show the distribution of the professions.
This is my report:
As you can see, all the professions have the same count. It's wrong because Cantante and Profesor appear two times.
I define the chart in iReport of the following way:
How to fix it?
jasper-reports pie-chart
add a comment |
I am developing a program that create a report with data of diferents persons stored in a database.
The report is created fine, but the chart isn't correct. I'm trying to show the distribution of the professions.
This is my report:
As you can see, all the professions have the same count. It's wrong because Cantante and Profesor appear two times.
I define the chart in iReport of the following way:
How to fix it?
jasper-reports pie-chart
add a comment |
I am developing a program that create a report with data of diferents persons stored in a database.
The report is created fine, but the chart isn't correct. I'm trying to show the distribution of the professions.
This is my report:
As you can see, all the professions have the same count. It's wrong because Cantante and Profesor appear two times.
I define the chart in iReport of the following way:
How to fix it?
jasper-reports pie-chart
I am developing a program that create a report with data of diferents persons stored in a database.
The report is created fine, but the chart isn't correct. I'm trying to show the distribution of the professions.
This is my report:
As you can see, all the professions have the same count. It's wrong because Cantante and Profesor appear two times.
I define the chart in iReport of the following way:
How to fix it?
jasper-reports pie-chart
jasper-reports pie-chart
edited Nov 19 '18 at 21:06
Alex K
18.5k1480159
18.5k1480159
asked Nov 19 '18 at 8:29
user3118887user3118887
35111
35111
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.
If you are using groups at JasperReports you should always remember about data sorting.
Looks like all you need is to sort data by profession field.
Example built with Jaspersoft Studio 7.1.0
Datasource
Using simple csv datasource is enough for example.
The content of my professions.csv file is very simple. The first row is going for fields names.
name,age,profession
Miguel,25,Professor
Toni,22,Engineer
Josefa,34,Singer
Andres,27,Mechanic
Jony,41,Professor
Lola,33,Singer
The name of dataadapter at JSS for my example will be professions.csv.
The report template, jrxml file
I used main dataset at pie chart for showing information.
<?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="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
<property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="profession" class="java.lang.String"/>
<sortField name="profession"/>
<group name="ProfessionGroup">
<groupExpression><![CDATA[$F{profession}]]></groupExpression>
</group>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Age]]></text>
</staticText>
<staticText>
<reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Profession]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
<textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="240" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
</jasperReport>
The key features of this example are:
using net.sf.jasperreports.chart.pie.ignore.duplicated.key property,
using group by profession field
and applying sorting by this (profession) field.
If not all values of profession field are unique you will get an error:
net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.
The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
Without sorting the data (just remove <sortField name="profession"/>
row from jrxml) the result will be wrong, like at your screenshot:
After applying sorting, for example with help of code
<sortField name="profession"/>
we will get the right result:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370846%2fpie-chart-is-showing-wrong-value-for-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.
If you are using groups at JasperReports you should always remember about data sorting.
Looks like all you need is to sort data by profession field.
Example built with Jaspersoft Studio 7.1.0
Datasource
Using simple csv datasource is enough for example.
The content of my professions.csv file is very simple. The first row is going for fields names.
name,age,profession
Miguel,25,Professor
Toni,22,Engineer
Josefa,34,Singer
Andres,27,Mechanic
Jony,41,Professor
Lola,33,Singer
The name of dataadapter at JSS for my example will be professions.csv.
The report template, jrxml file
I used main dataset at pie chart for showing information.
<?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="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
<property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="profession" class="java.lang.String"/>
<sortField name="profession"/>
<group name="ProfessionGroup">
<groupExpression><![CDATA[$F{profession}]]></groupExpression>
</group>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Age]]></text>
</staticText>
<staticText>
<reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Profession]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
<textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="240" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
</jasperReport>
The key features of this example are:
using net.sf.jasperreports.chart.pie.ignore.duplicated.key property,
using group by profession field
and applying sorting by this (profession) field.
If not all values of profession field are unique you will get an error:
net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.
The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
Without sorting the data (just remove <sortField name="profession"/>
row from jrxml) the result will be wrong, like at your screenshot:
After applying sorting, for example with help of code
<sortField name="profession"/>
we will get the right result:
add a comment |
Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.
If you are using groups at JasperReports you should always remember about data sorting.
Looks like all you need is to sort data by profession field.
Example built with Jaspersoft Studio 7.1.0
Datasource
Using simple csv datasource is enough for example.
The content of my professions.csv file is very simple. The first row is going for fields names.
name,age,profession
Miguel,25,Professor
Toni,22,Engineer
Josefa,34,Singer
Andres,27,Mechanic
Jony,41,Professor
Lola,33,Singer
The name of dataadapter at JSS for my example will be professions.csv.
The report template, jrxml file
I used main dataset at pie chart for showing information.
<?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="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
<property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="profession" class="java.lang.String"/>
<sortField name="profession"/>
<group name="ProfessionGroup">
<groupExpression><![CDATA[$F{profession}]]></groupExpression>
</group>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Age]]></text>
</staticText>
<staticText>
<reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Profession]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
<textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="240" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
</jasperReport>
The key features of this example are:
using net.sf.jasperreports.chart.pie.ignore.duplicated.key property,
using group by profession field
and applying sorting by this (profession) field.
If not all values of profession field are unique you will get an error:
net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.
The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
Without sorting the data (just remove <sortField name="profession"/>
row from jrxml) the result will be wrong, like at your screenshot:
After applying sorting, for example with help of code
<sortField name="profession"/>
we will get the right result:
add a comment |
Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.
If you are using groups at JasperReports you should always remember about data sorting.
Looks like all you need is to sort data by profession field.
Example built with Jaspersoft Studio 7.1.0
Datasource
Using simple csv datasource is enough for example.
The content of my professions.csv file is very simple. The first row is going for fields names.
name,age,profession
Miguel,25,Professor
Toni,22,Engineer
Josefa,34,Singer
Andres,27,Mechanic
Jony,41,Professor
Lola,33,Singer
The name of dataadapter at JSS for my example will be professions.csv.
The report template, jrxml file
I used main dataset at pie chart for showing information.
<?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="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
<property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="profession" class="java.lang.String"/>
<sortField name="profession"/>
<group name="ProfessionGroup">
<groupExpression><![CDATA[$F{profession}]]></groupExpression>
</group>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Age]]></text>
</staticText>
<staticText>
<reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Profession]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
<textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="240" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
</jasperReport>
The key features of this example are:
using net.sf.jasperreports.chart.pie.ignore.duplicated.key property,
using group by profession field
and applying sorting by this (profession) field.
If not all values of profession field are unique you will get an error:
net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.
The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
Without sorting the data (just remove <sortField name="profession"/>
row from jrxml) the result will be wrong, like at your screenshot:
After applying sorting, for example with help of code
<sortField name="profession"/>
we will get the right result:
Looks like you are using net.sf.jasperreports.chart.pie.ignore.duplicated.key property but forgot to sort data at dataset.
If you are using groups at JasperReports you should always remember about data sorting.
Looks like all you need is to sort data by profession field.
Example built with Jaspersoft Studio 7.1.0
Datasource
Using simple csv datasource is enough for example.
The content of my professions.csv file is very simple. The first row is going for fields names.
name,age,profession
Miguel,25,Professor
Toni,22,Engineer
Josefa,34,Singer
Andres,27,Mechanic
Jony,41,Professor
Lola,33,Singer
The name of dataadapter at JSS for my example will be professions.csv.
The report template, jrxml file
I used main dataset at pie chart for showing information.
<?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="NotSortedDataChart" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="09483b77-da8e-4dbb-a87d-ae46c27df140">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="professions.csv"/>
<property name="net.sf.jasperreports.chart.pie.ignore.duplicated.key" value="true"/>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.String"/>
<field name="profession" class="java.lang.String"/>
<sortField name="profession"/>
<group name="ProfessionGroup">
<groupExpression><![CDATA[$F{profession}]]></groupExpression>
</group>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="120" height="15" uuid="edda065f-1a67-4f94-a2e9-1fc0042e4d32"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="120" y="0" width="145" height="15" uuid="95bf7063-fe77-48d6-924a-e0028b8ec0c4"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Age]]></text>
</staticText>
<staticText>
<reportElement x="245" y="0" width="125" height="15" uuid="97f91177-abd3-416d-abb1-e8692f96dc5c"/>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<text><![CDATA[Profession]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="120" height="15" uuid="b10df174-e36e-402d-ad52-4aa15dbebd49"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="0" width="125" height="15" uuid="5c66bca9-34af-49a6-a4fe-87c4e945f31b"/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="245" y="0" width="145" height="15" uuid="d169f529-349c-4c84-b697-680048eabd94"/>
<textFieldExpression><![CDATA[$F{profession}]]></textFieldExpression>
</textField>
</band>
</detail>
<summary>
<band height="240" splitType="Stretch">
<pieChart>
<chart evaluationTime="Report">
<reportElement x="50" y="40" width="200" height="200" uuid="7454b149-6176-4070-8f9b-dd10bbd8f47a"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset>
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
</pieDataset>
<piePlot>
<plot/>
<itemLabel/>
</piePlot>
</pieChart>
</band>
</summary>
</jasperReport>
The key features of this example are:
using net.sf.jasperreports.chart.pie.ignore.duplicated.key property,
using group by profession field
and applying sorting by this (profession) field.
If not all values of profession field are unique you will get an error:
net.sf.jasperreports.engine.JRRuntimeException: Key Professor is duplicated in pie dataset.
The using net.sf.jasperreports.chart.pie.ignore.duplicated.key property allow us to build report even with duplicated values calculated by key expression:
<keyExpression><![CDATA[$F{profession}]]></keyExpression>
<valueExpression><![CDATA[$V{ProfessionGroup_COUNT}]]></valueExpression>
<labelExpression><![CDATA[$V{ProfessionGroup_COUNT}.toString()]]></labelExpression>
Without sorting the data (just remove <sortField name="profession"/>
row from jrxml) the result will be wrong, like at your screenshot:
After applying sorting, for example with help of code
<sortField name="profession"/>
we will get the right result:
edited Nov 19 '18 at 21:53
answered Nov 19 '18 at 21:40
Alex KAlex K
18.5k1480159
18.5k1480159
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370846%2fpie-chart-is-showing-wrong-value-for-key%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown