当前位置:编程学习 > C#/ASP.NET >>

求优化

using System; 
using System.IO;//用于文件存取 
using System.Data;//用于数据访问 
using System.Drawing;//提供画GDI+图形的基本功能 
using System.Drawing.Text;//提供画GDI+图形的高级功能 
using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能 
using System.Drawing.Imaging;//提供画GDI+图形的高级功能 
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.UI;

namespace Lzq_The_Image
{
/// <summary>
/// The_Curve 的摘要说明。
/// </summary>
public class The_Curve : Page
{
protected The_Select_Color tsc=new The_Select_Color();
public The_Curve()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

private float repleace(string number)
{

return Convert.ToSingle(Convert.ToSingle(number).ToString("F2"));
}

private float repleace(float number)
{
return Convert.ToSingle(number.ToString("F2"));
}

public void Make_Curve(string title,System.Data.DataSet tmds,string[] legendname,float devicemaxvalue,float Tittle_Image_Width,bool line,bool num)
{
//定义画布整体高度,曲线范围位置,宽度.
const float CHART_Height=390;
const float CHART_xnow=4;
//定义画布整体宽度,曲线范围高度.
float CHART_Width=0;
int Max_DataSet_Row_Count=0;
int First_Table=0;
//查找最大数据个数
for(int z=0;z<tmds.Tables.Count;z++)
{
if(tmds.Tables[z].Rows.Count>Max_DataSet_Row_Count)
{
Max_DataSet_Row_Count=tmds.Tables[z].Rows.Count;
First_Table=z;
}
}
this.Drawing_TittleAndCutline(Tittle_Image_Width,legendname,title);
//动态设置画布总体宽度.
if(Max_DataSet_Row_Count>20)
{
CHART_Width=Max_DataSet_Row_Count*40F;
CHART_Width+=140F;
}
else
{
CHART_Width=510;
}
//创建画布
Bitmap bm = new Bitmap(Convert.ToInt32(CHART_Width),Convert.ToInt32(CHART_Height+50F));
//创建绘图实例
Graphics g = Graphics.FromImage(bm);
//设置图象呈现质量和模
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
//填充背景
g.Clear(System.Drawing.Color.White);
//绘制曲线范围边框
g.DrawRectangle(new Pen(Brushes.Black,4F),71F,1F,CHART_Width-140F-1,CHART_Height-1);
//填充曲线范围背景
System.Drawing.Rectangle re=new Rectangle(71,1,Convert.ToInt32(CHART_Width-140-1),Convert.ToInt32(CHART_Height-1));
LinearGradientBrush lgb=new LinearGradientBrush(re,System.Drawing.Color.FromArgb(255,255,255),System.Drawing.Color.FromArgb(129,241,254),-90F,false);
g.FillRectangle(lgb,71F,1F,CHART_Width-140F-1,CHART_Height-1);
//定义数据总量
int TablesCount=tmds.Tables.Count;
//定义 横纵坐标值
float numberfory=0;
float numberforx=0;
this.Drawing_X_Value(50F,CHART_Height,devicemaxvalue,CHART_xnow,CHART_Height);
float value_x=0;
float[] dashValues = { 5, 5, 5, 5 };
Pen blackPen = new Pen(Color.Black,0.02F);
blackPen.DashPattern = dashValues;
if(line)
{
//绘制11个X轴坐标值(可调整)
for(int ynow=0;ynow<11;ynow++)
{
//计算X轴位置
value_x=repleace(devicemaxvalue/10F*ynow);
g.DrawLine(blackPen,71F,CHART_Height-(CHART_Height/10*ynow),CHART_Width-70F,CHART_Height-(CHART_Height/10*ynow));
}
}
//绘制横向坐标轴
for(int c=0;c<Max_DataSet_Row_Count;c++)
{
if(c%2!=0)
{
g.DrawString(tmds.Tables[First_Table].Rows[c][1].ToString(),new Font("Tahoma",7),Brushes.Black,new PointF(71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c)-25F,CHART_Height+10));
if(line)
{
g.DrawLine(blackPen,71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c),CHART_Height,71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c),1F);
}
}
else
{
g.DrawString(tmds.Tables[First_Table].Rows[c][1].ToString(),new Font("Tahoma",7),Brushes.Black,new PointF(71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c)-25F,CHART_Height+25));
if(line)
{
g.DrawLine(blackPen,71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c),CHART_Height,71F+repleace((CHART_Width-140F)/(tmds.Tables[First_Table].Rows.Count-1)*c),1F);
}

}
}
//循环绘制曲线
for(int a=0;a<TablesCount;a++)//---根据数据集生成多条曲线
{
if(tmds.Tables[a].Rows.Count>1)//---如果数据集中数据个数多余1个才执行绘制.(数据集个数必须>=2)
{
//定义曲线坐标总数值
int pointnum=tmds.Tables[a].Rows.Count;
//定义曲线坐标数组
PointF[] pt=new PointF[pointnum];
//循环生成当前数据集曲线所有坐标
for(int b=0;b<tmds.Tables[a].Rows.Count;b++)
{
numberforx=(CHART_Width-140F)/(tmds.Tables[a].Rows.Count-1);
numberfory=(repleace(tmds.Tables[a].Rows[b][0].ToString())/devicemaxvalue)*CHART_Height;
pt[b].X=71F+repleace(numberforx*b);
pt[b].Y=CHART_Height-(numberfory);
}
//根据坐标数组绘制曲线
g.DrawCurve(new Pen(tsc.GetChartItemColor(a),2.0F),pt,0.3F);
float num_x=0;
float num_y=0;
float num_width=0;
float num_height=0;
if(num)
{
for(int h=0;h<pt.Length;h++)
{
num_height=18F;
if(devicemaxvalue>10000)
{
num_width=Convert.ToString(Convert.ToSingle(Convert.ToSingle(tmds.Tables[a].Rows[h][0].ToString())/10000).ToString("F2").Replace(".00","")+"万").Length*8F+4F;
}
else
{
num_width=Convert.ToString(Convert.ToSingle(Convert.ToSingle(tmds.Tables[a].Rows[h][0].ToString())).ToString("F2").Replace(".00","")).Length*8F+4F;
}
                            num_x=pt[h].X-(num_width/2);
num_y=pt[h].Y-num_height-3F;
if(num_y<20F)
{
num_y=20F;
}
g.DrawRectangle(new Pen(System.Drawing.Color.Black),num_x,num_y,num_width,num_height);
g.FillRectangle(Brushes.LightGoldenrodYellow,num_x,num_y,num_width,num_height);
if(devicemaxvalue>10000)
{
g.DrawString(Convert.ToSingle(Convert.ToSingle(tmds.Tables[a].Rows[h][0].ToString())/10000).ToString("F2").Replace(".00","")+"万",new Font("Tahoma",10),Brushes.Black,num_x+2F,num_y+2F);
}
else
{
g.DrawString(Convert.ToSingle(Convert.ToSingle(tmds.Tables[a].Rows[h][0].ToString())).ToString("F2").Replace(".00",""),new Font("Tahoma",10),Brushes.Black,num_x+2F,num_y+2F);
}
}
}
}
else
{

}
}
//判断此项曲线图是否已经存在图象实例
if(File.Exists(Page.Server.MapPath("../TheCurve.gif")))
{
File.Delete(Page.Server.MapPath("../TheCurve.gif"));//如果存在将旧实例删除
}
//将绘图实例保存为GIF格式的图象文件
bm.Save(Page.Server.MapPath("../TheCurve.gif"),ImageFormat.Gif);
//释放绘图资源
bm.Dispose(); 
g.Dispose(); 
}

