OxyPlot 中的多 LineSeries 绑定 [英] Multiple LineSeries Binding in OxyPlot

查看:64
本文介绍了OxyPlot 中的多 LineSeries 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to bind a plot to a collection of LineSeries instead of a single LineSeries in OxyPlot? (and not through the Model).

我正在寻找这样的东西:

I'm looking for something like this:

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

myCollectionOfLineSeries 在哪里:

Where myCollectionOfLineSeries is:

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

            }
        }

我希望答案是:a)不,这是不可能的"或 b)是的,只需将 XYZ 放在 IJK 之前".

I expect as answer: a) "No, it's impossible" or b) "Yes, just put XYZ before IJK".

感谢阅读.

推荐答案

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

It may be a bit late, but recently I have the same question: I need to plot multiple series dynamically (several yield curves based on user selected currencies) but I don't want to directly bind the Plot using PlotModel, as other properties (e.g. Title) need to be set in my View Model as code instead of XAML markup.

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

So I defined the PlotModel as resource, binding it to the Plot. And look up the PlotModel when the view is loaded. By this approach, I can define visual stuffs (e.g. Title, Axes, Legend, etc) by XAML markup, while putting logic to generate series in my view model code.

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

Not sure if it's a good way, but it solves my problem.

1) XAML - 定义资源

1) XAML - define resource

<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) 视图模型 - 从视图中获取模型但不绑定

3) View model - get the model from view but not binding

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

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

4) View model - generate multiple series

_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);
}

希望能帮到你!

这篇关于OxyPlot 中的多 LineSeries 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