c# GDI+畫折線圖(初級版本)

來源:互聯網
上載者:User

        /// <summary>
        /// 製作折線圖類
        /// </summary>
        /// <param name="x">中心點x值</param>
        /// <param name="y">中心點y值</param>
        /// <param name="xylength">x,y軸長度</param>
        /// <param name="xString">x軸說明</param>
        /// <param name="yString">y軸說明</param>
        /// <param name="xInfo">x資料</param>
        /// <param name="yInfo">y資料</param>
        /// <param name="maxPoint">從資料庫擷取y軸最大資料</param> 

public Bitmap CreateLine(float x,float y,float xylength,string xString,string yString,string[] xInfo,float[] yInfo,float maxPoint)
        {
            Bitmap bitmap = new Bitmap(this.Width, this.Height);//定義畫布
            Graphics g = Graphics.FromImage(bitmap);
            g.Clear(Color.White);

            PointF p = new PointF(x, y);//中心點
            PointF[] xpt = new PointF[3] { new PointF(p.X + xylength + 15, p.Y), new PointF(p.X + xylength, p.Y - 8), new PointF(p.X + xylength, p.Y + 8) };//x軸三角形
            PointF[] ypt = new PointF[3] { new PointF(p.X, p.Y - xylength - 15), new PointF(p.X - 8, p.Y - xylength), new PointF(p.X + 8, p.Y - xylength) };//y軸三角形

            //x
            g.DrawLine(Pens.Black, p.X, p.Y, p.X + xylength, p.Y);
            g.DrawPolygon(Pens.Black, xpt);
            g.FillPolygon(Brushes.Black, xpt);
            g.DrawString(xString, new Font("宋體", 12), new SolidBrush(Color.Red), p.X + xylength + 10, p.Y + 10);

            //y
            g.DrawLine(Pens.Black, p.X, p.Y - xylength, p.X, p.Y);
            g.DrawPolygon(Pens.Black, ypt);
            g.FillPolygon(Brushes.Black, ypt);
            g.DrawString(yString, new Font("宋體", 12), new SolidBrush(Color.Red), p.X - 40, p.Y - xylength - 30);
            //像素點
            int point = (int)(p.Y / xInfo.Length);

            int ypoint = 0;
            if (maxPoint.ToString().Substring(0, 1) == "1")
            {
                ypoint = 10;
            }
            else
            {
                int sale = Convert.ToInt32(maxPoint);
                int length = sale.ToString().Length;
                string max = maxPoint.ToString().Substring(0, 1);
                string cz = "1";
                for (int a = 1; a < length; a++)
                {
                    cz += "0";
                    max += "0";
                }
                maxPoint = Convert.ToSingle(cz) + Convert.ToSingle(max);
                ypoint = (int)(maxPoint / Convert.ToSingle(cz));

            }

            //畫Y刻度
            for (int kd = 1; kd <= ypoint; kd++)
            {
                float kdy = p.Y - (360 / maxPoint) * (maxPoint / ypoint * kd);
                g.DrawString(Convert.ToString(maxPoint / ypoint * kd), new Font("宋體", 10), Brushes.Black, p.X - 40, kdy - 5);
                g.DrawLine(Pens.Black, p.X - 5, kdy, p.X, kdy);
            }

            for (int i = 0; i < xInfo.Length; i++)
            {

                //畫x軸項目
                g.DrawString(xInfo[i], new Font("宋體", 10), Brushes.Black, new PointF(p.X + i * point - 5, p.Y + 5));

                g.DrawEllipse(Pens.Black, p.X + i * point - 1.5f, p.Y - 360 / maxPoint * yInfo[i], 3, 3);
                g.FillEllipse(new SolidBrush(Color.Black), p.X + i * point - 1.5f, p.Y - 360 / maxPoint * yInfo[i], 3, 3);

                //畫數值
                g.DrawString(yInfo[i].ToString(), new Font("宋體", 10), Brushes.Black, p.X + i * point, p.Y - 360 / maxPoint * yInfo[i]);
                //畫折線
                if (i + 1 < xInfo.Length)
                    g.DrawLine(Pens.Red, p.X + i * point, p.Y - 360 / maxPoint * yInfo[i], p.X + (i + 1) * point, p.Y - 360 / maxPoint * yInfo[i + 1]);
            }
            return bitmap;
        }

 

//在表單中調用

 DataSet ds = DBUitily.ExecuteQuerySql("select * from t_Users");//橫座標資料來源
            string[] Users = new string[ds.Tables[0].Rows.Count];
            float[] Sales = new float[ds.Tables[0].Rows.Count];
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Users[i] = ds.Tables[0].Rows[i][0].ToString();
                Sales[i] = Convert.ToSingle(DBUitily.ExecuteScalarSql("select sum(SaleMoney) from t_Sales where UserId='" + Users[i] + "'"));//縱座標資料來源
            }
            float maxValue = Convert.ToSingle(DBUitily.ExecuteScalarSql("select Sum(SaleMoney) from t_Sales group by UserId order by Sum(SaleMoney) Desc"));

            picChart.Image = CreateLine(300, 450, 400, "人員編號", "銷售價格(元)", Users, Sales, maxValue);

片:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.