【发布时间】:2017-05-23 09:25:56
【问题描述】:
我需要使用 jasper-reports 实现两个日期类型的轴。
时间序列图允许在 x 轴上有一个日期,在 y 轴上有一个数值,有没有办法实现两个日期类型轴,其中 y 轴和 x 轴都是日期类型?
【问题讨论】:
标签: java jasper-reports jfreechart timeserieschart
我需要使用 jasper-reports 实现两个日期类型的轴。
时间序列图允许在 x 轴上有一个日期,在 y 轴上有一个数值,有没有办法实现两个日期类型轴,其中 y 轴和 x 轴都是日期类型?
【问题讨论】:
标签: java jasper-reports jfreechart timeserieschart
要将 y 轴设置为您需要的日期轴。
1.将valueExpression 中的日期转换为java.lang.Number,因为valueExpression 只允许此类(或子类)
通过调用Date.getTime(),而不是在valueExpression 中传递java.util.Date,以毫秒为单位传递时间java.lang.Long
<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),请参阅 Sample reference
*一些用意大利语格式化的随机日期
用于生成带有 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>
注意:如果您不使用
timeSeriesChart,则需要对 x 轴进行相同操作。
【讨论】: