wl13k1 2017-05-18 12:32 采纳率: 0%
浏览 826

C#以下代码可实现在网页中显示饼图,但我要想在Panel中显示该怎样修改

    protected void Page_Load(object sender, EventArgs e)
    {
        DB db = new DB();
        string id = Application["id1"].ToString();
        Label15.Text = id;
        TextBox8.Text = id;
        string sqlStr1 = "select 限额 from 普通用户信息表 where 账号=" + id;
        TextBox1.Text = db.reDt(sqlStr1).Rows[0]["限额"].ToString();
        string sqlStr2 = "select 密码 from 普通用户信息表 where 账号=" + id;
        TextBox3.Text = db.reDt(sqlStr2).Rows[0]["密码"].ToString();
        string sqlStr3 = "select 余额 from 普通用户信息表 where 账号=" + id;
        TextBox9.Text = db.reDt(sqlStr3).Rows[0]["余额"].ToString();
        string sqlStr4 = "select 补助金额 from 补助领取信息表 where 账号=" + id;
        TextBox7.Text = db.reDt(sqlStr4).Rows[0]["补助金额"].ToString();
        string sqlStr5 = "select 余额 from 普通用户信息表 where 账号=" + id;
        TextBox12.Text = db.reDt(sqlStr5).Rows[0]["余额"].ToString();
        Panel15.Visible = false;
        Panel16.Visible = false;
        DB db = new DB();
        string cmdtxt = "select * from 消费信息表 where 账号='131909030115'";
        DataTable dt = db.reDt(cmdtxt);
        float Total = 0.0f, Tmp;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //转换成单精度,也可写成Convert.ToInt32
            Tmp = Convert.ToSingle(dt.Rows[i]["消费金额"]);
            Total += Tmp;
        }
        //设置字体,fonttitle为主标题的字体
        Font fontlegend = new Font("verdana", 9);
        Font fonttitle = new Font("verdana", 10, FontStyle.Bold);
        //背景宽
        int width = 230;
        int bufferspace = 15;
        int legendheight = fontlegend.Height * (dt.Rows.Count + 1) + bufferspace;
        int titleheight = fonttitle.Height + bufferspace;
        int height = width + legendheight + titleheight + bufferspace;//白色背景高
        int pieheight = width;
        Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);
        //加上各种随机色
        ArrayList colors = new ArrayList();
        Random rnd = new Random();
        for (int i = 0; i < dt.Rows.Count; i++)
            colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
        //创建一个bitmap实例
        Bitmap objbitmap = new Bitmap(width, height);
        Graphics objgraphics = Graphics.FromImage(objbitmap);
        //画一个白色背景
        objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
        //画一个亮黄色背景
        objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);
        //以下为画饼图(有几行row画几个)
        float currentdegree = 0.0f;
        float[] Tot = { 0.0f, 0.0f, 0.0f };
        float Tmp1, Tmp2, Tmp3;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if ((String)dt.Rows[i]["消费类型"] == "饮食")
            {
                //转换成单精度,也可写成Convert.ToInt32
                Tmp1 = Convert.ToSingle(dt.Rows[i]["消费金额"]);
                Tot[0] += Tmp1;
            }
            if ((String)dt.Rows[i]["消费类型"] == "学习")
            {
                //转换成单精度,也可写成Convert.ToInt32
                Tmp2 = Convert.ToSingle(dt.Rows[i]["消费金额"]);
                Tot[1] += Tmp2;
            }
            if ((String)dt.Rows[i]["消费类型"] == "生活")
            {
                //转换成单精度,也可写成Convert.ToInt32
                Tmp3 = Convert.ToSingle(dt.Rows[i]["消费金额"]);
                Tot[2] += Tmp3;
            }
        }
        for (int i = 0; i < 3; i++)
        {
            objgraphics.FillPie((SolidBrush)colors[i], pierect, currentdegree, Convert.ToSingle(Tot[i]) / Total * 360);
            currentdegree += Convert.ToSingle(Tot[i]) / Total * 360;
        }
        //以下为生成主标题
        SolidBrush blackbrush = new SolidBrush(Color.Black);
        string title = "各种消费类型所占比例";
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
        objgraphics.DrawString(title, fonttitle, blackbrush, new Rectangle(0, 0, width, titleheight), stringFormat);
        //列出各字段及所占百分比
        objgraphics.DrawRectangle(new Pen(Color.Black, 2), 0, height - legendheight, width, legendheight);
        for (int i = 0; i < 3; i++)
        {
            if (i == 0)
            {
                objgraphics.FillRectangle((SolidBrush)colors[i], 5, height - legendheight + fontlegend.Height * i + 5, 10, 10);
                objgraphics.DrawString(("饮食") + "——" + Convert.ToString(Convert.ToSingle(Tot[i]) * 100 / Total).Substring(0, 2) + "%", fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * i + 1);
            }
            if (i == 1)
            {
                objgraphics.FillRectangle((SolidBrush)colors[i], 5, height - legendheight + fontlegend.Height * i + 5, 10, 10);
                objgraphics.DrawString(("学习") + "——" + Convert.ToString(Convert.ToSingle(Tot[i]) * 100 / Total).Substring(0, 2) + "%", fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * i + 1);
            }
            if (i == 2)
            {
                objgraphics.FillRectangle((SolidBrush)colors[i], 5, height - legendheight + fontlegend.Height * i + 5, 10, 10);
                objgraphics.DrawString(("生活") + "——" + Convert.ToString(Convert.ToSingle(Tot[i]) * 100 / Total).Substring(0, 2) + "%", fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * i + 1);
            }
        }
        //图像总的高度——一行字体的高度,即是最底行的一行字体高度(height-fontlegend.Height)
        objgraphics.DrawString("消费总数:" + Convert.ToString(Total) + "元", fontlegend, blackbrush, 5, height - fontlegend.Height);
        Response.ContentType = "image/Jpeg";
        objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        objgraphics.Dispose();
        objbitmap.Dispose();
    }
  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2017-05-18 12:44
    关注

    你这个页面输出动态图片而已,用img标签加载就行了,panel对应客户端的div,无法加载图片。你要放panel里面下面的html放panel里面就行了

     <img src="你这个aspx页面的url地址"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档