动态生成柱状图
2024-07-21 02:21:59
供稿:网友
前几天在网上看了一个老外的生成图片的方法,
感觉非常实用,不敢独享,下面是我整理过的部分源码,
如果需要,给我发邮件地址([email protected])。
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" style="z-index: 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^国内最大的酷站演示中心!