SciChart Column Series LabelFormatting

时间:2014-03-25 08:28:57

标签: c# wpf charts scichart

我有一个数据系列List<Tuple<string,double>>,我从中创建了XyDataSeries<double, double>。我使用以下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);
    }
}

我想创建一个FastColumnRenderableSeries

<sciChart:FastColumnRenderableSeries
        x:Name="columnSeries" DataPointWidth="1"
        SeriesColor="#A99A8A" Opacity="0.5"
        XAxisId="BottomAxisId"
        YAxisId="LeftAxisId"
        DataSeries="{Binding Series}">
</sciChart:FastColumnRenderableSeries>

将字符串用作列系列的标签。

目前,我可以用YAxis显示系列,清楚地显示数值。 但是如何使用标签格式化程序显示XAxis字符串?我不知道该如何处理这些方法:

public ITickLabelViewModel CreateDataContext(IComparable dataValue) 

public ITickLabelViewModel UpdateDataContext(ITickLabelViewModel labelDataContext, IComparable dataValue) 

我正在尝试在这里创建帕累托图表。

1 个答案:

答案 0 :(得分:1)

API已在SciChart v3.0中发生了变化,但我们最近在这里发布了一篇文章&#39; Overview of the Label Provider API&#39;

我建议继承LabelProviderBase,因为这会实现许多您不必担心的界面方法。

您可以使用LabelProvider指定文字标签,如下所示:

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
    }
}

XAML中的用法

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

最后,如果您需要更多信息,请在Screenshots, Printing and String Axis Labels here上进行完整的演练,其中包含可下载的示例(已更新为使用API​​的v3.0)。

String Axis in SciChart WPF Charts

披露: 我是SciChart WPF Charts的主管和所有者