


















chart.cs
using System;
using System.Drawing;namespace Report
{
/// <summary>
/// Chart 的摘要说明。
/// ==================================================================================================
///
/// ClassName :Report.Chart
/// Intro :
/// Example :
/// Ver :0.2
///
/// Author :ttyp
/// Email :ttyp@21cn.com
/// Date :2007-7-30
/// ==================================================================================================
/// </summary>
public class Chart
{
public Chart(){}private string _data = "";
private int _width = 100;
private int _height = 100;
private int _padding= 8;
private Color _grid_color = Color.FromArgb(0x93,0xbe,0xe2);
private Color _border_color = Color.FromArgb(0x93,0xbe,0xe2);
private Font _font = new Font("Arial",8);public Font Font
{
get { return _font;}
set { _font = value;}
}public Color BorderColor
{
get { return _border_color;}
set { _border_color = value;}
}public Color GridColor
{
get { return _grid_color;}
set { _grid_color = value;}
}public int Padding
{
get { return _padding;}
set { _padding = Math.Max(0,value);}
}public int Width
{
get { return _width;}
set { _width = Math.Max(0,value);}
}public int Height
{
get { return _height;}
set { _height = Math.Max(0,value);}
}public string Data
{
get { return _data;}
set { _data = value;}
}public void Render()
{
int width = this.Width;
int height = this.Height;
int padding = this.Padding;
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width,height);
Graphics g = Graphics.FromImage(image);//清空图片背景色
g.Clear(Color.White);//虚线画笔
Pen dot = new Pen(this.GridColor);
dot.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//实线画笔
Pen solid = new Pen(this.BorderColor);//文字字体
Font font = this.Font;
try
{ //冗余,去除最后的数据分割标记,防止空数据
if(this.Data.EndsWith(";"))
{
this.Data = this.Data.Substring(0,this.Data.Length-1);
}string[] info = this.Data.Split(';'); //数据信息
if(info.Length>=2)
{
string[] lines = info[0].Split(','); //图例
string[] units = info[1].Split(','); //单位和标题格式,a,b,c,d a 纵坐标单位 b 纵坐标格式 N 数字 D 时间 后面是具体格式,c 横坐标单位 d 横坐标格式(同b)//曲线颜色表
Color[] color = new Color[]{Color.Blue,Color.Green,Color.Red,Color.Gray,Color.Black,Color.Magenta,Color.Cyan,Color.Yellow,Color.DeepPink,Color.BurlyWood,Color.DarkRed,Color.Gold};//图例文字的大小
SizeF sFont = GetMaxSize(lines,g,font);//获得刻度文字高度
int textHeight = (int)(sFont.Height*3/2);//曲线点的个数
int points = info.Length-2;//得到曲线点数组集合
string[,] curve = new string[info.Length-2,lines.Length+1];
for(int i=0;i<points;i++)
{
string[] l = info[i+2].Split(',');
int len = l.Length;
for(int j=0;j<=lines.Length;j++)
{
if(j<len)
{
curve[i,j] = l[j];
}
else
{
curve[i,j] = "N"; //非数据,不画线
}
}
}
//获得最大,最小值
double maxY,minY,maxX,minX;
GetMaxMin(curve,
out maxY,out minY,out maxX,out minX);}
//画坐标g.DrawLine(solid,right,bottom,right
-padding/2,bottom-padding/3); //X箭头SizeF xf
= GetMaxSize(text,g,font);SizeF xf
= GetMaxSize(text,g,font);px[i]
= cx; System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ContentType
image.Save(System.Web.HttpContext.Current.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Gif);
}
finallyrow
= data.GetLength(0);maxY
= m;maxX
= p;使用方法
demo.cs
private void Page_Load(object sender, System.EventArgs e)
{string data = "Total,T1,T2,T3,T4;(kwh),N0.0,(day),Dyyyy-MM-dd;";int days = 50;
DateTime start = DateTime.Now.AddDays(-1*days);
System.Random random
= new Random();System.Text.StringBuilder sb
= new System.Text.StringBuilder();sb.Append(data);
string[] b = new string[days];for(int i=0;i<days;i++)a[
0] = ((int)DateTime.Now.AddDays(-1*i).ToOADate()).ToString();for(int j=1;j<6;j++)a[j]
= random.Next(100).ToString();}
b[i]
=string.Join(",",a);sb.Append(
string.Join(";",b));data
= sb.ToString();Report.Chart chart
= new Report.Chart();chart.Data
= data;chart.Render();
}
demo

此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。