Reputation: 1288
I want to have different tick label formats for x axis
Something like this.
1st Nov then Time format should be Hour..
Is it possible by using jFreeChart TimePeriodValues
and TimePeriodValuesCollection
dataset.
Upvotes: 3
Views: 1356
Reputation: 5923
You will need to use a DateFormatOverride
with two SimpleDateFormat
s 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);
}
});
Upvotes: 5