Xaml oxyplot HighLowSeries 项目绑定

Xaml oxyplot HighLowSeries Items Binding

我想通过 XAML 定义一个 HighLowSeries。由于 HighLowSeries-graphsoxyplot.wpf 命名空间中不可用,我创建了以下 class:

public class HighLowSeries : OxyPlot.Wpf.XYAxisSeries
{

    public HighLowSeries()
    {
        this.InternalSeries = new OxyPlot.Series.HighLowSeries();
    }

    public override OxyPlot.Series.Series CreateModel()
    {
        this.SynchronizeProperties(this.InternalSeries);
        return this.InternalSeries;
    }

    /// <summary>
    /// The synchronize properties.
    /// </summary>
    /// <param name="series">
    /// The series.
    /// </param>
    protected override void SynchronizeProperties(OxyPlot.Series.Series series)
    {
        base.SynchronizeProperties(series);
        var s = (OxyPlot.Series.HighLowSeries)series;

        foreach (HighLowItem item in s.Items)
        {
            Trace.WriteLine("HighLowSeries " + item.X);
        }

    }

}

在我的 Window 中,我按以下方式使用 HighLowSeries

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Oxy="http://oxyplot.org/wpf"
    xmlns:Tmp="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Oxy:PlotView Width="300"
                    Height="300" 
                    Title="Test">
        <Oxy:PlotView.Series>
            <Tmp:HighLowSeries ItemsSource="{Binding list}" />
        </Oxy:PlotView.Series>
    </Oxy:PlotView>
</Grid>
</Window>

在Window后面的代码中,我有以下代码:

        private IList<HighLowItem> _list;

    public IList<HighLowItem> list
    {
        get
        {
            return this._list;
        }
        set
        {
            this._list = value;
        }
    }

    public MainWindow()
    {
        this.list = new List<HighLowItem>();

        list.Add(new HighLowItem(10, 8, 3, 2, 5));
        list.Add(new HighLowItem(12, 7, 4, 4, 2));
        list.Add(new HighLowItem(18, 4, 1, 2, 3));

        this.DataContext = this;

        InitializeComponent();
    }

当我启动应用程序时,在 (0,0) 处只有一条细小的水平线:

此外,HighLowSeries.SynchronizeProperties() 中有一个 Trace.WriteLine,我在其中打印出 Items-Collection 中 HighLowElements 的 X-coordinates。输出的数量与 MainWindow 的 list-property 中的元素匹配。但是 X-coordinate 始终为 0(以及 HighLowElements 的其他属性)。 当我以同样的方式实现我自己的 LineSeries 时,一切正常。

这是 oxyplot 中的错误吗?还是我错过了什么?目前无法通过代码创建 HighLowSeries

终于找到了地方,DataPoints的列表被清除了。

在源代码中(OxyPlot Source HighLowItemSeries)

有这个方法:

/// <summary>
/// Updates the data.
/// </summary>
protected internal override void UpdateData()
{
    if (this.ItemsSource == null)
    {
        return;
    }

    this.items.Clear();

    // Use the mapping to generate the points
    if (this.Mapping != null)
    {
        foreach (var item in this.ItemsSource)
        {
            this.items.Add(this.Mapping(item));
        }
        return;
    }

    var filler = new ListFiller<HighLowItem>();
    filler.Add(this.DataFieldX, (p, v) => p.X = Axis.ToDouble(v));
    filler.Add(this.DataFieldHigh, (p, v) => p.High = Axis.ToDouble(v));
    filler.Add(this.DataFieldLow, (p, v) => p.Low = Axis.ToDouble(v));
    filler.Add(this.DataFieldOpen, (p, v) => p.Open = Axis.ToDouble(v));
    filler.Add(this.DataFieldClose, (p, v) => p.Close = Axis.ToDouble(v));
    filler.FillT(this.items, this.ItemsSource);
}

由于 this.Mapping 等于 null,mapping 被绕过并添加了 filler object

Mapping-field的评论说了如何设置值:

    /// <summary>
    /// Gets or sets the mapping delegate.
    /// </summary>
    /// <value>The mapping.</value>
    /// <remarks>Example: series1.Mapping = item => new HighLowItem(((MyType)item).Time,((MyType)item).Value);</remarks>
    public Func<object, HighLowItem> Mapping { get; set; }

如果我将 SynchronizeProperties 方法更改为以下,则一切正常。

    protected override void SynchronizeProperties(OxyPlot.Series.Series series)
    {
        base.SynchronizeProperties(series);
        var s = (OxyPlot.Series.HighLowSeries)series;
        s.Mapping = item => new HighLowItem(((HighLowItem)item).X, ((HighLowItem)item).High, ((HighLowItem)item).Low, ((HighLowItem)item).Open, ((HighLowItem)item).Close);
    }