Hello,
I want to use SubDayTextFormatting in android so that when the visible range reaches the same day, e.g. as I zoom, it switches from dd:MMM:yyyy to HH:mm.
How can I do that?
- Muhammad Bilal asked 4 years ago
- You must login to post comments
Hi Muhammad,
In v3.x we added SubDayTextFormatting property which allows to do this.
As for 2.x – we don’t support such behavior in v2.x and the only way to achieve this in v2.x is to create custom LabelProvider or LabelFormatter used by LabelProvider:
public class DateLabelFormatter implements ILabelFormatter<DateAxis> {
private static final double ONE_DAY = DateIntervalUtil.fromDays(1);
private final SimpleDateFormat labelFormat;
private final SimpleDateFormat cursorLabelFormat;
public DateLabelFormatter() {
this(new SimpleDateFormat("dd:MMM:yyyy", Locale.getDefault()), new SimpleDateFormat("dd:MMM:yyyy", Locale.getDefault()));
}
public DateLabelFormatter(@NonNull SimpleDateFormat labelFormat, @NonNull SimpleDateFormat cursorLabelFormat) {
this.labelFormat = labelFormat;
this.cursorLabelFormat = cursorLabelFormat;
}
@Override
public void update(DateAxis axis) {
final String cursorTextFormatting = axis.getCursorTextFormatting();
final IRange<Date> visibleRange = axis.getVisibleRange();
final double min = visibleRange.getMinAsDouble();
final double max = visibleRange.getMaxAsDouble();
// select label format strings based on VisibleRange
final String labelFormatString;
if(max - min > ONE_DAY) {
labelFormatString = "dd:MMM:yyyy";
} else {
labelFormatString = "HH:mm";
}
final String cursorLabelFormatString = StringUtil.isNullOrEmpty(cursorTextFormatting) ? labelFormatString : cursorTextFormatting;
synchronized (labelFormat) {
labelFormat.applyPattern(labelFormatString);
}
synchronized (cursorLabelFormat) {
cursorLabelFormat.applyPattern(cursorLabelFormatString);
}
}
@Override
public CharSequence formatLabel(Comparable dataValue) {
synchronized (labelFormat) {
return labelFormat.format(ComparableUtil.toDate(dataValue));
}
}
@Override
public CharSequence formatCursorLabel(Comparable dataValue) {
synchronized (cursorLabelFormat) {
return cursorLabelFormat.format(ComparableUtil.toDate(dataValue));
}
}
}
Then assign to axis:
dateAxis.setLabelProvider(new DateLabelProvider(new DateLabelFormatter()));
Hope this will help you!
Best regards,
Yura
- Yura Khariton answered 4 years ago
- You must login to post comments
Please login first to submit.