我需要使用 jasper-reports 实现两个日期类型的轴。
时间序列图表允许 x 轴上的日期和 y 轴上的数值,有没有办法实现两个日期类型轴,其中 y 轴和 x 轴都是日期类型?
要将 y 轴设置为日期轴,您需要这样做。
1. 将 Date 中的 Date 转换valueExpression
为 ajava.lang.Number
,因为valueExpression
只允许这个类(或子类)
相反,传递的java.util.Date
在valueExpression
打发时间以毫秒为单位java.lang.Long
,通过调用Date.getTime()
<valueExpression><![CDATA[$F{myDateOnYAxis}.getTime()]]></valueExpression>
2.添加一个JRChartCustomizer
将数字轴更改为带有相对格式器的日期轴
public class MyChartCustomizer implements JRChartCustomizer {
@Override
public void customize(JFreeChart jfchart, JRChart jrchart) {
XYPlot plot = (XYPlot) jfchart.getPlot(); //get the plot
//Create the new date axis for y
DateAxis yDateAxis = new DateAxis();
//Set desired time format
DateFormat dateFormat = new SimpleDateFormat("MMM - yyyy");
yDateAxis.setDateFormatOverride(dateFormat);
//Add your own Tickunit if you like (you can do with out also, comment out the below line and let JFreeChart decided)
yDateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MONTH,3));
//Set the new y-axis to the plot
plot.setRangeAxis(yDateAxis);
}
}
有关如何将 JRChartCustomizer 添加到您的设计 (jrxml) 的信息,请参阅示例参考
*一些用意大利语格式化的随机日期
用于使用 csv 数据源生成图形的示例 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="Test2DateGraph" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2347c131-1884-430a-b77f-59f08f896c8a">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="Dates"/>
<queryString language="csv">
<![CDATA[]]>
</queryString>
<field name="Date1" class="java.util.Date"/>
<field name="Date2" class="java.util.Date"/>
<summary>
<band height="353">
<timeSeriesChart>
<chart evaluationTime="Report" customizerClass="MyChartCustomizer">
<reportElement x="10" y="50" width="530" height="256" uuid="4a93e72e-251b-4026-bb11-edc26ecd6599"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<timeSeriesDataset>
<timeSeries>
<seriesExpression><![CDATA["SERIES 1"]]></seriesExpression>
<timePeriodExpression><![CDATA[$F{Date2}]]></timePeriodExpression>
<valueExpression><![CDATA[$F{Date2}.getTime()]]></valueExpression>
</timeSeries>
</timeSeriesDataset>
<timeSeriesPlot>
<plot/>
<timeAxisFormat>
<axisFormat/>
</timeAxisFormat>
<valueAxisFormat>
<axisFormat/>
</valueAxisFormat>
</timeSeriesPlot>
</timeSeriesChart>
</band>
</summary>
</jasperReport>
注意:如果您不使用 a
timeSeriesChart
,则需要对 x 轴进行相同的操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句