【问题标题】:Multiple LineSeries Binding in OxyPlotOxyPlot 中的多 LineSeries 绑定
【发布时间】:2013-06-16 10:35:30
【问题描述】:

是否可以将绘图绑定到 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 之前”。

感谢阅读。

【问题讨论】:

    标签: c# wpf plot oxyplot


    【解决方案1】:

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

    所以我将PlotModel 定义为资源,将其绑定到绘图。并在加载视图时查找 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);
    }
    

    希望对你有帮助!

    【讨论】:

    • 这不是解决方案。您应该将 PlotModel 移动到您的 ViewModel 中,并在您的 viewmodel 中添加多个系列。 PlotModel 必须从 ViewModel 绑定。这没办法:this._model = (PlotModel)((XXXView)view).FindResource("TestPlotModel");
    【解决方案2】:

    是的,看看他们的examples,你需要绑定到DataPoint的集合

    public ObservableCollection<DataPoint> MyCollection { ... }
    

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

    Plot type 上的 Series 属性没有设置器:

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

    您可以绑定到Model 属性,这是一个PlotModel 类型,它有一个带有getter 和setter 的Series 集合属性。请查看SimpleDemo 了解更多详情。

    <oxy:Plot Model="{Binding MyModel}" ...
    

    【讨论】:

    • 我不确定我是否理解你!从我的角度来看,您在回答中的建议与绑定单个 LineSeries 相同。我想知道是否可以绑定到 LineSeries 的 collection,或者在这种情况下,绑定到 DataPoint 的 collection of collection。谢谢
    • “属性“系列”没有可访问的设置器。”它说,我尝试了几种绑定“系列”的方法,但到目前为止没有成功。 也不起作用。
    • objo(OxyPlot 的创建者):“不,这是不可能的。您需要为要显示的每一行创建一个 LineSeries。创建一个 PlotModel 并绑定 Model 属性或使用代码-后面更动态地操作系列集合。我相信还有其他方法可以仅在 XAML 中执行此操作(可能使用附加属性??)“。谢谢,最后我按照您的建议使用模型。
    • 19.3.2017: Model 不再是 &lt;oxy:Plot /&gt; 的财产,而是 &lt;oxy:PlotView /&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    相关资源
    最近更新 更多