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

Answered
0
0

Hi, I found that the text formatting of discontinous DateTimeAxis is not working in WPF
I put the following format in DateTimeAxis and it displays correctly but not in discontinous DateTimeAxis
TextFormatting=”dd-MM”
SubDayTextFormatting=”HH:mm”

Version
6
  • You must to post comments
Best Answer
0
0

Hi Vincent

The DiscontinuousDateTimeAxis and the CategoryDateTimeAxis both use a slightly different way to format labels. They both use the Label Provider feature in SciChart WPF.

Internally in the DiscontinuousDateTimeAxis constructor we have this code:

    public DiscontinuousDateTimeAxis()
    {
        this.LabelProvider = new DiscontinuousDateTimeLabelProvider();
    }

Where DiscontinuousDateTimeLabelProvider is defined as this

/// <summary>
/// The DiscontinuousDateTimeLabelProvider is a pass-through which uses the <see cref="AxisCore.TextFormatting"/> and <see cref="AxisCore.CursorTextFormatting"/> properties
/// to format axis and cursor label texts. It also uses the <see cref="DateTimeAxis.SubDayTextFormatting"/> property to alter text-formatting when the date-range 
/// switches to intra-day
/// </summary>
public class DiscontinuousDateTimeLabelProvider : LabelProviderBase
{
    /// <summary>
    /// Formats a label for the cursor, from the specified data-value passed in
    /// </summary>
    /// <param name="dataValue">The data-value to format</param>
    /// <returns>
    /// The formatted cursor label string
    /// </returns>
    public override string FormatCursorLabel(IComparable dataValue)
    {
        var dateTime = dataValue.ToDateTime();

        var formattedText = ParentAxis.CursorTextFormatting.IsNullOrEmpty()
            ? FormatLabel(dataValue)
            : dateTime.ToString(ParentAxis.CursorTextFormatting);

        return formattedText;
    }

    /// <summary>
    /// Performs a type check of <see cref="ProviderBase.ParentAxis"/>. 
    /// Throws <see cref="InvalidOperationException"/> if the type is not derived from <see cref="DiscontinuousDateTimeAxis"/>.
    /// </summary>
    protected void CheckAxisType()
    {
        if (!(ParentAxis is DiscontinuousDateTimeAxis))
        {
            throw new InvalidOperationException(String.Format("The {0} is only valid on instances of {1}", GetType().Name, typeof(DiscontinuousDateTimeAxis).Name));
        }
    }

    /// <summary>
    /// Formats a label for the axis from the specified data-value passed in
    /// </summary>
    /// <param name="dataValue">The data-value to format</param>
    /// <returns>
    /// The formatted label string
    /// </returns>
    /// <exception cref="System.InvalidOperationException">The DiscontinuousDateTimeLabelProvider is only valid on instances of DiscontinuousDateTimeAxis</exception>
    public override string FormatLabel(IComparable dataValue)
    {
        CheckAxisType();

        var dtAxis = ParentAxis as DiscontinuousDateTimeAxis;

        var dt = dataValue.ToDateTime();
        var calendar = dtAxis.Calendar;
        var tickRange = dtAxis.VisibleRange;

        return DiscontinuousTickHelper.GetLabelFormat(calendar, tickRange, dt);
    }
}

So if you want to make changes to the label formatting this is the place to do it. Create a class which inherits LabelProviderBase and apply to the axis.

More info how to do this in our docs at Label Provider API

Best regards,
Andrew

  • You must to post comments
0
0

Very useful information, I found answers to my questions. And as a programmer, I want to share my latest project https://bestessaytyper.com/, which I am currently working on in the field of online educational platforms. I worked on it for a very long time and I think it will be useful

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.