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

0
0

I have a DateTimeAxis.

While I can see a way to template the format the text for all the axis tick labels, I need a way to completely override what is being rendered for each item. I want, for example, today’s date to be in bold, and to apply other text/buttons below the axis label conditionally, depending on logic specific to that date.

Is this possible?

Version
5.1.0
  • You must to post comments
0
0

Hi Christopher,

It’s not standard however there may be a way to do it.

If you look at the source code (you have source-code access) to LabelProviderBase you will see two methods for defining the DataContext for axis tick labels.

public abstract class LabelProviderBase : ProviderBase, ILabelProvider
{
    /// <summary>
    /// Creates a <see cref="ITickLabelViewModel"/> instance, based on the data-value passed in.
    /// Invokes <see cref="FormatLabel"/> to format the specified data-value passed in.
    /// </summary>
    /// <param name="dataValue">The data-value to format</param>
    public virtual ITickLabelViewModel CreateDataContext(IComparable dataValue)
    {
         // ... 
    }

    /// <summary>
    /// Updates existing <see cref="ITickLabelViewModel"/>, based on the data-value passed in.
    /// Invokes <see cref="FormatLabel"/> to format the specified data-value passed in.
    /// </summary>
    /// <param name="labelDataContext">The instance to update</param>
    /// <param name="dataValue">The data-value to format</param>
    public virtual ITickLabelViewModel UpdateDataContext(ITickLabelViewModel labelDataContext, IComparable dataValue)
    {
         // ... 
    } 
}

So, you can actually define the DataContext for the Tick Labels. By default on our NumericLabelProvider we return a NumericTickLabelViewModel.

You can, if you want, define your own ViewModel then selectively turn things on or off in the Axis Label template (like bold, or showing or hiding UI).

We haven’t tried it though, and don’t have any examples but if I were to go about this problem, this is where I’d start.

Let me know if it helps,

Best regards,
Andrew

  • You must to post comments
0
0

Hi,

Thanks, this worked for me.

I didn’t need to override the UpdateDataContext method, just CreateDataContext:

/// <summary>Class defining the tick label provider</summary>
public class TickLabelProvider : DateTimeLabelProvider
{
    /// <summary>
    /// Creates a <see cref="T:SciChart.Charting.Visuals.Axes.LabelProviders.ITickLabelViewModel" /> instance, based on the data-value passed in.
    /// Invokes <see cref="M:SciChart.Charting.Visuals.Axes.LabelProviders.LabelProviderBase.FormatLabel(System.IComparable)" /> to format the specified data-value passed in.
    /// </summary>
    /// <param name="dataValue">The data-value to format</param>
    /// <returns>The view model</returns>
    public override ITickLabelViewModel CreateDataContext(IComparable dataValue)
    {
        var datetimeValue = (DateTime)dataValue;
        var context = ((System.Windows.FrameworkElement)this.ParentAxis).DataContext as QaChartViewModel;

        return this.UpdateDataContext(new TickLabelFormatProvider(datetimeValue, context?.GetNotes(datetimeValue)), dataValue);
    }
} 

I set up the ticker format using this model.

<Style x:Key="DefaultTickLabelStyle"
               TargetType="{x:Type s:DefaultTickLabel}">
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate DataType="local:TickLabelFormatProvider">
                        ...
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Text}" 
                                       FontWeight="{Binding DateFormatter}"/>

                            <Rectangle Style="{DynamicResource ElementDropShadowStyle}" 
                                       Width="15"
                                       Height="15"
                                       Fill="Red"
                                       Tag="{Binding Text}"
                                       Visibility="{Binding IsNoteVisible}"
                                       ToolTip="{Binding NoteRollover}"
                                       >
                            </Rectangle>

                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style> 

many thanks!

  • Andrew
    Great! Thanks for sharing
  • You must to post comments
0
0

Update:

If the visible range is likely to change, then you will have to call UpdateDataContext, as the existing tickers will be reused with different ticker labels. I finished on this as my Ticker Label Provider:

/// <summary>Class defining the tick label provider</summary>
public class TickLabelProvider : DateTimeLabelProvider
{
    /// <summary>
    /// Creates a <see cref="T:SciChart.Charting.Visuals.Axes.LabelProviders.ITickLabelViewModel" /> instance, based on the data-value passed in.
    /// Invokes <see cref="M:SciChart.Charting.Visuals.Axes.LabelProviders.LabelProviderBase.FormatLabel(System.IComparable)" /> to format the specified data-value passed in.
    /// </summary>
    /// <param name="dataValue">The data-value to format</param>
    /// <returns>The view model</returns>
    public override ITickLabelViewModel CreateDataContext(IComparable dataValue)
    {
        return this.UpdateDataContext(new TickLabelFormatProvider(), dataValue);
    }

    /// <summary>
    /// Updates existing <see cref="T:SciChart.Charting.Visuals.Axes.LabelProviders.ITickLabelViewModel" />, based on the data-value passed in.
    /// Invokes <see cref="M:SciChart.Charting.Visuals.Axes.LabelProviders.LabelProviderBase.FormatLabel(System.IComparable)" /> to format the specified data-value passed in.
    /// </summary>
    /// <param name="labelDataContext">The instance to update</param>
    /// <param name="dataValue">The data-value to format</param>
    /// <returns>The view model</returns>
    public override ITickLabelViewModel UpdateDataContext(ITickLabelViewModel labelDataContext, IComparable dataValue)
    {
        var provider = labelDataContext as TickLabelFormatProvider;

        var datetimeValue = (DateTime)dataValue;
        var context = ((System.Windows.FrameworkElement)this.ParentAxis).DataContext as QaChartViewModel;

        provider?.SetFormat(datetimeValue, context?.GetNoteForGivenDate(datetimeValue));

        return base.UpdateDataContext(labelDataContext, dataValue);
    }
}

You will have to raise a property changed Notification when the Bound properties of your ticker ViewModel (for me, TickLabelFormatProvider) are updated.

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.