Pre loader

How to switch from dd:MMM:yyyy to HH:mm on zoom in android?

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

0
0

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?

Version
2.5.0.2608
  • You must to post comments
0
0

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

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.

Try SciChart Today

Start a trial and discover why we are the choice
of demanding developers worldwide

Start TrialCase Studies