2

我想在我的 Windows 窗体中停靠一个 OxyPlot 图并绘制函数图y = 2x - 7。我已经下载了 OxyPlot 并将引用添加到我的项目中。我使用以下代码将绘图添加到我的表单中:

public partial class GraphForm : Form
{
    public OxyPlot.WindowsForms.Plot Plot;

    public Graph()
    {
        InitializeComponent();

        Plot = new OxyPlot.WindowsForms.Plot();
        Plot.Model = new PlotModel();
        Plot.Dock = DockStyle.Fill;
        this.Controls.Add(Plot);

        Plot.Model.PlotType = PlotType.XY;
        Plot.Model.Background = OxyColor.FromRgb(255, 255, 255);
        Plot.Model.TextColor = OxyColor.FromRgb(0, 0, 0);
    }
}

使用此代码,我看到了白色背景,控件已创建,但它只是白色背景。我环顾了OxyPlot.Plot班上的成员,但找不到一种方法来制定我的方程式。如何在图表中绘制我的方程?

4

1 回答 1

5

您需要添加一些要显示的数据,将其添加到 Models Series 属性中。

线 (X,Y) 图示例。

    public Graph()
    {
        InitializeComponent();

        Plot = new OxyPlot.WindowsForms.Plot();
        Plot.Model = new PlotModel();
        Plot.Dock = DockStyle.Fill;
        this.Controls.Add(Plot);

        Plot.Model.PlotType = PlotType.XY;
        Plot.Model.Background = OxyColor.FromRGB(255, 255, 255);
        Plot.Model.TextColor = OxyColor.FromRGB(0, 0, 0);

        // Create Line series
        var s1 = new LineSeries { Title = "LineSeries", StrokeThickness = 1 };
        s1.Points.Add(new DataPoint(2,7));
        s1.Points.Add(new DataPoint(7, 9));
        s1.Points.Add(new DataPoint(9, 4));

        // add Series and Axis to plot model
        Plot.Model.Series.Add(s1);
        Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Bottom, 0.0, 10.0));
        Plot.Model.Axes.Add(new LinearAxis(AxisPosition.Left, 0.0, 10.0));

    }

这个例子:

在此处输入图像描述

于 2013-01-18T01:45:16.640 回答