Asp.Netインスタンス:C#コードで折れ線図を描く

11888 ワード

1.構想分析:


1.まず必ずデータベースを読み込み、データテーブルの3列のデータを調べて3つの配列に入れる.
2.Bitmapを新規作成し、すべての図形をGraphicsで実現します.
3.スクリーン座標に基づいて、X軸とY軸を描き、目盛りをつける.
4.X軸とY軸をもとに、各データのスクリーン上の相対座標を算出し、折れ線を引く.
5.データ点に点を描き、データ値を表示する.
6.図形を修飾し、文字などの情報を表示して終了します.

2.データの取得


1.データベースからデータを取得してデータベース接続を確立し、クエリーを行い、コード多重化率を向上させるためにC#クラスを特別に新規作成したり、関数を1つだけ書いたりすることができます.次のコードは、原作者が書いたC#クラスSQL_です.接続、App_に置くCodeフォルダの下で、データベース操作を実行するために使用されます(私は直接データベースhelperクラスを新規作成しました.前の文章にはすでにあります)コードは以下の通りです.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

///
///          
///
public class SQL_Connection
{
private string sql = null;

public SQL_Connection(string sql)
{
this.sql = sql;
}
public DataSet query()
{
string ConStr = "Data Source=……;Initial Catalog=……;Persist SecurityInfo=True;UserID=……;Password=……";
SqlConnection conn = new SqlConnection(ConStr);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = this.sql;
cmd.CommandTimeout = 10000;
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
conn.Close();
return ds;
}
}

2.バックグラウンドのCSファイルでデータベースクエリーを実行し、結果を配列に配置します.具体的なコードは以下の通りです.
//データベース照会
string sql = "select * from manqi_cx where manqpfl <>0 and manqpfl1<>0 and manqpfl2<>0 and bmid=2";
SQL_Connection sqlCon = new SQL_Connection(sql);
DataSet ds = sqlCon.query();

//        
double[] manqpfl = new double[TotalMonths];
double[] manqpfl1 = new double[TotalMonths];
double[] manqpfl2 = new double[TotalMonths];
for (int i = 0; i
{
manqpfl[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl"]);
manqpfl1[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl1"]);
manqpfl2[i] = Convert.ToDouble(ds.Tables[0].Rows[i]["manqpfl2"]);
}

3.具体的な実現


1.Bitmapを新規作成し、Graphicsを使用して文字を描画し、表示します.具体的なコードは以下の通りです.

Bitmap image = new Bitmap(width, height);
Graphics graphics = Graphics.FromImage(image);
try
{
graphics.Clear(Color.White);
Font font = new Font("Arial", 9, FontStyle.Regular);//          
Font font1 = new Font("  ", 15, FontStyle.Regular);//          
Font font2 = new Font("  ", 10, FontStyle.Regular);//          
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Gray, Color.Gray, 1.2f, true);//      LinearGradientBrush
LinearGradientBrush brush2 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Red, 1.2f, true);//     LinearGradientBrush
LinearGradientBrush brush3 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Green, Color.Green, 1.2f, true);//     LinearGradientBrush
LinearGradientBrush brush4 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Brown, Color.Brown, 1.2f, true);//     LinearGradientBrush

graphics.FillRectangle(Brushes.White, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Black);
if (manqpfl.Length > 0 & manqpfl1.Length > 0 & manqpfl2.Length > 0)
{
graphics.DrawString("  " + MinYear.ToString() + " "+MinMonth.ToString()+"  " + MaxYear.ToString() + " "+MaxMonth.ToString()+"         ", font1, brush1, new PointF(width / 2 - 200, 30));//      
graphics.DrawString("        ", font2, brush2, new PointF(width / 2 + 200, 500));//      
graphics.DrawString("        ", font2, brush3, new PointF(width / 2 + 200, 530));//      
graphics.DrawString("        ", font2, brush4, new PointF(width / 2 + 200, 560));//      
}
//       
graphics.DrawRectangle(new Pen(Color.Black), 0, 0, image.Width - 1, image.Height - 1);

//  Y 
int x = 60;
Pen mypen1 = new Pen(Color.Black, 2);
graphics.DrawLine(mypen1, x, 120, x, 458);
//      
Pen mypen = new Pen(brush, 1);
for (int i = 0; i < TotalMonths; i++)
{
graphics.DrawLine(mypen, x, 120, x, 458);
x = x + 60; //Y   60         
}
//      
int y = 158;
for (int i = 0; i < 5; i++)
{
graphics.DrawLine(mypen, 60, y, width-15, y);
y = y + 60; //X   60         
}
//  X 
graphics.DrawLine(mypen1, 60, y, width-15, y);

