Pre loader

How can I format major label and minor label differently?

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

I want to format my labels after tapping a radio button. But the major labels with the “dd mm” pattern and the minor labels with the “HH:mm” pattern. If I check the radio button in the radio button group the labels will format with the others formats. It works with TradeChartAxisLabelProvider but I can’t handle it with my own formats. What I have to use?

Version
4.3.0.4646
  • You must to post comments
1
0

Hi Iliya,

If I understand correctly you want to set custom format strings for TradeChartAxisLabelProvider used by CategoryDateAxis. Under the hood it uses quite complex algorithm to provide nice looking labels and tries to avoid duplication of information on axis. To customize formatting you would need to reimplement ICalendarUnitDateFormatter interface which provides API for formatting Dates, but also it allows to pass calendar units (bitmask which specifies what units should be used to format date). Here is an example of how this interface can be implemented ( it’s simplified version of default SciChart implementation ):

    class CustomCalendarFormatter implements ICalendarUnitDateFormatter {
    private final Locale locale = Locale.getDefault();
    private final TimeZone timeZone = TimeZone.getDefault();

    private boolean hasDefaultFormatter;
    private final SimpleDateFormat defaultDateFormatter = new SimpleDateFormat("", locale);

    private final SimpleDateFormat contextualDateFormatter = new SimpleDateFormat("", locale);

    @Override
    public Locale getLocale() {
        return locale;
    }

    @Override
    public TimeZone getTimeZone() {
        return timeZone;
    }

    @Override
    public void tryApplyDefaultFormat(String defaultFormat) {
        // force format that comes from axis instead of one based on calendar unit
        hasDefaultFormatter = !StringUtil.isNullOrEmpty(defaultFormat);
        if (hasDefaultFormatter) {
            defaultDateFormatter.applyPattern(defaultFormat);
        }
    }

    @Override
    public CharSequence format(Date date, int calendarUnit) {
        if(hasDefaultFormatter)
            return defaultDateFormatter.format(date);
        else {
            String format = "";

            if ((calendarUnit & CalendarUnit.YEAR) != 0) {
                format = format.concat(" yyyy");
            }
            if ((calendarUnit & CalendarUnit.MONTH) != 0) {
                format = format.concat(" MMM");
            }
            if ((calendarUnit & CalendarUnit.DAY) != 0) {
                format = format.concat(" dd");
            }
            if ((calendarUnit & CalendarUnit.HOUR) != 0) {
                format = format.concat(" HH:mm");
            }
            if ((calendarUnit & CalendarUnit.SECOND) != 0) {
                format = format.concat(":ss");
            }

            contextualDateFormatter.applyPattern(format);

            return contextualDateFormatter.format(date);
        }
    }
}

Then you can use it to create LabelProvider for CategoryDateAxis:

 xAxis.setLabelProvider(new TradeChartAxisLabelProvider(new TradeChartAxisLabelFormatter<>(

           new CustomCalendarFormatter(), new CursorCalendarUnitDateFormatter() 
    )));

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