asp.net中怎么生成饼状与柱状图-创新互联
本篇内容介绍了“asp.net中怎么生成饼状与柱状图”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联成立十多年来,这条路我们正越走越好,积累了技术与客户资源,形成了良好的口碑。为客户提供成都网站制作、成都网站建设、网站策划、网页设计、主机域名、网络营销、VI设计、网站改版、漏洞修补等服务。网站是否美观、功能强大、用户体验好、性价比高、打开快等等,这些对于网站建设都非常重要,创新互联通过对建站技术性的掌握、对创意设计的研究为客户提供一站式互联网解决方案,携手广大客户,共同发展进步。一、生成图形的公共方法:
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Text;
//
//using System.Data;
//using System.Web.UI.WebControls;
//
using System.Drawing;
using System.Drawing.Imaging;
namespace Tools
{
public static class OWCImageHelp
{
///
/// 动态的生成柱状图和饼状图
///
/// 行坐标要显示的字段
/// 纵坐标要显示的数字
/// 标题
public static void GetZBImage(string[] arrValueNames, int[] arrValues, string title)
{
Bitmap objBitMap = new Bitmap(650, 300);
Graphics objGraphics;
objGraphics = Graphics.FromImage(objBitMap);
objGraphics.Clear(Color.White);
//int[] arrValues = { 40000, 32000, 24000, 30000, 36000, 28000 };
//string[] arrValueNames = new string[] { "第一次", "第二次", "第三次", "第四次", "第五次", "第六次" };
objGraphics.DrawString(title, new System.Drawing.Font("宋体", 16), Brushes.Blue, new PointF(5, 5));
PointF symbolLeg = new PointF(335, 20);
PointF descLeg = new PointF(360, 16);
//画出说明部分的图形
for (int i = 0; i < arrValueNames.Length; i++)
{
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), symbolLeg.X, symbolLeg.Y, 20, 10);
objGraphics.DrawRectangle(Pens.Black, symbolLeg.X, symbolLeg.Y, 20, 10);
objGraphics.DrawString(arrValueNames[i].ToString(), new System.Drawing.Font("宋体", 10), Brushes.Black, descLeg);
symbolLeg.Y += 15;
descLeg.Y += 15;
}
float TotalValues = 0;
for (int i = 0; i <= arrValues.Length - 1; i++)
{
TotalValues += arrValues[i];
}
//绘出矩形图。
float Rectangleheight = 0;
PointF recLeg = new PointF(12, 200 - arrValues[0] / TotalValues * 300);
for (int i = 0; i < arrValues.Length; i++)
{
Rectangleheight = arrValues[i] / TotalValues * 300;
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (i * 35) + 15, 200 - Rectangleheight, 20, Rectangleheight + 50);
objGraphics.DrawRectangle(Pens.Black, (i * 35) + 15, 200 - Rectangleheight, 20, Rectangleheight + 50);
recLeg.Y = 200 - Rectangleheight - 14;
objGraphics.DrawString(arrValues[i].ToString(), new System.Drawing.Font("宋体", 10), Brushes.Blue, recLeg);
recLeg.X += 35;
}
//绘出圆形图。
float sglCurrentAngle = 0;
float sglTotalAngle = 0;
for (int i = 0; i < arrValues.Length; i++)
{
sglCurrentAngle = arrValues[i] / TotalValues * 360;
objGraphics.FillPie(new SolidBrush(GetColor(i)), 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
objGraphics.DrawPie(Pens.Black, 220, 95, 100, 100, sglTotalAngle, sglCurrentAngle);
sglTotalAngle += sglCurrentAngle;
}
objBitMap.Save(System.Web.HttpContext.Current.Response.OutputStream, ImageFormat.Gif);
}
//定义颜色。
private static Color GetColor(int itemIndex)
{
Color objColor;
if (itemIndex == 0)
{
objColor = Color.Maroon;
}
else if (itemIndex == 1)
{
objColor = Color.Red;
}
else if (itemIndex == 2)
{
objColor = Color.Gray;
}
else if (itemIndex == 3)
{
objColor = Color.Blue;
}
else if (itemIndex == 4)
{
objColor = Color.Orange;
}
else if (itemIndex == 5)
{
objColor = Color.Cyan;
}
else if (itemIndex == 6)
{
objColor = Color.Bisque;
}
else if (itemIndex == 7)
{
objColor = Color.Maroon;
}
else if (itemIndex == 8)
{
objColor = Color.Maroon;
}
else
{
objColor = Color.Blue;
}
return objColor;
}
}
}
二、新建生成饼状柱状图页面BZImage.aspx:
后台:
复制代码 代码如下:
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 BLL;
using Models;
public partial class GridViewDemo_BZImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetBZIamge();
}
///
/// 生成饼状柱状图
///
public void GetBZIamge()
{
DataTable dt = BLL.StudentBLL.SelAllStudent();
string[] rows = new string[dt.Rows.Count];
int[] columns = new int[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
rows[i] = dt.Rows[i]["学生姓名"].ToString();
columns[i] = Convert.ToInt32(dt.Rows[i]["薪金"].ToString());
}
Tools.OWCImageHelp.GetZBImage(rows, columns, "学生薪水查询");
}
}
三、显示饼状柱状图的页面:
前台:
复制代码 代码如下:
“asp.net中怎么生成饼状与柱状图”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
网站题目:asp.net中怎么生成饼状与柱状图-创新互联
当前地址:http://ybzwz.com/article/dpdhcs.html