//  x   
x = 35;
for (int i = 0; i < TotalMonths; i++)
{
string month= ds.Tables[0].Rows[i]["yy"].ToString() + "." + ds.Tables[0].Rows[i]["mm"].ToString();
graphics.DrawString(month.ToString(), font, Brushes.Red, x, 460); //           
x = x + 60; //X   60         
}
//y   
String[] m = { " 90", " 80", " 70", " 60", " 50" ," 40"};
y = 150;
for (int i = 0; i < 6; i++)
{
graphics.DrawString(m[i].ToString(), font, Brushes.Red, 25, y); //           
y = y + 60; //Y   60         
}

int[] Count = new int[TotalMonths];
int[] Count1 = new int[TotalMonths];
int[] Count2 = new int[TotalMonths];
for (int j = 0; j < TotalMonths; j++)
{
Count[j] = Convert.ToInt32(manqpfl[j] * 360 / 60);//             
Count1[j] = Convert.ToInt32(manqpfl1[j] * 360 / 60);
Count2[j] = Convert.ToInt32(manqpfl2[j] * 360 / 60);
}

Point[] myPoint = caculatePoints(Count, TotalMonths);//              
Point[] myPoint1 = caculatePoints(Count1, TotalMonths);
Point[] myPoint2 = caculatePoints(Count2, TotalMonths);

Pen mypen2 = new Pen(Color.Red, 2);//manqpfl     
Pen mypen3 = new Pen(Color.Green, 2);//manqpfl1     
Pen mypen4 = new Pen(Color.Brown, 2);//manqpfl2     

graphics.DrawLines(mypen2, myPoint); //  manqpfl  
graphics.DrawLines(mypen3, myPoint1); //  manqpfl1  
graphics.DrawLines(mypen4, myPoint2); //  manqpfl2  

//       
for (int i = 0; i < TotalMonths;i++ )
{
graphics.DrawString(manqpfl[i].ToString(), font, Brushes.Blue, myPoint[i].X-8, myPoint[i].Y-15);
graphics.DrawString(manqpfl1[i].ToString(), font, Brushes.Blue, myPoint1[i].X-8, myPoint1[i].Y-15);
graphics.DrawString(manqpfl2[i].ToString(), font, Brushes.Blue, myPoint2[i].X-8, myPoint2[i].Y-15);
}

//  Bitmap
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(MStream.ToArray());
//this.mess.InnerHtml = MStream.ToArray().ToString();
}
finally
{
graphics.Dispose();
image.Dispose();
}

//         
protected Point[] caculatePoints(int[] Count,int TotalNumber)
{
Point[] myPoint = new Point[TotalNumber];
for (int i = 0; i < TotalNumber; i++)
{
myPoint[i].X = 60 + i * 60; myPoint[i].Y = 700 - Count[i];
}
return myPoint;
}

例:


新しいページをDefault 3とします.aspx;
そして直接Default 3.aspx.csバックグラウンドページには、次のようにコードが書かれています.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

//     

double[] manqpfl0 ={ 2009.1, 2009.2, 2009.3, 2009.4, 2009.5, 2009.6, 2009.7, 2009.8, 2009.9, 2009.10, 2009.11, 2009.12, 2010.1, 2010.2, 2010.3 };
double[] manqpfl = { 84, 85, 86, 84, 85, 84, 86, 87, 85, 84, 88, 87, 84, 85, 87 };
double[] manqpfl1 = { 72, 71, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 71, 72, 717 };
double[] manqpfl2 = { 60, 61, 60, 61, 60, 61, 60, 61, 60, 61, 60, 61, 60, 61, 62 };
int TotalMonths = 14;


Bitmap image = new Bitmap(1000, 1000);
Graphics graphics = Graphics.FromImage(image);
try
{
graphics.Clear(Color.White);
Font font = new Font("Arial", 9, FontStyle.Regular);//          
Font font1 = new Font("  ", 15, FontStyle.Regular);//          
Font font2 = new Font("  ", 10, FontStyle.Regular);//          
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Gray, Color.Gray, 1.2f, true);//      LinearGradientBrush
LinearGradientBrush brush2 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.Red, 1.2f, true);//     LinearGradientBrush
LinearGradientBrush brush3 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Green, Color.Green, 1.2f, true);//     LinearGradientBrush
LinearGradientBrush brush4 = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Brown, Color.Brown, 1.2f, true);//     LinearGradientBrush

