【问题标题】:How to create a line chart with date's on both axis?如何在两个轴上创建带有日期的折线图?
【发布时间】:2017-05-23 09:25:56
【问题描述】:

我需要使用 jasper-reports 实现两个日期类型的轴。

时间序列图允许在 x 轴上有一个日期,在 y 轴上有一个数值,有没有办法实现两个日期类型轴,其中 y 轴和 x 轴都是日期类型?

【问题讨论】:

    标签: java jasper-reports jfreechart timeserieschart


    【解决方案1】:

    要将 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 轴进行相同操作。

    【讨论】:

    • 感谢您回答我的问题,由于某种奇怪的原因,y 轴显示为空白,没有值,只是空白。
    • @Martin 没有刻度标签?,没有图表?什么没有显示?,也许你的 y 轴的前景色只是白色?
    • @Martin 我添加了我用来测试这个的 jrxml 并生成了上图,参见“显示代码 sn-p”
    • 请看图表link的截图,由于某种原因它不会显示x轴值
    • 出色的移除 TickUnit 完成了这项工作。这是使用折线图的屏幕截图,也可以使用时间序列 ibb.co/nytrYF 。非常感谢
    猜你喜欢
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2013-01-07
    • 2020-09-03
    • 2020-08-01
    • 2012-12-07
    相关资源
    最近更新 更多