动态生成柱状图

80酷酷网    80kuku.com

  动态|柱状图前几天在网上看了一个老外的生成图片的方法,
感觉非常实用,不敢独享,下面是我整理过的部分源码,
如果需要,给我发邮件地址(xwoceansoft.com.cn)。

ChartIndex.aspx

<% Page language="c#" Codebehind="ChartIndex.aspx.cs" AutoEventWireup="false" Inherits="TT_W.ChartIndex" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <FONT face="宋体">
                <TABLE id="Table1" 101; LEFT: 23px; POSITION: absolute; TOP: 22px" cellSpacing="1" cellPadding="1" width="300" border="0">
                    <TR>
                        <TD>
                            <asp:Label id="Label1" runat="server">红色</asp:Label></TD>
                        <TD>
                            <asp:TextBox id="t1" runat="server"></asp:TextBox></TD>
                        <TD></TD>
                    </TR>
                    <TR>
                        <TD>
                            <asp:Label id="Label2" runat="server">兰色</asp:Label></TD>
                        <TD>
                            <asp:TextBox id="t2" runat="server"></asp:TextBox></TD>
                        <TD></TD>
                    </TR>
                    <TR>
                        <TD>
                            <asp:Label id="Label3" runat="server">黄色</asp:Label></TD>
                        <TD>
                            <asp:TextBox id="t3" runat="server"></asp:TextBox></TD>
                        <TD></TD>
                    </TR>
                    <TR>
                        <TD></TD>
                        <TD>
                            <asp:Button id="Button1" runat="server" Text="显示例图" Width="75px"></asp:Button></TD>
                        <TD></TD>
                    </TR>
                    <TR>
                        <TD vAlign="top" align="left" colSpan="3">
                            <img src="ShowImage.aspx?V1=<%=m_t1%>&V2=<%=m_t2%>&V3=<%=m_t3%>" width=200 height=200></TD>
                    </TR>
                </TABLE>
            </FONT>
        </form>
    </body>
</HTML>

ChartIndex.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TT_W
{
    /// <summary>
    /// WebForm1 的摘要说明。
    /// </summary>
    public class ChartIndex : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.TextBox t1;
        protected System.Web.UI.WebControls.TextBox t2;
        protected System.Web.UI.WebControls.TextBox t3;
        protected System.Web.UI.WebControls.Label Label3;
        protected System.Web.UI.WebControls.Button Button1;

        public Single m_t1;
        public Single m_t2;
        public Single m_t3;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(t1.Text!="")
            {
                m_t1 = Convert.ToSingle(t1.Text);
            }
            if(t2.Text!="")
            {
                m_t2 = Convert.ToSingle(t2.Text);
            }
            if(t3.Text!="")
            {
                m_t3 = Convert.ToSingle(t3.Text);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}

ShowImage.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TT_W
{
    /// <summary>
    /// ShowImage 的摘要说明。
    /// </summary>
    public class ShowImage : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(Request.QueryString["V1"] != null && Request.QueryString["V1"] != "")
            {
                Bitmap m_bmp = new Bitmap(200,200);
                Graphics m_gp = Graphics.FromImage(m_bmp);
                SolidBrush redbrush = new SolidBrush(Color.Red);
                SolidBrush yellowbrush = new SolidBrush(Color.Yellow);
                SolidBrush bluebrush = new SolidBrush(Color.Blue);
                SolidBrush whitebrush = new SolidBrush(Color.White);
                Pen blackpen = new Pen(Color.Black,2);
            
                m_gp.FillRectangle(whitebrush,0,0,200,200);

                m_gp.DrawLine(blackpen,new Point(0,195),new Point(195,195));
                m_gp.DrawLine(blackpen,new Point(5,5),new Point(5,200));

                Single m_tt = 1;
                Single m_t1 = Convert.ToSingle(Request.QueryString["V1"]);
                Single m_t2 = Convert.ToSingle(Request.QueryString["V2"]);
                Single m_t3 = Convert.ToSingle(Request.QueryString["V3"]);
                Single m_h1 = 1;
                Single m_h2 = 1;
                Single m_h3 = 1;
            
                if(m_t1>m_t2)
                {
                    m_tt = m_t1;
                }
                else
                {
                    m_tt = m_t2;
                }
                if(m_tt>m_t3)
                {}
                else
                {
                    m_tt = m_t3;
                }
                if(m_tt == 0)
                {
                    m_tt =1;
                }

                m_h1 = (m_t1/m_tt) * 190;
                m_h2 = (m_t2/m_tt) * 190;
                m_h3 = (m_t3/m_tt) * 190;

                m_gp.FillRectangle(redbrush,10,194-m_h1,50,m_h1);
                m_gp.FillRectangle(yellowbrush,70,194-m_h2,50,m_h2);
                m_gp.FillRectangle(bluebrush,130,194-m_h3,50,m_h3);

                Response.ContentType = "image/gif";

                m_bmp.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}

根据它大家还可以试一试生成饼状图。 ^o^

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: