用法:Commlib.Bar bar2d = new Commlib.Bar(); //定义一个画图类实例
bar2d.Title = Logic.BLL.T_YSKZ_BAS_ORG.GetOrgName(unit_code)+"本年预算情况";
bar2d.Unit = "元";
bar2d.Keys = new string[] {"a","b","c" };
bar2d.Values = items;
Bitmap bmp = bar2d.CreateImage();
bmp.Save(Response.OutputStream, ImageFormat.Gif);
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace Commlib
{
/// <summary>
/// Bar 的摘要说明
/// </summary>
public class Bar
{
private Graphics objGraphics; //Graphics 类提供将对象绘制到显示设备的方法
private Bitmap objBitmap; //位图对象
private int m_Width = 800; //图像宽度
private int m_Height = 400; //图像高度
private string m_Title = ""; //标题
private string m_Unit = ""; //单位
//键
private string[] m_Keys;
//值
private double[] m_Values;
private Color m_BgColor = Color.Snow; //背景
private Color m_TextColor = Color.Black; //文字颜色
private Color m_BorderColor = Color.Black; //整体边框颜色
//Bar颜色数组
private Color[] m_BarColor = new Color[] { Color.Green, Color.Red, Color.Yellow };
//, Color.Blue, Color.Orange, Color.Aqua, Color.SkyBlue, Color.DeepPink, Color.Azure, Color.Brown
public int Width
{
set
{
if (value < 300)
{
m_Width = 300;
}
else
{
m_Width = value;
}
}
get
{
return m_Width;
}
}
public int Height
{
set
{
if (value < 300)
{
m_Height = 300;
}
else
{
m_Height = value;
}
}
get
{
return m_Height;
}
}
public string Title
{
set
{
m_Title = value;
}
get
{
return m_Title;
}
}
public string Unit
{
set
{
m_Unit = value;
}
get
{
return m_Unit;
}
}
public string[] Keys
{
set
{
m_Keys = value;
}
get
{
return m_Keys;
}
}
public double[] Values
{
set
{
m_Values = value;
}
get
{
return m_Values;
}
}
public Color BgColor
{
set
{
m_BgColor = value;
}
get
{
return m_BgColor;
}
}
public Color TextColor
{
set
{
m_TextColor = value;
}
get
{
return m_TextColor;
}
}
public Color BorderColor
{
set
{
m_BorderColor = value;
}
get
{
return m_BorderColor;
}
}
public Color[] BarColor
{
set
{
m_BarColor = value;
}
get
{
return m_BarColor;
}
}
private Color GetColor(int i) //分配各Bar的颜色
{
try
{
i = i % 3;
if (0 < i && i < BarColor.Length)
{
return BarColor[i];
}
else if (i >= BarColor.Length)
{
int j = i % BarColor.Length;
return BarColor[j];
}
else
{
return Color.Green;
}
}
catch
{
return Color.Green;
}
}
//生成图像并返回bmp图像对象
public Bitmap CreateImage()
{
InitializeGraph();
DrawRight(ref objGraphics);
DrawContent(ref objGraphics);
return objBitmap;
}
//初始化和填充图像区域,画出边框,初始标题
private void InitializeGraph()
{
//根据给定的高度和宽度创建一个位图图像
objBitmap = new Bitmap(Width, Height);
//从指定的 objBitmap 对象创建 objGraphics 对象 (即在objBitmap对象中画图)
objGraphics = Graphics.FromImage(objBitmap);
//根据给定颜色(LightGray)填充图像的矩形区域 (背景)
objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width, Height);
objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2);
//初始化标题
CreateTitle(ref objGraphics);
}
//初始化邮编说明部分
private void DrawRight(ref Graphics objGraphics)
{
objGraphics.DrawString(String.Format("单位:{0}", Unit), new Font("宋体", 10), new SolidBrush(TextColor), Width - 100, 30);
Point KeysPoint = new Point(Width - 95, 50);
Point KeysTextPoint = new Point(Width - 70, 50);
for (int i = 0; i < Keys.Length; i++)
{
objGraphics.DrawRectangle(new Pen(BorderColor, 1), KeysPoint.X, KeysPoint.Y, 21, 11);
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), KeysPoint.X, KeysPoint.Y, 20, 10);
objGraphics.DrawString(Keys[i], new Font("宋体", 10), new SolidBrush(TextColor), KeysTextPoint);
KeysPoint.Y += 15;
KeysTextPoint.Y += 15;
}
}
//Bar图的主要部分
private void DrawContent(ref Graphics objGraphics)
{
Point KeysPoint = new Point(20, Height - 20);
Point KeysTextPoint = new Point(20, Height - 20);
int SpaceWidth = (Width - 120) / (Values.Length * 2);
double SpaceHeight;
//画Bar
for (int i = 0; i < Values.Length; i++)
{
SpaceHeight = (Height - 100) * (Values[i] / MaxValues(Values));
objGraphics.DrawRectangle(new Pen(BorderColor, 1), (int)(KeysPoint.X), (int)(KeysPoint.Y - SpaceHeight), (int)(SpaceWidth + 1), (int)(SpaceHeight + 1));
objGraphics.FillRectangle(new SolidBrush(GetColor(i)), (int)(KeysPoint.X), (int)(KeysPoint.Y - SpaceHeight), (int)SpaceWidth, (int)SpaceHeight);
KeysPoint.X += SpaceWidth * 2;
}
//画Bar上的数值,不和上边同时进行操作是因为Bar柱有可能覆盖了数值的显示,所以要分开
for (int i = 0; i < Values.Length; i++)
{
SpaceHeight = (Height - 100) * (Values[i] / MaxValues(Values));
KeysTextPoint.Y = (int)(KeysPoint.Y - SpaceHeight - 20);
objGraphics.DrawString(Values[i].ToString(), new Font("宋体", 10), new SolidBrush(TextColor), KeysTextPoint);
KeysTextPoint.X += SpaceWidth * 2;
}
}
//初始化标题
private void CreateTitle(ref Graphics objGraphics)
{
objGraphics.DrawString(Title, new Font("宋体", 16), new SolidBrush(TextColor), new Point(5, 5));
}
//获取数组的最大值
private double MaxValues(double[] Values)
{
//int[] SortValues = Values;
return Values[0] > Values[1] ? Values[0] : Values[1];
}
public Bar()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}