asp.net 数据图表源码

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;
using PCM.Business.Plan;
using System.Drawing.Drawing2D;
using System.IO;
using System.Collections.Generic;

public partial class Modules_Plan_HCXYPlanTB : PageBase
{
    void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string BillNo = Request["BillNo"] != null ? Request["BillNo"] : "";
            this.hidBillNo.Value = BillNo;
            HCXYPlanMx iHCXYPlanMx = new HCXYPlanMx();
            DataTable dt = iHCXYPlanMx.GetList(BillNo);
            if (dt != null)
            {
                int pagewidth = 600;
                int pageheight = 400;
                if (dt.Rows.Count * 100 > pagewidth)
                {
                    pagewidth = dt.Rows.Count * 100;
                }
                if (dt.Rows.Count * 40 > pageheight)
                {
                    pageheight = dt.Rows.Count * 40;
                }

                LineChart c = new LineChart(pagewidth, pageheight, Page);
                if (dt.Rows.Count > 1)
                {
                    c.ScaleX = dt.Rows.Count - 1; c.Xdivs = dt.Rows.Count - 1;     // x轴 c.ScaleX 总数 ; c.Xdivs 份数
                }
                else
                {
                    c.ScaleX = dt.Rows.Count; c.Xdivs = dt.Rows.Count;     // x轴 c.ScaleX 总数 ; c.Xdivs 份数
                }
                List<float> xx = new List<float>();//单价
                List<float> yy = new List<float>();//日期
                float maxDJ = 0;      //Y轴最大金额

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (float.Parse(dt.Rows[i]["SHDJ"].ToString()) - maxDJ > 0)
                    {
                        maxDJ = float.Parse(dt.Rows[i]["SHDJ"].ToString());
                        for (int l = 1; l < l + 1; l++)
                        {
                            if (l * 100 - maxDJ > 0)
                            {
                                c.ScaleY = l * 100; c.Ydivs = 10;   // y轴 c.ScaleY 总数 ; c.Ydivs 份数  
                                maxDJ = l * 100;      //获取Y轴最大金额
                                break;
                            }
                        }
                    }

                    c.Title = dt.Rows[i]["MaName"].ToString();
                    float SHDJ = float.Parse(dt.Rows[i]["SHDJ"].ToString());
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (i == j)
                        {
                            c.AddValue(j, SHDJ);
                            xx.Add(j);
                            yy.Add(SHDJ);
                        }
                    }
                }
                c.Draw(BillNo, xx, yy, maxDJ);
            }
            else
            {
                Response.Write("<script>alert('没有数据')</script>");
            }

        }
    }
    class LineChart
    {

        public Bitmap b;

        public string Title;

        public ArrayList chartValues = new ArrayList();

        public int Xorigin = 0, Yorigin = 0;

        public int ScaleX, ScaleY;

        public int Xdivs = 2, Ydivs = 2;

        private int Width, Height;

        public Graphics g;

        private Page p;

        struct datapoint
        {
            public float x;
            public float y;
            public bool valid;
        }
        //初始化       

        public LineChart(int myWidth, int myHeight, Page myPage)
        {
            Width = myWidth; Height = myHeight;
            ScaleX = myWidth; ScaleY = myHeight;
            b = new Bitmap(myWidth, myHeight);
            g = Graphics.FromImage(b);
            p = myPage;
        }

        /// <summary>
        /// 设计X,Y轴的值
        /// </summary>
        /// <param name="x">X轴的值</param>
        /// <param name="y">Y轴的值</param>

        public void AddValue(float x, float y)
        {

            datapoint myPoint;
            myPoint.x = x;
            myPoint.y = y;
            myPoint.valid = true;
            chartValues.Add(myPoint);

        }

        public void Draw(string BillNo, List<float> xx, List<float> yy, float maxDJ)
        {
            int i;

            float x, y, x0, y0;

            string myLabel;

            Pen redPen = new Pen(Color.Red, 1);   //折线的颜色,粗细

            Pen grayPen = new Pen(Color.Gray, 1);   //表格中虚线
            grayPen.DashStyle = DashStyle.Dash;

            Brush blackBrush = new SolidBrush(Color.Black);

            Brush redBrush = new SolidBrush(Color.Red);

            Brush blueBrush = new SolidBrush(Color.Blue);     //具体金额的Brush

            Font axesFont = new Font("arial", 10);
            //首先要创建图片的大小
            int ChartInset = 50;
            int ChartWidth = Width - (2 * ChartInset);
            int ChartHeight = Height - (2 * ChartInset);
            p.Response.ContentType = "image/jpeg";
            g.FillRectangle(new SolidBrush(ColorTranslator.FromHtml("#E6FAED")), 0, 0, Width, Height);
            //矩形的设置
            g.DrawRectangle(new Pen(Color.Black, 1), ChartInset, ChartInset, ChartWidth, ChartHeight);
            //写出图片上面的图片内容文字
            g.DrawString(Title, new Font("arial", 10), blackBrush, Width / 3, 10);
            //沿X坐标写入X标签
            HCXYPlanMx iHCXYPlanMx = new HCXYPlanMx();
            DataTable dt = iHCXYPlanMx.GetList(BillNo);
            if (dt != null)
            {
                for (int k = 0; k < dt.Rows.Count; k++)
                {
                    for (i = 0; i <= k; i++)
                    {
                        x = ChartInset + (i * ChartWidth) / Xdivs;
                        y = ChartHeight + ChartInset;
                        myLabel = dt.Rows[i]["SHDate"].ToString();
                        g.DrawString(myLabel, axesFont, blackBrush, x - 30, y + 10);
                        g.DrawString(dt.Rows[i]["SHDJ"].ToString(), new Font("arial", 10), blueBrush, x, y - yy[i] / maxDJ * y );
                        g.DrawLine(grayPen, x, y, x, ChartInset);
                    }
                }
                //沿Y坐标写入Y标签
                for (i = 0; i <= Ydivs; i++)
                {
                    x = ChartInset;
                    y = ChartHeight + ChartInset - (i * ChartHeight / Ydivs);
                    myLabel = (Yorigin + (ScaleY * i / Ydivs)).ToString();
                    g.DrawString(myLabel, axesFont, blackBrush, 5, y - 6);
                    float fenzi = float.Parse((dt.Rows.Count - 1).ToString());
                    float fenmu = float.Parse(dt.Rows.Count.ToString());
                    g.DrawLine(grayPen, x, y, Width - x, x + ((Height - 2 * x) * (Ydivs - i) / Ydivs));
                    //g.DrawLine(grayPen, x, y, x + (Width - 2 * x) * (fenzi / fenmu), x + ((Height - 2 * x) * (Ydivs - i) / Ydivs));
                    //g.DrawLine(grayPen, x, y, x + (Width - 2 * x), x + ((Height - 2 * x) * (Ydivs - i) / Ydivs));
                }
            }
            g.RotateTransform(180);
            g.TranslateTransform(0, -Height);
            g.TranslateTransform(-ChartInset, ChartInset);
            g.ScaleTransform(-1, 1);
            //画出图表中的数据
            datapoint prevPoint = new datapoint();
            prevPoint.valid = false;
            foreach (datapoint myPoint in chartValues)
            {
                if (prevPoint.valid == true)
                {
                    x0 = ChartWidth * (prevPoint.x - Xorigin) / ScaleX;
                    y0 = ChartHeight * (prevPoint.y - Yorigin) / ScaleY;
                    x = ChartWidth * (myPoint.x - Xorigin) / ScaleX;
                    y = ChartHeight * (myPoint.y - Yorigin) / ScaleY;
                    g.DrawLine(redPen, x0, y0, x, y);
                    g.FillEllipse(redBrush, x0 - 2, y0 - 2, 4, 4);
                    g.FillEllipse(redBrush, x - 2, y - 2, 4, 4);
                }
                prevPoint = myPoint;
            }
            //最后以图片形式来浏览
            b.Save(p.Response.OutputStream, ImageFormat.Jpeg);
        }
        ~LineChart()
        {
            g.Dispose();
            b.Dispose();
        }
    }
}

转载于:https://www.cnblogs.com/xiayang123/archive/2012/10/17/2728653.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值