开发者

How to pass date as a field in jrxml of JasperReports?

开发者 https://www.devze.com 2022-12-27 09:29 出处:网络
I want to pass date as a field to the jrxml. Foll开发者_运维百科owing is the code for it. <xyLineChart>

I want to pass date as a field to the jrxml. Foll开发者_运维百科owing is the code for it.

<xyLineChart> 
    <chart evaluationTime="Band"> 
        <reportElement x="0" y="0" width="555" height="500"/> 
    </chart> 
    <xyDataset> 
        <dataset incrementType="None"/> 
        <xySeries> 
            <seriesExpression><![CDATA["CpuUsageGraph"]]></seriesExpression> 
            <xValueExpression><![CDATA[new java.util.Date($F{time}.getTime())]]></xValueExpression> 
            <yValueExpression><![CDATA[$F{cpuUsage}]]></yValueExpression>
        </xySeries> 
    </xyDataset> 
    <linePlot> 
        <plot/> 
    </linePlot> 
</xyLineChart>

But it is not working. Its giving error as can not cast from date to number. Then how to convert it?


You are using the time field in the constructor of Date. No need for that.

Instead of this:

java.util.Date($F{time}.getTime())

use this:

$F{time}

If you want the long value of it: then use this

$F{time}.getTime()

UPDATE
I didn't notice you are using a chart, here is a new answer:

In charts, X and Y value expressions should be any Number object, check subclasses in Number Class JavaDoc, in your case you are getting the long value of your Time field, which cannot be cast to Number, you will need to define a new object, for example:

new Long($F{time}.getTime())


Side Note: in this case the report will compile and work, BUT, you are getting the number of milliseconds and using it in your chart. I don't think that is what you want exactly. So i would suggest extracting a specific field from your Date field like Day. Month, Year ... etc


I cannot see the attached JRXML. However, open your JRXML file in a text editor, and check that the field is defined something like this:

<field name="MyDate" class="java.util.Date"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号