private void Drawing_X_Value(float width,float height,float devicemaxvalue,float CHART_xnow,float CHART_Height)
{
float TAC_Backgroud_Width=width;
float TAC_Backgroud_Height=height;
//创建画布
Bitmap bm = new Bitmap(Convert.ToInt32(TAC_Backgroud_Width),Convert.ToInt32(TAC_Backgroud_Height+10F));
//创建绘图实例
Graphics g = Graphics.FromImage(bm);
//设置图象呈现质量和模式
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
//填充背景
g.Clear(System.Drawing.Color.White);
//绘制纵向坐标轴
//定义运算用X轴变量
float value_x=0;
//绘制11个X轴坐标值(可调整)
for(int ynow=0;ynow<11;ynow++)
{
//计算X轴位置
if(devicemaxvalue>10000)
{
value_x=repleace(devicemaxvalue/10000/10F*ynow);
g.DrawString(value_x.ToString("F0")+"万",new Font("Tahoma",7),Brushes.Black,new PointF(width-(value_x.ToString("F0").Length*6F+10F)-1F,CHART_Height-(CHART_Height/10*ynow)));
}
else
{
value_x=repleace(devicemaxvalue/10F*ynow);
g.DrawString(value_x.ToString("F0")+"%",new Font("Tahoma",7),Brushes.Black,new PointF(width-(value_x.ToString("F0").Length*6F+10F)-1F,CHART_Height-(CHART_Height/10*ynow)));
}
//绘制X轴值

}
//判断此项曲线图是否已经存在图象实例
if(File.Exists(Page.Server.MapPath("../TheCurve_X_Value.gif")))
{
File.Delete(Page.Server.MapPath("../TheCurve_X_Value.gif"));//如果存在将旧实例删除
}
//将绘图实例保存为GIF格式的图象文件
bm.Save(Page.Server.MapPath("../TheCurve_X_Value.gif"),ImageFormat.Gif);
//释放绘图资源
bm.Dispose(); 
g.Dispose(); 
}

