如何创建两个轴上都有日期的折线图?
How to create a line chart with date's on both axis?
我需要用 jasper-reports 实现两个日期类型的轴。
时间序列图表允许x轴为日期,y轴为数值,有没有办法实现y轴和x轴都是日期类型的双日期型轴?
要将 y 轴设置为您需要的日期轴。
1.将 valueExpression
中的日期转换为 java.lang.Number
,因为 valueExpression
只允许此 class(或子 class)
而不是在 valueExpression
中传递 java.util.Date
通过调用 Date.getTime()
以毫秒为单位传递时间 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>
Note: If your not using a timeSeriesChart
, the same needs to be done with x-axis.
我需要用 jasper-reports 实现两个日期类型的轴。
时间序列图表允许x轴为日期,y轴为数值,有没有办法实现y轴和x轴都是日期类型的双日期型轴?
要将 y 轴设置为您需要的日期轴。
1.将 valueExpression
中的日期转换为 java.lang.Number
,因为 valueExpression
只允许此 class(或子 class)
而不是在 valueExpression
中传递 java.util.Date
通过调用 Date.getTime()
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>
Note: If your not using a
timeSeriesChart
, the same needs to be done with x-axis.