jFree绘制x轴刻度标签的不同时间格式

Nag*_*nke 3 jfreechart

我想为x轴设置不同的刻度标签格式
.

11月1日然后时间格式应该是小时..

在此输入图像描述

是否可以使用jFreeChart TimePeriodValuesTimePeriodValuesCollection数据集.

Gra*_*amA 5

你需要在中间阶段DateFormatOverride使用两个SimpleDateFormats,在午夜使用另一个,试试这个:

XYPlot plot = (XYPlot) chart.getPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();

axis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 6, new SimpleDateFormat("HH:mm")));


final SimpleDateFormat hourFmt = new SimpleDateFormat("HH:mm");
final SimpleDateFormat datFmt = new SimpleDateFormat("d.MMM");

axis.setDateFormatOverride(new DateFormat(){

    @SuppressWarnings("deprecation")
    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo,FieldPosition fieldPosition) {
        if ( date.getHours() == 0 ) {
          return datFmt.format(date, toAppendTo, fieldPosition);
        } else {
          return hourFmt.format(date, toAppendTo, fieldPosition);
        }
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        return hourFmt.parse(source,pos);
    }

});
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述