如何在OxyPlot图表上绘制多个LineSeries? [英] How to plot MULTIPLE LineSeries on an OxyPlot chart?

查看:295
本文介绍了如何在OxyPlot图表上绘制多个LineSeries?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉问了这么多OxyPlot图表问题,但我似乎在使用OxyPlot图表控件时确实很挣扎。



我的项目采用WPF格式,因此我最初使用的是托管的WINFORMS图表,它的工作原理非常吸引人,并且我做了我需要做的所有事情,直到我需要覆盖一个WPF元素位于托管的winform图表顶部。由于出现 AirSpace问题,无论执行什么操作,我都看不到放置在托管图表之上的WPF元素。那是我决定使用OxyPlot的时候,到目前为止,这让我有些头疼。



这是我的



我想绘制一条带有新颜色的新线

解决方案

成功!!!



AwkwardCoder,谢谢您的帮助,但我意识到我的错误只是我忽略了一些东西!



以下是适用的代码版本:

  //创建新的plotmodel 
private PlotModel model = new PlotModel();

//为盐分分割创建OxyPlot图
私有OxyPlot.Wpf.PlotView plot = new OxyPlot.Wpf.PlotView();

//绘制数据的功能
private void plotData(double numWeeks,double startingSS)
{
List< LineSeries> listPointAray = new List< LineSeries>();

//初始化新的Salt Split类以访问数据变量
Salt_Split_Builder calcSS = new Salt_Split_Builder();
calcSS.compute(numWeeks,startingSS,maxDegSS);

//创建新的线系列
LineSeries linePoints = new LineSeries()
{StrokeThickness = 1,MarkerSize = 1,Title = numWeeks.ToString()+ weeks} ;


//将每个点添加到新系列
foreach(calcSS.saltSplitCurve中的变量点)
{
DataPoint XYpoint = new DataPoint() ;
XYpoint =新的DataPoint(point.Key,point.Value * 100);
linePoints.Format(%,XYpoint.Y);
linePoints.Points.Add(XYpoint);
}

listPointAray.Add(linePoints);

//添加图表标题
model.Title = Salt Split Degradation;

//将每个系列添加到
foreach(listPointAray中的var系列)
{
//定义X轴
OxyPlot.Axes.LinearAxis Xaxis =新的OxyPlot.Axes.LinearAxis();
Xaxis.Maximum = numWeeks;
Xaxis.Minimum = 0;
Xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
Xaxis.Title =周数;
model.Axes.Add(Xaxis);

//定义Y轴
OxyPlot.Axes.LinearAxis Yaxis = new OxyPlot.Axes.LinearAxis();
Yaxis.MajorStep = 15;
Yaxis.Maximum = calcSS.saltSplitCurve.Last()。Value * 100;
Yaxis.MaximumPadding = 0;
Yaxis.Minimum = 0;
Yaxis.MinimumPadding = 0;
Yaxis.MinorStep = 5;
Yaxis.Title =降级百分比;
//Yaxis.StringFormat = {0.00}%;
model.Axes.Add(Yaxis);

model.Series.Add(series);
}


//将绘图添加到窗口

plot.Model = model;
plot.InvalidatePlot(true);
SaltSplitChartGrid.Children.Clear();
SaltSplitChartGrid.Children.Add(plot);

}

这是我做错的多件事:


  1. 在我的foreach var系列循环中,我添加的是已添加的原始系列,而不是列表中的下一个var系列! (哑巴!)

  2. 每次运行该方法时,我都会创建一个新模型。这意味着每次运行代码时,我都会添加一个先前模型中已经存在的系列。 (也是愚蠢的!)

  3. 我每次都创建一个新图,并尝试在新图中添加已经属于先前图的模型。 (变得更暗了。。)

  4. 每次运行该方法时,都会将图添加到网格中,因此在重新添加相同图之前,我必须先清除网格的子级。 / li>
  5. 我没有刷新情节。

那是很多错误,但我可以通过这。希望这对以后的人有所帮助。另外,我知道我没有使用普通的数据绑定技术,但这至少是可行的。



最终结果:


I apologize for asking so many OxyPlot questions, but I seem to be really struggling with using the OxyPlot chart control.

My project is in WPF format so I was originally using a hosted WINFORMS chart and that worked like a charm and did absolutely everything I needed it to until I needed to overlay a WPF element on top of the hosted winform chart. Due to the "AirSpace" issue, I was not able to see the WPF element that I put on top of the hosted chart no matter what I did. That is when I decided to go with OxyPlot, which is giving me quite a few headaches so far.

Here is my origional question! that I asked over at CodePlex. I don't seem to be getting much help over there so I am trying again here.

My question is:

Does anyone know how to plot MULTIPLE LineSeries onto a Plot??

My approach so far:

