Format String to Number iReport

0

Hey,

i want to generate a xyline chart in ireport with existing database.

I use for x-axis a field in string but i need to convert to number.

When i use

[CODE]

<field name="DESCRIPTION" class="java.lang.String" />

<field name="HALFHOUR" class="java.lang.String" />

<field name="VALUEA" class="java.lang.Double" />

........

    <xyLineChart>                 <chart>                     <reportElement x="126" y="32" width="311" height="253"/>                     <chartTitle/>                     <chartSubtitle/>                     <chartLegend/>                 </chart>                 <xyDataset>                     <xySeries>                         <seriesExpression><![CDATA[$F{DESCRIPTION}]]></seriesExpression>                         <xValueExpression><![CDATA[new java.text.SimpleDateFormat("hh24:mi").format(new java.text.SimpleDateFormat("hh24:mi").parse($F{HALFHOUR}))]]></xValueExpression>                         <yValueExpression><![CDATA[$F{VALUEA}]]></yValueExpression>                     </xySeries>                 </xyDataset>                 <linePlot>                     <plot/>                     <categoryAxisFormat>                         <axisFormat>                             <labelFont/>                             <tickLabelFont/>                         </axisFormat>                     </categoryAxisFormat>                     <valueAxisFormat>                         <axisFormat>                             <labelFont/>                             <tickLabelFont/>                         </axisFormat>                     </valueAxisFormat>                 </linePlot>             </xyLineChart>

[/CODE]

When i try to compile i get the message: Cannot cast from string to number.

 

Can somebody help ply and can i use this template in Eclipse/JasperReports?

 

Thanks and Regards

mkeym's picture
12
Joined: Jun 14 2010 - 7:54am
Last seen: 4 years 3 months ago

3 Answers:

0

mkeym:

Not sure about using your XML template in Eclipse/JasperReports but if it's attempting to compile...maybe it works.

As far as the cast error: Try to type cast your Strings to Integers by using intValue. (see the modified code sample)

Hope this helps.

 

Regards

 

 

Code:
<xyDataset>
   <xySeries>
      <seriesExpression><![CDATA[$F{DESCRIPTION}]]></seriesExpression>
      <xValueExpression><![CDATA[new java.text.SimpleDateFormat("hh24:mi").format(new java.text.SimpleDateFormat("hh24:mi").parse($F{HALFHOUR}.intValue()))]]></xValueExpression>
      <yValueExpression><![CDATA[$F{VALUEA}.intValue()]]></yValueExpression>
   </xySeries>
</xyDataset>


Post Edited by rwigfall at 06/14/2010 20:52
rwigfall's picture
Joined: Nov 12 2009 - 7:49am
Last seen: 4 years 10 months ago
0

Now, when i try to use the method intValue() i get a new error: The method intValue() is undefined for the type String

 

I actually also try to create LineChart but it can't show time parameter in x-axis. The x-axis is without a descirption of Halfhour String. Here the chart seems to be ok but i want to create it with XYLineChart or maybe i use TimeSeries?

If i use this template in eclipse i get over 60 errors. But first of all i need to create it in ireport.

mkeym's picture
12
Joined: Jun 14 2010 - 7:54am
Last seen: 4 years 3 months ago
0
Oops...can't use intValue to convert a date type. Also you don't need to convert ValueA to integer so omit that as well. I think you want to convert the field $F{HALFHOUR} from string to integer then use $F{HALFHOUR}.intValue() Are you sure your java is correct? I didn't see "i" as an acceptable pattern letter in the javadoc - string java.text.SimpleDateFormat("hh24:mi"), remove the "i" so you have java.text.SimpleDateFormat("hh24:m") or java.text.SimpleDateFormat("H:m") works I suggest making sure the java code is correct first, this way you isolate your errors and troubleshooting. (I tried java.text.SimpleDateFormat("hh24:mi") and got errors) If I am helpful please give Karma points.
rwigfall's picture
Joined: Nov 12 2009 - 7:49am
Last seen: 4 years 10 months ago
Feedback
randomness