如题,在GDI+中利用多点绘制曲线折线一般用DrawCurve(Pen, Point[])和DrawLines(Pen, Point[])之类的函数,可是这两个函数的第二个参数都要求是纯Point[]数组类型,在绘制动态曲线的时候,显然是要等到程序运行之后才能确定数据,并且数组里的个数也一直在变(第一秒钟有两个点,第二秒钟有三个点,以此类推的增加)。我的想法自然是先定义动态数组 List<Point> myPoints = new List<Point>();需要点的时候直接Add()就行了,但是你把泛型的myPoints传到DrawCurve(Pen, Point[])里面程序是会报错的,myPoints.ToArray();之后也一样不行。
    另外,就算使用下下策最开始的时候定义几个足够大的数组(就暂时不考虑内存浪费了)也不行,比方开始Point[] myPoints=new Point[100](想后面把泛型里面数传会数组)当数组里面的数据点没有100个的时候,里面会有很多的(0,0),那样绘制的曲线后面就有一大堆没用的(0,0)直线。怎么办?怎么办?
    问题应该已经描述清楚了,请教各位大侠,在教师节里,各位也当当好老师吧!

解决方案 »

  1.   

    那就别用这两个函数了,直接用DrawLine,用一个线程去画图,时时根据列表中的点计算坐标
      

  2.   


    这样也行?按照net的风格不至于非要把人往这路上逼吧!另外,画折线的话这样是可以,可我要画的是圆滑曲线啊?还有什么高招没,求教求教~
      

  3.   

    高手啊高手们啊,别都回去看老师了就忘了csdn里的学生啊!!!!!!!!
      

  4.   

    楼主回来了,下面的代码是博客园里网名为“天行健 自强不息”的大侠给的,可以实现传入泛型画直线,但是圆滑的曲线要怎么办?继续向高手求救。想想微软也是的,我觉得他完全有必要把这两个都作为函数的重载版本给出来的……
    private void DrawLines(Graphics grp, List<Point> pointList)
            {
                Point one, two;
                for (int i = 0; i < pointList.Count-1; i++)
                {
                    one = pointList[i];
                    two = pointList[i + 1];
                    grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    grp.DrawLine(new Pen(Color.Red, 2), one,two);
                }
                grp.Dispose();
            }
      

  5.   

    楼主又回来了,人生第一大乐事是编程,第二大乐事是分享~~
    高手们不愿意露面,我自个儿依葫芦画瓢学着上面的写了个,一并贴出来和大家分享。private void DrawCurves(Graphics grp, List<Point> pointList)
            {
                Point[] temps = new Point[pointList.Count];
                for (int i = 0; i < pointList.Count; i++)
                {
                    temps[i] = pointList[i];
                }
                grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                grp.DrawCurve(new Pen(Color.Red, 2), temps);
                grp.Dispose();
            }
    如此,问题大致算解决了,大家还有什么新奇的好的想法,欢迎提出来交流交流……
      

  6.   

    引用ZedGraph.dll
    using ZedGraph;GraphPane myPan = new GraphPane();
            PointPairList list = new PointPairList();
            Random ran = new Random();
            LineItem myCurve;
            DataSet ds = new DataSet();
            SqlConnection myConnection = null;
            SqlCommand myCommand = null;
            SqlDataAdapter adapt = null;
            SqlDataReader reader = null;
            public Form1()
            {
                InitializeComponent();
            }
            public string GetConnectString(string server, string database, string username, string password)
            {
                return "Server=" + server + ";Database=" + database + ";UID=" + username + ";PWD=" + password;
            }        public void Exc()
            {
                double x, y;
                myConnection = new SqlConnection();
                myConnection.ConnectionString = GetConnectString("192.168.0.101", "AMR", "sa", "123");
                myCommand = new SqlCommand("select MSID,UpdateTime from Tab_BS_Dailyfile", myConnection);
                myCommand.CommandType = CommandType.Text;
                myConnection.Open();            adapt = new SqlDataAdapter();            try
                {
                    //myCommand.ExecuteNonQuery();                reader = myCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        for (int i = 0; i < reader.FieldCount; )
                        {
                            y = double.Parse(reader[i++].ToString());
                            x = (double)new XDate(DateTime.Parse(reader[i++].ToString()));
                            list.Add(x, y);
                        }
                    }            }
                catch (SqlException ex)
                {
                    MessageBox.Show("插入错误!" + ex.Message.ToString());
                }
                myCommand.Clone();
                myConnection.Close();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.zedGraphControl1.GraphPane.Title.Text = "动态折线图";            this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";
                this.zedGraphControl1.GraphPane.X2Axis.Scale.FontSpec.Size = 28;            this.zedGraphControl1.GraphPane.YAxis.Title.Text = "数量";
                this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 8;
                myPan.XAxis.Scale.IsVisible = true;
                this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;
                Exc();            DateTime dt = DateTime.Now;            myCurve = zedGraphControl1.GraphPane.AddCurve("My Curve",                    list, Color.DarkGreen, SymbolType.None);            this.zedGraphControl1.AxisChange();            this.zedGraphControl1.Refresh();        }
    /// <summary>
            /// 动起来
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void timer1_Tick(object sender, EventArgs e)
            {
                zedGraphControl1.GraphPane.XAxis.Scale.MaxAuto = true;            double x = (double)new XDate(DateTime.Now);            double y = ran.NextDouble();            list.Add(x, y);//改成你数据库的数据就可以了
                list.RemoveAt(0);            this.zedGraphControl1.AxisChange();            this.zedGraphControl1.Refresh();        }
      

  7.   

    没想到myPoints.ToArray()会是错的,那就只能
    Point[] points=new Point[myPoints.Count],再挨个赋值呗