private void Drawing_TittleAndCutline(float width,string[] legendname,string title)
{
float TAC_Backgroud_Width=width;
float TAC_Backgroud_Height=50F;
//创建画布
Bitmap bm = new Bitmap(Convert.ToInt32(TAC_Backgroud_Width),Convert.ToInt32(TAC_Backgroud_Height));
//创建绘图实例
Graphics g = Graphics.FromImage(bm);
//设置图象呈现质量和模式
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
//填充背景
g.Clear(System.Drawing.Color.White);
//绘制主标题
g.DrawString(title,new Font("Tahoma",12),Brushes.Black,new PointF(40,10));
//绘制图例
//定义首个图例颜色框
Point boxOrigin = new Point(740,22);
//定义首个图例文本
Point textOrigin = new Point(710,9);
//根据图例个数进行绘制
for(int i=0;i<legendname.Length;i++)
{
if(legendname[i].ToString()!="")
{
//画框
g.DrawRectangle(Pens.Black,boxOrigin.X,boxOrigin.Y,20,10);
//填充刚画的框
g.FillRectangle(new SolidBrush(tsc.GetChartItemColor(i)),boxOrigin.X,boxOrigin.Y,20,10);
if(i%2!=0)
{
//画字
g.DrawString(legendname[i].ToString(),new Font("Tahoma",8),Brushes.Black,new Point(textOrigin.X,textOrigin.Y+26));
}
else
{
g.DrawString(legendname[i].ToString(),new Font("Tahoma",8),Brushes.Black,new Point(textOrigin.X,textOrigin.Y));
}
//初始坐标 横向移动
boxOrigin.X -= 60;
textOrigin.X -= 60;
}
}

//判断此项曲线图是否已经存在图象实例
if(File.Exists(Page.Server.MapPath("../TheCurve_Tittle.gif")))
{
File.Delete(Page.Server.MapPath("../TheCurve_Tittle.gif"));//如果存在将旧实例删除
}
//将绘图实例保存为GIF格式的图象文件
bm.Save(Page.Server.MapPath("../TheCurve_Tittle.gif"),ImageFormat.Gif);
//释放绘图资源
bm.Dispose(); 
g.Dispose(); 
}
}
}
--------------------编程问答-------------------- using System; 
using System.IO;//用于文件存取 
using System.Data;//用于数据访问 
using System.Drawing;//提供画GDI+图形的基本功能 
using System.Drawing.Text;//提供画GDI+图形的高级功能 
using System.Drawing.Drawing2D;//提供画高级二维,矢量图形功能 
using System.Drawing.Imaging;//提供画GDI+图形的高级功能 
using System.Collections;
using System.ComponentModel;

namespace Lzq_The_Image
{
/// <summary>
/// The_Select_Color 的摘要说明。
/// </summary>
public class The_Select_Color
{
public The_Select_Color()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public System.Drawing.Color GetChartItemColor(int itemIndex)
{
System.Drawing.Color selectedColor;
switch(itemIndex)
{
case 0:
selectedColor = Color.Blue; 
break;
case 1:
selectedColor = Color.Red;
break;
case 2:
selectedColor = Color.Goldenrod;
break;
case 3:
selectedColor = Color.Purple;
break;
case 4:
selectedColor = Color.Chocolate;
break;
case 5:
selectedColor = Color.CadetBlue;
break;
case 6:
selectedColor = Color.GreenYellow;
break;
case 7:
selectedColor = Color.Yellow;
break;
case 8:
selectedColor = Color.Honeydew;
break;
case 9:
selectedColor = Color.HotPink;
break;
case 10:
selectedColor = Color.IndianRed;
break;
case 11:
selectedColor = Color.Indigo;
break;
case 12:
selectedColor = Color.Ivory;
break;
default:
selectedColor = Color.Khaki;
break;
}
return selectedColor;
}
}
}
--------------------编程问答-------------------- 没看出好优化的地方。
因为没看懂。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,