c# - 如何在 OxyPlot 图表上绘制多个 LineSeries?

标签 c# wpf charts oxyplot lineseries

我很抱歉问了这么多 OxyPlot 问题,但我似乎真的很难使用 OxyPlot 图表控件。

我的项目是 WPF 格式的,所以我最初使用的是一个托管的 WINFORMS 图表,它工作起来非常棒,并且完成了我需要它做的所有事情,直到我需要在托管的 winform 图表上覆盖一个 WPF 元素。由于“AirSpace”问题,无论我做什么,我都无法看到放在托管图表顶部的 WPF 元素。那是我决定使用 OxyPlot 的时候,到目前为止,这让我很头疼。

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

我的问题是:

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

到目前为止我的方法:

我正在获取一个 c# 列表数组并添加一个新的 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);

    }

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

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

下面的图是我想要生成的图类型(使用 WinForms Chart 控件效果很好):

Image

每次运行该方法时,我都希望绘制一条具有新颜色的新线。

最佳答案

这就是我之前在 OxyPlot 图表上创建多条线的方式,关键是为每个系列创建一组数据点 - 在以下示例代码中称为 circlePoints 和 linePoints,然后将它们绑定(bind)到 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);

关于c# - 如何在 OxyPlot 图表上绘制多个 LineSeries?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24938689/

相关文章:

c# - 使用 ItextSharp 的带有文本的条形码

c# - Task.Delay 是否启动一个新线程?

javascript - 无法更改 Chart.js 中的默认值

javascript - Zingchart 无法正确绘制超过 10 列的 CSV

c# - #define 的意义何在?

c# - 如何开始使用 SQlite 和 .net 4.0?

wpf - 为什么 WPF 主题不使用 SystemColors?

wpf - 分组组合框显示组标题,但不显示项目

c# - 从主 Viewmodel 调用 subview 模型方法

html - SVG - 带有图像背景的图案 - 图像填充不同 - 适应它们包含的形状