c# - 如何在 OxyPlot 中绘制自定义函数的图形?

标签 c# windows winforms plot oxyplot

我正在尝试为我的数据绘制趋势线。无论如何要定义一个自定义函数?我见过的最接近的是此处的 Hello Windows Forms 示例中的 with:http://www.oxyplot.org/doc/HelloWindowsForms.html

代码:

namespace WindowsFormsApplication1
{
    using System;
    using System.Windows.Forms;

    using OxyPlot;
    using OxyPlot.Series;

    public partial class Form1 : Form
    {
        public Form1()
        {
            this.InitializeComponent();
            var myModel = new PlotModel("Example 1");
            myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
            this.plot1.Model = myModel;
        }
    }
}

在示例中,他们使用余弦。如果我需要定义自定义多变量方程怎么办?

编辑: 我找到了部分答案。

使用 Lambda 系列:

new FunctionSeries( x => a*x*x*x + b*x*x + c*x + d, .... )

来源: https://oxyplot.codeplex.com/discussions/439064

虽然仍然不知道如何计算多变量方程。

最佳答案

示例图片如下: the image from the function below 这是代码:

    //your function based on x,y
    public double getValue(int x, int y)
    {
        return (10 * x * x + 11 * x*y*y  + 12*x*y );
    }

    //setting the values to the function
    public FunctionSeries GetFunction()
    { 
        int n = 100;
        FunctionSeries serie = new FunctionSeries();
        for (int x = 0; x < n; x++)
        {
            for (int y = 0; y < n; y++)
            {
                //adding the points based x,y
                DataPoint data = new DataPoint(x, getValue(x,y));

                //adding the point to the serie
                serie.Points.Add(data);
            }
        }
        //returning the serie
        return serie;
    }

    //setting all the parameters of the model
    public void graph()
    {
        model = new PlotModel { Title = "example" };
        model.LegendPosition = LegendPosition.RightBottom;
        model.LegendPlacement = LegendPlacement.Outside;
        model.LegendOrientation = LegendOrientation.Horizontal;

        model.Series.Add(GetFunction());
        var Yaxis = new OxyPlot.Axes.LinearAxis();
        OxyPlot.Axes.LinearAxis XAxis = new OxyPlot.Axes.LinearAxis { Position = OxyPlot.Axes.AxisPosition.Bottom, Minimum = 0, Maximum = 100 };
        XAxis.Title = "X";
        Yaxis.Title = "10 * x * x + 11 * x*y*y  + 12*x*y";
        model.Axes.Add(Yaxis);
        model.Axes.Add(XAxis);
        this.plot.Model = model;
    }

    //on click on the button 3 then show the graph
    private void button3_Click(object sender, EventArgs e)
    {
        graph();
    }

关于c# - 如何在 OxyPlot 中绘制自定义函数的图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313258/

相关文章:

c# - 自定义 Visual Studio

C# 组件事件?

windows - 在 Xperf 中捕获调用堆栈和事件

c# - 消息为 "The request was aborted: The request was canceled."的 WebException 的根本原因是什么

windows - 如何在 Windows/DOS 上捕获 stderr?

c++ - 在 Windows 上使用多线程文件 IO 的 SHARING_VIOLATION

c# - 信息标签

c# - Button.PerformClick 有什么作用?

c# - 如何使用 Java 客户端导入 WCF Web 服务

c# - 捕获的正则表达式超出预期