Search code examples
scichart

Custom Label Provider: Can't override Init() method


I'm trying to access a scaling factor in a viewModel through my CustomNumericLabelProvider.

I'm not quite sure what the best approach is, but I figured I might be able to access it through the parent axis if I used the Init(IAxis parentAxis) method which was shown in the LabelProvider documentation. I've given that a try, but now I'm getting an error telling me that "There is no suitable method for override".

If I comment out the Init() method, CustomNumericLabelProvider works great (with a hard-coded scaling factor).

Any idea why I'm receiving this error message? Or what another good approach would be to gain access to the scaling factor in my viewModel?

Note: I've also tried passing the viewModel into a custom constructor for the label provider (I was able to do something like this with viewportManager), however that didn't seem to work.

Here's the code (With the custom constructor, although I get the same error message without it)

public class CustomNumericLabelProvider : SciChart.Charting.Visuals.Axes.LabelProviders.NumericLabelProvider
{
    // Optional: called when the label provider is attached to the axis
    public override void Init(IAxis parentAxis) {
        // here you can keep a reference to the axis. We assume there is a 1:1 relation
        // between Axis and LabelProviders
        base.Init(parentAxis);
    }

    /// <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>
    public override string FormatLabel(IComparable dataValue)
    {
        // Note: Implement as you wish, converting Data-Value to string
        var converted = (double)dataValue * .001 //TODO: Use scaling factor from viewModel
        return converted.ToString();

        // NOTES:
        // dataValue is always a double.
        // For a NumericAxis this is the double-representation of the data
    }
}

Solution

  • I would propose passing the scaling factor into the constructor of the CustomNumericLabelProvider, and instantiating it in your viewmodel.

    So your code becomes

        public class CustomNumericLabelProvider : LabelProviderBase
        {
            private readonly double _scaleFactor;
    
            public CustomNumericLabelProvider(double scaleFactor)
            {
                _scaleFactor = scaleFactor;
            }
    
            public override string FormatLabel(IComparable dataValue)
            {
                // TODO
            }
    
            public override string FormatCursorLabel(IComparable dataValue)
            {
                // TODO 
            }
        }
    
        public class MyViewModel : ViewModelBase
        {
            private CustomNumericLabelProvider _labelProvider = new CustomNumericLabelProvider(0.01);
    
            public CustomNumericLabelProvider LabelProvider { get { return _labelProvider; } }
        }
    

    And you then bind to it as follows

    <s:NumericAxis LabelProvider="{Binding LabelProvider}"/>
    

    Assuming the datacontext of NumericAxis is your viewmodel.

    Please be advised in SciChart v5 there will be new APIs for AxisBindings (similar to SeriesBinding) for dynamically creating axis in ViewModel. This will make dynamic axis in MVVM much easier. You can take SciChart v5 for a test-drive by accessing our WPF Chart Examples here.