问题描述:

I have a data series List<Tuple<string,double>> from which I create a XyDataSeries<double, double>. I use the following LabelFormatter.

public class SciChartCustomLabelFormatter : ILabelFormatter

{

private readonly string[] _xLabels;

public SciChartCustomLabelFormatter(string[] xLabels)

{

_xLabels = xLabels;

}

public void Init(IAxis parentAxis)

{

}

public void OnBeginAxisDraw()

{

}

public ITickLabelViewModel CreateDataContext(IComparable dataValue)

{

throw new NotImplementedException();

}

public ITickLabelViewModel UpdateDataContext(ITickLabelViewModel labelDataContext, IComparable dataValue)

{

throw new NotImplementedException();

}

public string FormatLabel(IComparable dataValue)

{

var index = (double)Convert.ChangeType(dataValue, typeof(double));

if (index >= 0 && index < _xLabels.Length)

return _xLabels[(int)index];

return index.ToString(CultureInfo.InvariantCulture);

}

public string FormatCursorLabel(IComparable dataValue)

{

var index = (double)Convert.ChangeType(dataValue, typeof(double));

if (index >= 0 && index < _xLabels.Length)

return _xLabels[(int)index];

return index.ToString(CultureInfo.InvariantCulture);

}

}

I want to create a FastColumnRenderableSeries

<sciChart:FastColumnRenderableSeries

x:Name="columnSeries" DataPointWidth="1"

SeriesColor="#A99A8A" Opacity="0.5"

XAxisId="BottomAxisId"

YAxisId="LeftAxisId"

DataSeries="{Binding Series}">

</sciChart:FastColumnRenderableSeries>

with the strings being used as labels for the column series.

At present, I can display the series with YAxis clearly displaying the values. But how do I use the label formatter to display the XAxis strings? I don't know what should I do with the methods:

public ITickLabelViewModel CreateDataContext(IComparable dataValue)

and

public ITickLabelViewModel UpdateDataContext(ITickLabelViewModel labelDataContext, IComparable dataValue)

I'm trying to create a Pareto chart here.

网友答案:

The API has changed in SciChart v3.0 but we have a recently updated article here on 'Overview of the Label Provider API'

I would suggest inheriting LabelProviderBase as this implements many of the interface methods that you don't need to worry about.

You can specify text labels with LabelProvider as follows:

public class CustomLabelProvider : LabelProviderBase
{
    /// <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)
    {
        return dataValue.ToString(); // TODO: Implement as you wish
    }
    /// <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)
    {
        return dataValue.ToString();// TODO: Implement as you wish
    }
}

Usage in XAML

<!-- Assumes you have declared the CustomLabelProvider as a static resource -->
<s:NumericAxis LabelProvider="{StaticResource local:CustomLabelProvider}"/>

Finally, if you need more information, there is a complete walkthrough on Screenshots, Printing and String Axis Labels here with downloadable sample (updated to use v3.0 of the API).

Disclosure: I am the MD and owner of SciChart WPF Charts

相关阅读:
Top