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

Answered
0
0

I am trying to add a VerticalLineAnnotation with a text at the bottom to my SciChart graph.

The similar thing works great when the XAxis is a NumericalAxis, but throws an Exception when it is a TimeSpanAxis.

var commentAnnotation = new VerticalLineAnnotationViewModel()
{
    // Setting the LabelValue only works when the XAxis is a NumericalAxis
    // and throws an Exception when it is a TimeSpanAxis
    LabelValue = "vertical line annotation",
    ShowLabel = true,
    // adjust this to '9' when trying it with a NumericalAxis
    X1 = TimeSpan.FromHours(9),
};
AnnotationViewModels.Add(commentAnnotation);

My SciChartSurface looks like this:

<s:SciChartSurface Annotations="{s:AnnotationsBinding AnnotationViewModels}">
    <s:SciChartSurface.XAxis>
        <s:TimeSpanAxis>
            <s:TimeSpanAxis.VisibleRange>
                <s:TimeSpanRange Min="00:00:00" Max="10:00:00" />
            </s:TimeSpanAxis.VisibleRange>
        </s:TimeSpanAxis>
    </s:SciChartSurface.XAxis>
</s:SciChartSurface>
Version
5.2.1.11757
  • You must to post comments
Best Answer
1
0

Hi Matthias,

That’s really crazy that this crashes, I can reproduce on my side. I also discovered something wierd.

TimeSpanAxis calls TimeSpanLabelProvider.FormatCursorLabel to format the text ‘blub’, which it realises is a string and just returns the string.

For some reason in scichart this crashes, but I took the very same code from our codebase and put it on your app and it doesn’t crash?

Maybe something got changed in the obfuscation/build process.

Try this as a workaround.

/// <summary>
/// The TimeSpanLabelProvider is a pass-through which uses the <see cref="AxisCore.TextFormatting"/> and <see cref="AxisCore.CursorTextFormatting"/> properties
/// to format axis and cursor label texts. 
/// </summary>
public class TimeSpanLabelProviderEx : TimeSpanLabelProvider
{
    /// <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)
    {
        if (!(ParentAxis is TimeSpanAxis timeSpanAxis))
        {
            throw new InvalidOperationException("The TimeSpanLabelFormatter is only valid on instances of TimeSpanAxis");
        }

        return FormatString(dataValue, timeSpanAxis.CursorTextFormatting);
    }

    /// <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 DateTimeLabelFormatter is only valid on instances of DateTimeAxis</exception>
    public override string FormatLabel(IComparable dataValue)
    {
        if (!(ParentAxis is TimeSpanAxis timeSpanAxis))
        {
            throw new InvalidOperationException("The TimeSpanLabelFormatter is only valid on instances of TimeSpanAxis");
        }

        return FormatString(dataValue, timeSpanAxis.TextFormatting);
    }

    private string FormatString(IComparable dataValue, string textFormatting)
    {
        bool hasNegativeFormatter = textFormatting.Contains("-");
        string result;

        if (dataValue is TimeSpan span)
        {
            bool negative = span < TimeSpan.Zero && hasNegativeFormatter;
            string format = hasNegativeFormatter ? textFormatting.TrimStart('-') : textFormatting;

            var formatted = span.ToTimeSpan().ToString(format);
            result = negative ? "-" + formatted : formatted;
        }
        else
        {
            result = dataValue.ToString();
        }

        return result;
    }
}

and usage

<Window x:Class="WpfApp7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp7"
        xmlns:s="http://schemas.abtsoftware.co.uk/scichart"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:MainWindowViewModel x:Key="ViewModel" />
        <local:TimeSpanLabelProviderEx x:Key="TimeSpanLabelProviderEx"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource ViewModel}">
        <s:SciChartSurface  Annotations="{s:AnnotationsBinding Annotations}">
            <s:SciChartSurface.XAxis>
                <s:TimeSpanAxis LabelProvider="{StaticResource TimeSpanLabelProviderEx}">
                    <s:TimeSpanAxis.VisibleRange>
                        <s:TimeSpanRange Min="00:00:00" Max="10:00:00" />
                    </s:TimeSpanAxis.VisibleRange>
                </s:TimeSpanAxis>
            </s:SciChartSurface.XAxis>
            <s:SciChartSurface.YAxis>
                <s:NumericAxis />
            </s:SciChartSurface.YAxis>
        </s:SciChartSurface>
    </Grid>
</Window>

Best regards,
Andrew

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.