如何在OxyPlot图表上绘制MULTIPLE LineSeries?

我为提出这么多OxyPlot问题而道歉,但我似乎真的在努力使用OxyPlot图表控件。

我的项目是WPF格式,所以我最初使用的是托管的WINFORMS图表,它就像一个魅力,并且做了我需要的所有内容,直到我需要在托管的winform图表上覆盖WPF元素。 由于“AirSpace”问题,无论我做什么,我都无法看到我放在托管图表顶部的WPF元素。 那时候我决定选择OxyPlot,这给我带来了很多令人头痛的问题。

这是我的原始问题 ! 我在CodePlex询问过。 我似乎没有得到太多帮助,所以我在这里再试一次。

我的问题是:

有谁知道如何将多个LineSeries绘制到一个图上?

到目前为止我的方法:

我正在使用ac#List数组并添加一个包含要绘制的新数据的LineSeries的新副本。 我的代码:

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

“`

我的代码第一次按下“图形数据”按钮时工作,但在连续尝试时失败并出现以下错误:

元素无法添加,它已经属于Plot模型

下图是我想要生成的绘图类型(使用WinForms Chart控件可以正常工作):

图片http://sofzh.miximages.com/c%23/jqs3n9.jpg

我想在每次运行方法时绘制一个新颜色的新线。

这就是我之前在OxyPlot图表上创建多行的关键,关键是为每个系列创建一组DataPoints – 在下面的示例代码中称为circlePoints和linePoints,然后将它们绑定到CircleSeries和LineSeries:

 var xAxis = new DateTimeAxis { Position = AxisPosition.Bottom, StringFormat = Constants.MarketData.DisplayDateFormat, Title = "End of Day", IntervalLength = 75, MinorIntervalType = DateTimeIntervalType.Days, IntervalType = DateTimeIntervalType.Days, MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.None, }; var yAxis = new LinearAxis { Position = AxisPosition.Left, Title = "Value", MajorGridlineStyle = LineStyle.Solid, MinorGridlineStyle = LineStyle.None }; var plot = new PlotModel(); plot.Axes.Add(xAxis); plot.Axes.Add(yAxis); var circlePoints = new[] { new ScatterPoint(DateTimeAxis.ToDouble(date1), value1), new ScatterPoint(DateTimeAxis.ToDouble(date2), value2), }; var circleSeries = new ScatterSeries { MarkerSize = 7, MarkerType = MarkerType.Circle, ItemsSource = circlePoints }; var linePoints = new[] { new DataPoint(DateTimeAxis.ToDouble(date1), value1), new DataPoint(DateTimeAxis.ToDouble(date2), value2), }; var lineSeries = new LineSeries { StrokeThickness = 2, Color = LineDataPointColor, ItemsSource = linePoints }; plot.Series.Add(circleSeries); plot.Series.Add(lineSeries); 

Sucess !!!!

AwkwardCoder,谢谢你的帮助,但我意识到我的错误就是我忽略了一些事情!

以下是适用的代码版本:

  // 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 listPointAray = new List(); // 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); } 

以下是我做错的多件事:

  1. 在我的foreach var系列循环中,我添加了已经添加的原始系列,而不是列表中的下一个var系列! (哑!)
  2. 每次运行方法时我都在创建一个新模型。 这意味着每次代码运行时,我都会添加一个已存在于上一个模型中的系列。 (也是哑巴!)
  3. 我每次都在创建一个新的情节,并尝试在已经属于之前情节的新情节中添加模型。 (变得笨拙……)
  4. 每次运行方法时都会将绘图添加到网格中,因此在重新添加相同的绘图之前,我必须首先清除网格的子节点。
  5. 我没有让情节更新清楚。

这是很多错误,但我通过它完成了。 希望这可以帮助将来的某个人。 另外,我知道我没有使用普通的数据绑定技术,但至少可以这样做。

最终结果: 工作情节http://sofzh.miximages.com/c%23/359b7d2.png