using
System;
using
System.Data;
using
System.Configuration;
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
System.Drawing;
using
System.Drawing.Drawing2D;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
qyjz
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
public class DrawHelper
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///渐变颜色的函数
/// </summary>
/// <returns>LinearGradientBrush 对象</returns>
private LinearGradientBrush GetColor(int ylong)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
LinearGradientBrush linGrBrush = new LinearGradientBrush(
new Point(0, 0),
new Point(0, ylong),
Color.FromArgb(255, 255, 0, 0), // Opaque red
Color.FromArgb(255, 0, 0, 255));
return linGrBrush;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 刻度线生成函数
/// </summary>
/// <param name="objGraphics">Graphics对象</param>
/// <param name="intxLong">图像x大小</param>
/// <param name="intyLong">图像y大小</param>
/// <param name="intxLeft">图像左边巨</param>
/// <param name="intEnd">图像下边距</param>
/// <param name="intyMax">刻度数</param>
/// <param name="kdvalue">刻度值</param>
/// <returns></returns>
public Graphics DwLine(Graphics objGraphics, int intxLong, int intyLong, int intxLeft, int intEnd, int intyMax, float kdvalue, string title)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
objGraphics.Clear(Color.White);
int intyScale = intyLong / intyMax;
objGraphics.DrawString(title, new Font("宋体", 9), Brushes.Black, new PointF(5 + intxLeft, 5));
Point p1 = new Point(intxLeft - 10, intyLong);
Pen p = new Pen(Color.Silver);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
for (int i = 0; i < intyMax; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
p1.Y = intyLong - i * intyScale;
Point pt = new Point(p1.X + 10, p1.Y);
objGraphics.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));//刻度线y轴
objGraphics.DrawLine(p, intxLeft, intyLong - intEnd - i * intyScale, intxLong, intyLong - intEnd - i * intyScale);
objGraphics.DrawString(Convert.ToString(kdvalue * i), new Font("宋体", 9), Brushes.Black, new Point(p1.X - 20, p1.Y - 30));
}
objGraphics.DrawLine(new Pen(Color.Black, 2), intxLeft, intyLong - intEnd, intxLeft, 0);//绘制y轴
objGraphics.DrawLine(new Pen(Color.Black, 2), intxLeft, intyLong - intEnd, intxLong, intyLong - intEnd);//绘制x轴
return objGraphics;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 生成矩形图
/// </summary>
/// <param name="objGraphics">Graphics 对象</param>
/// <param name="intxLong">图像x大小</param>
/// <param name="intyLong">图像y大小</param>
/// <param name="intxLeft">图像左边距</param>
/// <param name="intEnd">图像下边距</param>
/// <param name="intyMax">刻度数</param>
/// <param name="kdvalue">刻度值</param>
/// <param name="title">图片显示名</param>
/// <param name="mydt">DataTable的值</param>
/// <returns>Graphics</returns>
public Graphics DwMonthjxt(Graphics objGraphics, int intxLong, int intyLong, int intxLeft, int intEnd, int intyMax, float kdvalue, DataTable mydt, int jdwidth)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int intyScale = intyLong / intyMax;
float jx = Convert.ToInt32(jdwidth * 0.618);
for (int i = 0; i < mydt.Rows.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
float values = Convert.ToInt32(mydt.Rows[i]["co"].ToString());
if (values != 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
objGraphics.FillRectangle(GetColor(intyLong), (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd, jx, values / kdvalue * intyScale);//画矩形内部图案
objGraphics.DrawRectangle(Pens.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd, jx, values / kdvalue * intyScale);//矩形外部图案
objGraphics.DrawString(mydt.Rows[i]["co"].ToString(), new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 15);//矩形上的数值
objGraphics.DrawString(mydt.Rows[i]["months"].ToString() + "月", new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - intEnd + 3);//x轴下的文字
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
objGraphics.FillRectangle(GetColor(intyLong), (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 2, jx, values / kdvalue * intyScale + 2);//画矩形内部图案
objGraphics.DrawRectangle(Pens.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 2, jx, values / kdvalue * intyScale + 2);//矩形外部图案
objGraphics.DrawString(mydt.Rows[i]["co"].ToString(), new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 15);//矩形上的数值
objGraphics.DrawString(mydt.Rows[i]["months"].ToString() + "月", new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - intEnd + 3);//x轴下的文字
}
}
return objGraphics;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 生成矩形图
/// </summary>
/// <param name="objGraphics">Graphics 对象</param>
/// <param name="intxLong">图像x大小</param>
/// <param name="intyLong">图像y大小</param>
/// <param name="intxLeft">图像左边距</param>
/// <param name="intEnd">图像下边距</param>
/// <param name="intyMax">刻度数</param>
/// <param name="kdvalue">刻度值</param>
/// <param name="title">图片显示名</param>
/// <param name="mydt">DataTable的值</param>
/// <returns>Graphics</returns>
public Graphics DwHourjxt(Graphics objGraphics, int intxLong, int intyLong, int intxLeft, int intEnd, int intyMax, float kdvalue, DataTable mydt, int jdwidth)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int intyScale = intyLong / intyMax;
float jx = Convert.ToInt32(jdwidth * 0.618);
for (int i = 0; i < mydt.Rows.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
float values = Convert.ToInt32(mydt.Rows[i]["co"].ToString());
if (values != 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
objGraphics.FillRectangle(GetColor(intyLong), (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd, jx, values / kdvalue * intyScale);//画矩形内部图案
objGraphics.DrawRectangle(Pens.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd, jx, values / kdvalue * intyScale);//矩形外部图案
objGraphics.DrawString(mydt.Rows[i]["co"].ToString(), new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 15);//矩形上的数值
objGraphics.DrawString(mydt.Rows[i]["Hours"].ToString() + "时", new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - intEnd + 3);//x轴下的文字
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
objGraphics.FillRectangle(GetColor(intyLong), (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 2, jx, values / kdvalue * intyScale + 2);//画矩形内部图案
objGraphics.DrawRectangle(Pens.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 2, jx, values / kdvalue * intyScale + 2);//矩形外部图案
objGraphics.DrawString(mydt.Rows[i]["co"].ToString(), new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - values / kdvalue * intyScale - intEnd - 15);//矩形上的数值
objGraphics.DrawString(mydt.Rows[i]["Hours"].ToString() + "时", new Font("宋体", 9), Brushes.Black, (i * jdwidth) + intxLeft + 20, intyLong - intEnd + 3);//x轴下的文字
}
}
return objGraphics;
}
}
}
参考了前辈们的生成柱状图代码,自己加以改进的,基本上都有注释,新建一个类以后改改就好了.