我使用Jasper Report 5.2,我希望生成PieChart并显示2位小数的百分比。
我用一个饼图创建jrxml,数据源填充参数饼图类型List。
jrxml是:
<?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="Actions" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="70dacc07-184f-471e-ba3c-bd39e77f432b">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<import value="com.gsc.audit.core.actionplan.Action"/>
<import value="com.gsc.audit.util.Triple"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#EFF7FF"/>
</conditionalStyle>
</style>
<subDataset name="pieDataset" >
<parameter name="close_label" class="java.lang.String"/>
<parameter name="late_label" class="java.lang.String"/>
<parameter name="open_label" class="java.lang.String"/>
<field name="third" class="java.lang.Integer"/>
<field name="second" class="java.lang.Integer"/>
<field name="first" class="java.lang.Integer"/>
</subDataset>
<subDataset name="tableDataset" >
<parameter name="create_date_label" class="java.lang.String"/>
<parameter name="action_plan_label" class="java.lang.String"/>
<parameter name="state_label" class="java.lang.String"/>
<parameter name="problem_label" class="java.lang.String"/>
<field name="first" class="java.lang.String"/>
<field name="second" class="java.lang.String"/>
<field name="third" class="com.gsc.audit.core.actionplan.Action"/>
</subDataset>
<parameter name="now" class="java.lang.String"/>
<parameter name="user" class="java.lang.String"/>
<parameter name="images_dir" class="java.lang.String"/>
<parameter name="report_title" class="java.lang.String"/>
<parameter name="footer_label" class="java.lang.String"/>
<parameter name="platform_label" class="java.lang.String"/>
<parameter name="platform" class="java.lang.String"/>
<parameter name="pole" class="java.lang.String"/>
<parameter name="pole_label" class="java.lang.String"/>
<parameter name="create_date_label" class="java.lang.String"/>
<parameter name="start_date" class="java.util.Date"/>
<parameter name="end_date" class="java.util.Date"/>
<parameter name="to_label" class="java.lang.String"/>
<parameter name="piechart" class="java.util.List"/>
<parameter name="table" class="java.util.List"/>
<parameter name="action_plan_label" class="java.lang.String"/>
<parameter name="state_label" class="java.lang.String"/>
<parameter name="problem_label" class="java.lang.String"/>
<parameter name="filter_label" class="java.lang.String"/>
<parameter name="open_label" class="java.lang.String"/>
<parameter name="late_label" class="java.lang.String"/>
<parameter name="close_label" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="30" splitType="Stretch">
<image scaleImage="RealSize">
<reportElement mode="Transparent" x="0" y="0" width="200" height="30"/>
<imageExpression><![CDATA[$P{images_dir} + "rigor.jpg"]]></imageExpression>
</image>
<image scaleImage="RealSize">
<reportElement x="457" y="0" width="98" height="30"/>
<imageExpression><![CDATA[$P{images_dir} + "logo_pgo.png"]]></imageExpression>
</image>
</band>
</title>
<pageHeader>
<band height="54" splitType="Stretch">
<printWhenExpression><![CDATA[$V{PAGE_NUMBER} == 1]]></printWhenExpression>
<rectangle radius="0">
<reportElement x="0" y="20" width="555" height="30" forecolor="#000000" backcolor="#FFFFFF"/>
<graphicElement>
<pen lineWidth="1.0" lineStyle="Solid" lineColor="#999999"/>
</graphicElement>
</rectangle>
<textField>
<reportElement x="142" y="34" width="135" height="12"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{pole}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="142" y="22" width="135" height="12" forecolor="#666666"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{pole_label}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="0" width="555" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="styled">
<font fontName="Arial Black" size="14" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{report_title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2" y="34" width="135" height="12"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{platform}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="2" y="22" width="135" height="12" forecolor="#666666"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{platform_label}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="283" y="22" width="270" height="12" forecolor="#666666"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{create_date_label}]]></textFieldExpression>
</textField>
<textField pattern="yyyy/MM/dd" isBlankWhenNull="true">
<reportElement x="283" y="34" width="100" height="12"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{start_date}]]></textFieldExpression>
</textField>
<textField pattern="yyyy/MM/dd" isBlankWhenNull="true">
<reportElement x="453" y="34" width="100" height="12"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{end_date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="383" y="34" width="70" height="12" forecolor="#666666"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{to_label}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="280">
<pie3DChart>
<chart>
<reportElement x="0" y="0" width="555" height="200"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<pieDataset maxCount="3">
<dataset>
<datasetRun subDataset="pieDataset" >
<datasetParameter name="close_label">
<datasetParameterExpression><![CDATA[$P{close_label}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="late_label">
<datasetParameterExpression><![CDATA[$P{late_label}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="open_label">
<datasetParameterExpression><![CDATA[$P{open_label}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{piechart})]]></dataSourceExpression>
</datasetRun>
</dataset>
<pieSeries>
<keyExpression><![CDATA[$P{late_label}]]></keyExpression>
<valueExpression><![CDATA[$F{first}]]></valueExpression>
<labelExpression><![CDATA[$P{late_label}]]></labelExpression>
</pieSeries>
<pieSeries>
<keyExpression><![CDATA[$P{open_label}]]></keyExpression>
<valueExpression><![CDATA[$F{second}]]></valueExpression>
<labelExpression><![CDATA[$P{open_label}]]></labelExpression>
</pieSeries>
<pieSeries>
<keyExpression><![CDATA[$P{close_label}]]></keyExpression>
<valueExpression><![CDATA[$F{third}]]></valueExpression>
<labelExpression><![CDATA[$P{close_label}]]></labelExpression>
</pieSeries>
</pieDataset>
<pie3DPlot legendLabelFormat="{1} ({2})">
<plot>
<seriesColor seriesOrder="0" color="#FA0526"/>
<seriesColor seriesOrder="1" color="#1105FA"/>
<seriesColor seriesOrder="2" color="#0BDB0B"/>
</plot>
<itemLabel/>
</pie3DPlot>
</pie3DChart>
<componentElement>
<reportElement key="table" style="table" x="0" y="211" width="551" height="60"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="tableDataset" >
<datasetParameter name="create_date_label">
<datasetParameterExpression><![CDATA[$P{create_date_label}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="action_plan_label">
<datasetParameterExpression><![CDATA[$P{action_plan_label}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="state_label">
<datasetParameterExpression><![CDATA[$P{state_label}]]></datasetParameterExpression>
</datasetParameter>
<datasetParameter name="problem_label">
<datasetParameterExpression><![CDATA[$P{problem_label}]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{table})]]></dataSourceExpression>
</datasetRun>
<jr:column width="120" >
<jr:columnHeader style="table_CH" height="40" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="120" height="40"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{action_plan_label}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="120" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$F{first}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="120" >
<jr:columnHeader style="table_CH" height="40" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="120" height="40"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{state_label}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="120" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$F{second}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="120" >
<jr:columnHeader style="table_CH" height="40" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="120" height="40"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{create_date_label}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField pattern="yyyy/MM/dd">
<reportElement x="0" y="0" width="120" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[((Action)$F{third}).getDtActionCreated()]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="190" >
<jr:columnHeader style="table_CH" height="40" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="190" height="40"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="8"/>
</textElement>
<textFieldExpression><![CDATA[$P{problem_label}]]></textFieldExpression>
</textField>
</jr:columnHeader>
<jr:detailCell style="table_TD" height="20" rowSpan="1">
<textField>
<reportElement x="0" y="0" width="190" height="20"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial Black" size="7"/>
</textElement>
<textFieldExpression><![CDATA[((Action)$F{third}).getIdentifiedProblem()]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<pageFooter>
<band height="21" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="462" y="0" width="80" height="10" forecolor="#CCCCCC"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER} + "("]]></textFieldExpression>
</textField>
<textField evaluationTime="Report" isBlankWhenNull="true">
<reportElement x="542" y="0" width="13" height="10" forecolor="#CCCCCC"/>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER} + ")"]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="0" y="11" width="555" height="10" forecolor="#CCCCCC"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{footer_label}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="202" y="0" width="158" height="10" forecolor="#CCCCCC"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{now}]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="170" height="10" forecolor="#CCCCCC"/>
<textElement verticalAlignment="Middle">
<font fontName="SansSerif" size="7"/>
</textElement>
<textFieldExpression><![CDATA[$P{user}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
使用此代码,百分比显示为一个整数。我想要两位小数的百分比。这可能吗?
答案 0 :(得分:2)
如果你传递JRChartCustomizer
,这是可能的您喜欢的自定义程序格式标签示例
public class MyChartCustomizer implements JRChartCustomizer {
@Override
public void customize(JFreeChart jfchart, JRChart jrchart) {
PiePlot plot = (PiePlot) jfchart.getPlot();
StandardPieSectionLabelGenerator labelGen = new StandardPieSectionLabelGenerator(
"{1} ({2})",
new DecimalFormat("#,##0.00"),
new DecimalFormat("0.00%"));
plot.setLabelGenerator(labelGen);
}
}
在jrxml中设置图表上的customizerClass
属性
<chart customizerClass="my.package.MyChartCustomizer">
在IDE中,您可以在属性中找到它:“Customizer Class”
注意:执行报告时,类MyChartCustomizer
需要在类路径中
示例输出