SciChart® the market leader in Fast WPF Charts, WPF 3D Charts, and iOS Chart & Android Chart Components

Welcome to the SciChart Community Forums!

Please use the forums below to ask questions about SciChart. Take a moment to read our Question asking guidelines on how to ask a good question and our support policy. We also have a tag=SciChart on Stackoverflow.com where you can earn rep for your questions!

Please note: SciChart team will only answer questions from customers with active support subscriptions. Expired support questions will be ignored. If your support status shows incorrectly, contact us and we will be glad to help.

0
0

Hi, I want to color the axis label by its value,
eg. value< 0 -> show with red color
value = 0 -> show grey color
value > 0 -> show green color

similar to this question, but in android platform, it seems the LabelProvider has function related to the value(string) formatting only. Is there any ways to styling the label? Thanks!

Version
2.0.0
  • You must to post comments
0
0

Hi May Lym,

Yes, it’s possible by returning SpannableString with color from LabelProvider.

You’ll need to implement custom LabelFormatter which uses default implementation for formatting and then wraps it into SpannableString with desired color:

  class CustomLabelFormatter implements ILabelFormatter<NumericAxis> {
    private final NumericLabelFormatter defaultFormatter = new NumericLabelFormatter();

    @Override
    public void update(NumericAxis axis) {
        defaultFormatter.update(axis);
    }

    @Override
    public CharSequence formatLabel(Comparable dataValue) {
        return formatLabelInternal(dataValue, defaultFormatter.formatLabel(dataValue));
    }

    private static CharSequence formatLabelInternal(Comparable dataValue, CharSequence formattedValue) {
        final double doubleValue = ComparableUtil.toDouble(dataValue);

        if(doubleValue < 0d) {
            return formatLabelWithColor(formattedValue, Color.RED);
        } else if(doubleValue > 0d) {
            return formatLabelWithColor(formattedValue, Color.GREEN);
        } else
            return formatLabelWithColor(formattedValue, Color.GRAY);
    }

    private static CharSequence formatLabelWithColor(CharSequence label, int color) {
        final SpannableString coloredString = new SpannableString(label);

        coloredString.setSpan(new ForegroundColorSpan(color), 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return coloredString;
    }


    @Override
    public CharSequence formatCursorLabel(Comparable dataValue) {
        return formatLabelInternal(dataValue, defaultFormatter.formatCursorLabel(dataValue));
    }
  }

Then use this custom LabelFormatter for your axis:

 xAxis.setLabelProvider(new NumericLabelProvider(new CustomLabelFormatter()));

Hope this will help you!

Best regards,
Yura

Images
  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.