9

是否可以将绘图绑定到 LineSeries 的集合而不是 OxyPlot 中的单个 LineSeries?(而不是通过模型)。

我正在寻找这样的东西:

<oxy:Plot>        
    <oxy:Plot.Series>     
        <oxy:LineSeries ItemsSource="{Binding myCollectionOfLineSeries}" />              
    </oxy:Plot.Series>
</oxy:Plot>

myCollectionOfLineSeries 在哪里:

private ObservableCollection<LineSeries> _myCollectionOfLineSeries ;
        public ObservableCollection<LineSeries> myCollectionOfLineSeries 
        {
            get
            {
                return _myCollectionOfLineSeries ;
            }
            set
            {
                _myCollectionOfLineSeries = value;
                OnPropertyChanged("myCollectionOfLineSeries ");

            }
        }

我希望得到答案:a)“不,这是不可能的”或 b)“是的,只需将 XYZ 放在 IJK 之前”。

谢谢阅读。

4

2 回答 2

8

可能有点晚了,但最近我有同样的问题:我需要动态绘制多个系列(基于用户选择的货币的几条收益率曲线),但我不想直接绑定Plotusing PlotModel,作为其他属性(例如Title)需要在我的视图模型中设置为代码而不是 XAML 标记。

所以我将其定义PlotModel为资源,将其绑定到 Plot。并在加载视图时查找 PlotModel。通过这种方法,我可以通过 XAML 标记定义视觉内容(例如标题、轴、图例等),同时在我的视图模型代码中放置生成系列的逻辑。

不确定这是否是一个好方法,但它解决了我的问题。

1) XAML - 定义资源

<UserControl.Resources>
    <oxyPlot:PlotModel 
        x:Key="TestPlotModel"
        Title="XXX Curves (Preview)"
        Subtitle="Scroll mousewheel to zoom; Right-drag to pan"
        LegendPlacement="Outside"
        LegendBorder="{x:Static Member=oxyPlot:OxyColors.Black}"
        >
        <oxyPlot:PlotModel.Axes>
            <axes:LinearAxis 
                Title="Rate (%)" 
                Position="Left" 
                StartPosition="0" 
                StringFormat="#.00000"
                MajorGridlineStyle="Solid"
                MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                MinorGridlineStyle="Dot"
                MinorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                >
            </axes:LinearAxis>
            <axes:LinearAxis 
                Title="Maturity (Days)" 
                Position="Bottom" 
                StartPosition="0"
                MajorGridlineStyle="Solid"
                MajorGridlineColor="{x:Static Member=oxyPlot:OxyColors.LightGray}"
                >
            </axes:LinearAxis>
        </oxyPlot:PlotModel.Axes>
    </oxyPlot:PlotModel>
</UserControl.Resources>

2) XAML -Plot

<oxy:Plot Grid.Row="1" Model="{Binding Source={StaticResource TestPlotModel}}">
</oxy:Plot>

3) 查看模型 - 从视图中获取模型但不绑定

protected override void OnViewLoaded(object view)
{
    base.OnViewLoaded(view);
    this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
}

4)查看模型 - 生成多个系列

_model.Series.Clear();
foreach (var currency in distinctCurrencies)
{
    IEnumerable<DataPoint> dataPoints = ...;

    LineSeries lineSeries = new LineSeries()
    {
        Title = String.Format("*{0}", currency),
        ItemsSource = dataPoints
    };

    _model.Series.Add(lineSeries);
}

希望能帮助到你!

于 2014-03-06T11:17:08.507 回答
1

Yes, have a look at their examples, you need to bind to a collection of DataPoint

public ObservableCollection<DataPoint> MyCollection { ... }

and

<oxy:Plot.Series>
    <oxy:LineSeries ItemsSource="{Binding MyCollection}" DataFieldX="X" DataFieldY="Y"/>
</oxy:Plot.Series>

The Series property on the Plot type has no setter:

public Collection<Series> Series
{
    get
    {
        return this.series;
    }
}

You can though bind to the Model property, which is a PlotModel type which has a Series collection property with a getter and setter. Have a look at the SimpleDemo for more details.

<oxy:Plot Model="{Binding MyModel}" ...
于 2013-06-19T18:50:41.103 回答