graphics.FillRectangle(Brushes.White, 0, 0, 1000, 1000);
Brush brush1 = new SolidBrush(Color.Black);

//       
if (manqpfl.Length > 0 & manqpfl1.Length > 0 & manqpfl2.Length > 0)
{
graphics.DrawString("  2009 1  2010 3         ", font1, brush1, new PointF(1000 / 2 - 200, 30));//      
graphics.DrawString("        ", font2, brush2, new PointF(1000 / 2 + 200, 500));//      
graphics.DrawString("        ", font2, brush3, new PointF(1000 / 2 + 200, 530));//      
graphics.DrawString("        ", font2, brush4, new PointF(1000 / 2 + 200, 560));//      
}
//       
graphics.DrawRectangle(new Pen(Color.Black), 0, 0, image.Width - 1, image.Height - 1);

//  Y 
int x = 60;
Pen mypen1 = new Pen(Color.Black, 2);
graphics.DrawLine(mypen1, x, 120, x, 458);
//      
Pen mypen = new Pen(brush, 1);
for (int i = 0; i < TotalMonths; i++)
{
graphics.DrawLine(mypen, x, 120, x, 458);
x = x + 60; //Y   60         
}
//      
int y = 158;
for (int i = 0; i < 5; i++)
{
graphics.DrawLine(mypen, 60, y, 1000 - 15, y);
y = y + 60; //X   60         
}
//  X 
graphics.DrawLine(mypen1, 60, y, 1000 - 15, y);

//  x   
x = 35;
for (int i = 0; i < TotalMonths; i++)
{
string month = manqpfl0[i].ToString(); //      
graphics.DrawString(month.ToString(), font, Brushes.Red, x, 460); //           
x = x + 60; //X   60         
}
//y   
String[] m = { " 90", " 80", " 70", " 60", " 50", " 40" };
y = 150;
for (int i = 0; i < 6; i++)
{
graphics.DrawString(m[i].ToString(), font, Brushes.Red, 25, y); //           
y = y + 60; //Y   60         
}

int[] Count = new int[TotalMonths];
int[] Count1 = new int[TotalMonths];
int[] Count2 = new int[TotalMonths];
for (int j = 0; j < TotalMonths; j++)
{
Count[j] = Convert.ToInt32(manqpfl[j] * 360 / 60);//             
Count1[j] = Convert.ToInt32(manqpfl1[j] * 360 / 60);
Count2[j] = Convert.ToInt32(manqpfl2[j] * 360 / 60);
}

Point[] myPoint = caculatePoints(Count, TotalMonths);//              
Point[] myPoint1 = caculatePoints(Count1, TotalMonths);
Point[] myPoint2 = caculatePoints(Count2, TotalMonths);

Pen mypen2 = new Pen(Color.Red, 2);//manqpfl     
Pen mypen3 = new Pen(Color.Green, 2);//manqpfl1     
Pen mypen4 = new Pen(Color.Brown, 2);//manqpfl2     

graphics.DrawLines(mypen2, myPoint); //  manqpfl  
graphics.DrawLines(mypen3, myPoint1); //  manqpfl1  
graphics.DrawLines(mypen4, myPoint2); //  manqpfl2  

//       
for (int i = 0; i < TotalMonths; i++)
{
graphics.DrawString(manqpfl[i].ToString(), font, Brushes.Blue, myPoint[i].X - 8, myPoint[i].Y-15);
graphics.DrawString(manqpfl1[i].ToString(), font, Brushes.Blue, myPoint1[i].X - 8, myPoint1[i].Y - 15);
graphics.DrawString(manqpfl2[i].ToString(), font, Brushes.Blue, myPoint2[i].X - 8, myPoint2[i].Y - 15);
}

//  Bitmap
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(MStream.ToArray());

}
finally
{
graphics.Dispose();
image.Dispose();
}
}



//         
protected Point[] caculatePoints(int[] Count,int TotalNumber)
{
Point[] myPoint = new Point[TotalNumber];
for (int i = 0; i < TotalNumber; i++)
{
myPoint[i].X = 60 + i * 60; myPoint[i].Y = 700 - Count[i];
}
return myPoint;
}
}

これで折れ線図の描画が完了します!