Jasper reports 如何计算iReport中变量的中值?

Jasper reports 如何计算iReport中变量的中值?,jasper-reports,median,Jasper Reports,Median,我正在iReport中准备一份报告,我需要计算交叉表中使用的变量的字段中值。我注意到中位数没有内置的计算类型(只有最高和最低) 有没有办法在iReport中获得字段的中位数或第50个百分位?JasperReports中没有内置的中位数计算 通过手动收集列表中的值,然后使用进行计算,仍然可以获得中值 请参见下面的示例。报表还使用将包装器数组转换为基本数组。要运行报告,需要将commons-math3-x.y.z.jar和commons-lang3-x.y.jar添加到类路径中 <?xml

我正在iReport中准备一份报告,我需要计算交叉表中使用的变量的字段中值。我注意到中位数没有内置的计算类型(只有最高和最低)


有没有办法在iReport中获得字段的中位数或第50个百分位?

JasperReports中没有内置的中位数计算

通过手动收集列表中的值,然后使用进行计算,仍然可以获得中值

请参见下面的示例。报表还使用将包装器数组转换为基本数组。要运行报告,需要将commons-math3-x.y.z.jar和commons-lang3-x.y.jar添加到类路径中

<?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="FirstJasper" columnCount="1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30">
    <style name="Sans_Normal" isDefault="true" fontName="DejaVu Sans" fontSize="8"/>
    <queryString>SELECT * FROM Orders</queryString>
    <field name="Freight" class="java.lang.Double"/>
    <variable name="FreightList" class="java.util.List">
        <variableExpression>$V{FreightList}</variableExpression>
        <initialValueExpression>new java.util.ArrayList()</initialValueExpression>
    </variable>
    <variable name="AddFreight" class="java.lang.Boolean">
        <variableExpression>$V{FreightList}.add($F{Freight})</variableExpression>
    </variable>
    <title>
        <band height="50">
            <textField evaluationTime="Report">
                <reportElement x="5" y="5" width="350" height="40"/>
                <textFieldExpression><![CDATA["median is " + org.apache.commons.math3.stat.StatUtils.percentile(org.apache.commons.lang3.ArrayUtils.toPrimitive((Double[]) $V{FreightList}.toArray(new Double[$V{FreightList}.size()])), 50)]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="13">
            <textField pattern="0.00">
                <reportElement x="5" y="0" width="350" height="11"/>
                <textFieldExpression><![CDATA[$F{Freight}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

从订单中选择*
$V{FreightList}
新的java.util.ArrayList()
$V{FreightList}。添加($F{FreightList})

我认为平均值是所有值/值个数的总和。我不需要平均值,我需要字段的第50个百分位(第一个和最后一个数字的中间点)。JasperReports服务器有计算中间值的表达式-您可以尝试查看源代码-发布在Jaspersoft communityok上,我不希望过多地使用sql,但现在看来这可能是我唯一的选择。我不明白为什么JasperSoft不会把它作为一个内置计算添加进去。不管怎样,谢谢你为我指出一个可能的解决办法@欢迎AlexKYou:)你有JR服务器吗?我需要添加对类的引用吗?我试图将其添加到一个文本字段中,但我得到org.apache.commons.math3.stat.StatsUtils无法解析为类型org.apache.commons.lang3.ArrayUtils无法解析为类型Double@dadada67yes类型的toArray(Double)方法未定义,您的类路径中需要commons-math3和commons-lang3 jars。我编辑了答案,使要求更加明确。好问题,好答案