Question

I want to have different tick label formats for x axis
Something like this.

1st Nov then Time format should be Hour..

enter image description here

Is it possible by using jFreeChart TimePeriodValues and TimePeriodValuesCollection dataset.

Était-ce utile?

La solution

You will need to use a DateFormatOverride with two SimpleDateFormats one for the intermediate periods and another for midnight, Try this:

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);
    }

});

enter image description here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top