一个画折线图的通用代码(.net)

  我在写一个网上评议的子系统时,费了好大的劲才从网上找了一段代码.把它少微的改了一下,比原来好用的多,现在给大家共享一下;

在用这函数之前,必须引用using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System.Drawing.Text;using System.IO这几个命名空间.下面是这个函数的代码:

 public void PieChart(DataSet charData)
    {
        string[] month = new string[12] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
       
        ///从数据库读取
        int[] d = new int[12];
        DataTable dt = charData.Tables[0];
        int c = dt.Rows.Count;
        for (int i = 0; i < c;i++ )
        {
            d[i] =Convert.ToInt32(dt.Rows[i][0]);
        }

        //画图初始化
        Bitmap bMap = new Bitmap(500, 500);
        Graphics gph = Graphics.FromImage(bMap);
        gph.Clear(Color.White);

        PointF cPt = new PointF(40, 420);//中心点
        PointF[] xPt = new PointF[3] { new PointF(cPt.Y + 15, cPt.Y), new PointF(cPt.Y, cPt.Y - 8), new PointF(cPt.Y, cPt.Y + 8) };//X轴三角形
        PointF[] yPt = new PointF[3] { new PointF(cPt.X, cPt.X - 15), new PointF(cPt.X - 8, cPt.X), new PointF(cPt.X + 8, cPt.X) };//Y轴三角形
        gph.DrawString("评议结果图表", new Font("宋体", 14), Brushes.Black, new PointF(cPt.X + 60, cPt.X));//图表标题
        //画X轴
        gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y);
        gph.DrawPolygon(Pens.Black, xPt);
        gph.FillPolygon(new SolidBrush(Color.Black), xPt);
        gph.DrawString("次数", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 10, cPt.Y + 10));
        //画Y轴
        gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);
        gph.DrawPolygon(Pens.Black, yPt);
        gph.FillPolygon(new SolidBrush(Color.Black), yPt);
        gph.DrawString("分值", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
        for (int i = 1; i <= c; i++)
        {
            //画Y轴刻度
            if (i < c-1)
            {
                gph.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6));
                gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);
            }
            //画X轴项目
            gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 5));
            //gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 20));
            if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 35));
            //画点
            gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
            gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3);
            //画数值
            gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3));
            //画折线
            if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3);
        }
        //保存输出图片
        //(Response.OutputStream, ImageFormat.Gif);
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bMap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Gif";
        Response.BinaryWrite(ms.ToArray());
        gph.Dispose();
        bMap.Dispose();

在这我感谢这代码的作者,谢谢他为我们做的贡献,小弟在这拜谢了.
    }

你可能感兴趣的:(一个画折线图的通用代码(.net))