I am taking a c# List array and adding a new copy of the LineSeries that holds new data to be plotted. My code:

    // Function to plot data
    private void plotData(double numWeeks, double startingSS)
    {

        // Initialize new Salt Split class for acess to data variables
        Salt_Split_Builder calcSS = new Salt_Split_Builder();
        calcSS.compute(numWeeks, startingSS, maxDegSS);

        // Create the OxyPlot graph for Salt Split
        OxyPlot.Wpf.PlotView plot = new OxyPlot.Wpf.PlotView();

        var model = new PlotModel();

        // Add Chart Title
        model.Title = "Salt Split Degradation";

        // Create new Line Series
        LineSeries linePoints = new LineSeries() { StrokeThickness = 1, MarkerSize = 1, Title = numWeeks.ToString() + " weeks" };

        // Add each point to the new series
        foreach (var point in calcSS.saltSplitCurve)
        {
            DataPoint XYpoint = new DataPoint();
            XYpoint = new DataPoint(point.Key, point.Value * 100);
            linePoints.Format("%", XYpoint.Y);
            linePoints.Points.Add(XYpoint);
        }

        listPointAray.Add(linePoints);

        // Define X-Axis
        var Xaxis = new OxyPlot.Axes.LinearAxis();
        Xaxis.Maximum = numWeeks;
        Xaxis.Minimum = 0;
        Xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
        Xaxis.Title = "Number of Weeks";
        model.Axes.Add(Xaxis);

        //Define Y-Axis
        var Yaxis = new OxyPlot.Axes.LinearAxis();
        Yaxis.MajorStep = 15;
        Yaxis.Maximum = calcSS.saltSplitCurve.Last().Value * 100;
        Yaxis.MaximumPadding = 0;
        Yaxis.Minimum = 0;
        Yaxis.MinimumPadding = 0;
        Yaxis.MinorStep = 5;
        Yaxis.Title = "Percent Degradation";
        model.Axes.Add(Yaxis);

        // Add Each series to the
        foreach (var series in listPointAray)
        {
            LineSeries newpoints = new LineSeries();
            newpoints = linePoints;
            model.Series.Add(newpoints);
        }

        // Add the plot to the window
        plot.Model = model;
        SaltSplitChartGrid.Children.Add(plot);

    }

My code works the first time I press my "Graph Data" button, but fails on consecutive attempts with the following error:

The element cannot be added, it already belongs to a Plot Model

The following plot is the type of plot I would like to produce (it worked fine using WinForms Chart control):

I would like a new line with a new color to be plotted each time I run the method.

解决方案

Sucess!!!!

AwkwardCoder, thank you for the help, but I realized my mistake was just me having overlooked some things!

Here is the version of the code that works:

        // Make a new plotmodel
    private PlotModel model = new PlotModel();

    // Create the OxyPlot graph for Salt Split
    private OxyPlot.Wpf.PlotView plot = new OxyPlot.Wpf.PlotView();

    // Function to plot data
    private void plotData(double numWeeks, double startingSS)
    {
        List<LineSeries> listPointAray = new List<LineSeries>();

        // Initialize new Salt Split class for acess to data variables
        Salt_Split_Builder calcSS = new Salt_Split_Builder();
        calcSS.compute(numWeeks, startingSS, maxDegSS);

        // Create new Line Series
        LineSeries linePoints = new LineSeries() 
        { StrokeThickness = 1, MarkerSize = 1, Title = numWeeks.ToString() + " weeks" };


        // Add each point to the new series
        foreach (var point in calcSS.saltSplitCurve)
        {
            DataPoint XYpoint = new DataPoint();
            XYpoint = new DataPoint(point.Key, point.Value * 100);
            linePoints.Format("%", XYpoint.Y);
            linePoints.Points.Add(XYpoint);
        }

        listPointAray.Add(linePoints);

        // Add Chart Title
        model.Title = "Salt Split Degradation";

        // Add Each series to the
        foreach (var series in listPointAray)
        {
            // Define X-Axis
            OxyPlot.Axes.LinearAxis Xaxis = new OxyPlot.Axes.LinearAxis();
            Xaxis.Maximum = numWeeks;
            Xaxis.Minimum = 0;
            Xaxis.Position = OxyPlot.Axes.AxisPosition.Bottom;
            Xaxis.Title = "Number of Weeks";
            model.Axes.Add(Xaxis);

            //Define Y-Axis
            OxyPlot.Axes.LinearAxis Yaxis = new OxyPlot.Axes.LinearAxis();
            Yaxis.MajorStep = 15;
            Yaxis.Maximum = calcSS.saltSplitCurve.Last().Value * 100;
            Yaxis.MaximumPadding = 0;
            Yaxis.Minimum = 0;
            Yaxis.MinimumPadding = 0;
            Yaxis.MinorStep = 5;
            Yaxis.Title = "Percent Degradation";
            //Yaxis.StringFormat = "{0.00} %";
            model.Axes.Add(Yaxis);

            model.Series.Add(series);
        }


        // Add the plot to the window

        plot.Model = model;
        plot.InvalidatePlot(true);
        SaltSplitChartGrid.Children.Clear();
        SaltSplitChartGrid.Children.Add(plot);

    }

Here are the multiple things I did wrong:

  1. In my foreach var series loop, I was adding the original series which had already been added and NOT the next var series in the list! (dumb!)
  2. I was creating a new model each time I ran the method. This means that each time the code ran, I was adding a series that already existed in the previous model. (also dumb!)
  3. I was creating a new plot every time and trying to add a model in the new plot that already belonged to a previous plot. (getting dummer..)
  4. The plot was being added to the grid each time I ran the method, so I had to CLEAR the grid's children first before re-adding the same plot.
  5. I was not refreshing the plot.

That was a lot of mistakes, but I worked through it. Hopefully this helps someone in the future. Also, I know I am not using ordinary data binding techniques, but this, at-least, works.

Final result:

这篇关于如何在OxyPlot图表上绘制多个LineSeries?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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