当前位置: 首页 > 知识库问答 >
问题:

如何在PieChart中显示2位小数的百分比?

寇桐
2023-03-14

我使用Jasper Report 5.2,我想生成PieChart并显示带有2个小数的百分比。

我用一个piechart创建了jrxml,数据源中填充了参数piechart类型列表。

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>

使用此代码,百分比以一个整数显示。我要带两个小数的百分比。这可能吗?

共有1个答案

郎鸿雪
2023-03-14

如果您通过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中,在图表上设置CustizerClass属性

<chart customizerClass="my.package.MyChartCustomizer">

在IDE中,您可以在属性中找到它:“Customizer类”

注意:执行报表时,类MyChartCustomizer需要位于类路径中

示例输出

 类似资料:
  • 问题内容: 我有一个ng模型的浮点值,我希望始终在其小数点后2位显示: 当“ myNumb”为小数时,此方法适用于大多数情况。但是,如果“ myNumb”的小数位数少于2个(3.2)或整数(30), 则不会强制显示小数点后2位。如何在字段中强制显示小数点后2位 问题答案: AngularJS-输入数字两位小数可以帮助…过滤: 设置正则表达式以使用ng-pattern验证输入。在这里,我只接受最多2

  • 问题内容: 我想显示: 如 和: 如 不管小数点的长度或是否有任何小数位,我都希望显示带有2个小数位的a,并且我想以一种有效的方式来做到这一点。目的是显示货币值。 例如, 问题答案: 我想您可能正在使用模块中的对象?(如果您需要精确到小数点后两位精确到两位数且任意大的数字,那么绝对应该如此,这就是您的问题标题所暗示的……) 如果是这样,文档的“ 十进制常见问题解答” 部分将包含一个问题/答案对,这

  • 问题内容: 我要显示: 和: 不管小数点的长度或是否有小数位,我都希望显示一个带有2个小数位的,并且我想以一种有效的方式做到这一点。目的是显示货币值。 例如, 问题答案: 我想你可能正在使用模块中的对象?(如果你需要精确到小数点后两位任意大数的精度,则绝对应该如此,这就是你的问题标题所建议的……) 如果是这样,则文档的“ 十进制常见问题解答”部分将包含一个问题/答案对,这可能对你有用: 问:在具有

  • 问题内容: 我试图找到2到100个小数位的平方根,但是默认情况下它仅显示为10,如何更改此值? 问题答案: 十进制模块派上用场。

  • 有没有办法将值的格式设置为始终显示两位小数? 示例:我想查看,而不是。

  • 我正在制作一个表单,用户可以使用html数字输入标记输入美元金额。有没有办法让输入框始终显示2位小数?