您好,登錄后才能下訂單哦!
本篇內容介紹了“asp.net中怎么生成餅狀與柱狀圖”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一、生成圖形的公共方法:
復制代碼 代碼如下:
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
{
/// <summary>
/// 動態的生成柱狀圖和餅狀圖
/// </summary>
/// <param name="arrValueNames">行坐標要顯示的字段</param>
/// <param name="arrValues">縱坐標要顯示的數字</param>
/// <param name="title">標題</param>
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();
}
/// <summary>
/// 生成餅狀柱狀圖
/// </summary>
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, "學生薪水查詢");
}
}
三、顯示餅狀柱狀圖的頁面:
前臺:
復制代碼 代碼如下:
<table onMouseOver="over()" onMouseOut="out()">
<tr>
<td align="center">
<img id="BZImage" src="BZImage.aspx" alt=""/>
</td>
</tr>
</table>
“asp.net中怎么生成餅狀與柱狀圖”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。