繁体   English   中英

无法在Windows表单图表控件上画线

[英]Unable to draw lines on windows form chart control

使用Visual Studio 2010 IDE在C#中的Windows窗体上使用图表控件。

我正在创建一个绘制随机点的图表。 生成点后,我对图表中的每个点运行一个循环,查看所有其他点坐标。 在该循环中,我计算了从父点到它的邻居的距离。 如果距离<=我指定的某个距离,我希望画一条线来显示两者之间的连接。 我遇到的问题实际上是在画那条线。 此外,我还需要找到一种方法走这张图中的最短路径。

因此,这实际上是两个问题:1.如何在图表上绘制线条? (当前的困境)2.如何浏览图表以找到最短路径?

这是我正在使用的一些代码的摘要,以及当前的错误:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    public void createNodes(int x, int y, int Nodes, int dgNodes)
    {
        Random rdn = new Random();

        for (int i = 0; i < (Nodes - dgNodes); i++)
        {
            chtGraph.Series["Series1"].Points.AddXY
                        (rdn.Next(x), rdn.Next(y));
        }

        for (int i = 0; i <= dgNodes - 1; i++)
        {
            chtGraph.Series["Series2"].Points.AddXY
                        (rdn.Next(x), rdn.Next(y));
        }
    }

    public void buildGraph(int x, int y, int Nodes, int dgNodes)
    {
        //set the min/max axis on the chart
        chtGraph.ChartAreas["ChartArea1"].AxisX.Maximum = x;
        chtGraph.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
        chtGraph.ChartAreas["ChartArea1"].AxisY.Maximum = y;
        chtGraph.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
        chtGraph.ChartAreas["ChartArea1"].AxisX.Interval = x / 10;
        chtGraph.ChartAreas["ChartArea1"].AxisY.Interval = y / 10;

        chtGraph.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
        chtGraph.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;

        //build all the nodes
        createNodes(x, y, Nodes, dgNodes);
    }

    public void drawEdges(int intNumNodes, int intTransPower)
    {

        Pen pen = new Pen(Color.Black, 1);
        Graphics g = chtGraph.CreateGraphics();

        Point[] pts = new Point[intNumNodes];
        int i = 0;

        //Gather all the data generator data points into a point array
        foreach (DataPoint p in chtGraph.Series[0].Points)
        {
            Point point = new Point((int)p.XValue, (int)p.YValues[0]);
            pts[i] = point;
            i++;
        }

        //Gather all the non data generator into the same point array
        foreach (DataPoint p in chtGraph.Series[1].Points)
        {
            Point point = new Point((int)p.XValue, (int)p.YValues[0]);
            pts[i] = point;
            i++;
        }

        //loop through all the data points
        foreach (Point p in pts)
        {
            //examine all the other data points for each data point visited
            for (int j = 0; j < pts.Length; j++)
            {

                //if the distance from the parent node (p) to the neighbor node is less than the transmit power, then draw a line
                if (Math.Sqrt(Math.Pow((p.X - pts[j].X), 2) + Math.Pow((p.Y - pts[j].Y), 2)) <= intTransPower)
                {
                    //gr.DrawLine(pen, p, pts[j]);
                    //gr.Graphics.DrawLine(pen, p.X, p.Y, pts[j].X, pts[j].Y);

                }
            }
        }
    }

    private void btnExecute_Click(object sender, EventArgs e)
    {
        if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == ""
            || txtXAxis.Text == "" || txtXAxis.Text == "")
        {
            lblError.Text = "Please enter in all inputs!";
            lblError.Visible = true;
            return;
        }
        //create variables for use through program

        int intTransPower = Convert.ToInt32(txtTransPower.Text);
        int intXAxis = Convert.ToInt32(txtXAxis.Text);
        int intYAxis = Convert.ToInt32(txtYAxis.Text);
        int intNum_DG = Convert.ToInt32(txtDG.Text);
        int intNumNodes = Convert.ToInt32(txtNodes.Text);
        int intStorage = Convert.ToInt32(txtStorage.Text);

        lblError.Visible = false;
        lblError.Text = "";

        if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == "" 
            || txtXAxis.Text == "" || txtXAxis.Text == "")
        {
        lblError.Text = "Please enter in all inputs!";
        lblError.Visible = true;}

        chtGraph.Series["Series1"].Points.Clear();
        chtGraph.Series["Series2"].Points.Clear();

        buildGraph(intXAxis, intYAxis, intNumNodes, intNum_DG);

        drawEdges(intNumNodes, intTransPower);
    }

}

错误:错误1类型'System.Windows.Forms.DataVisualization.Charting.ChartGraphics'没有定义构造函数

问题注释中描述的二手事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM