Asp.net 인스턴스: C# 코드로 선형 차트 그리기
11861 단어 asp.net 사이트 구축
1. 먼저 데이터베이스를 읽고 데이터 테이블에 있는 이 세 열의 데이터를 찾아내 세 개의 그룹에 넣는다.
2. 비트맵을 새로 만들고 그릴 모든 도형을 Graphics로 실현한다.
3. 화면 좌표에 따라 먼저 X축과 Y축을 그리고 눈금을 표시한다.
4. X축과 Y축을 바탕으로 각 데이터가 화면에 있는 상대 좌표를 계산하여 접선을 그린다.
5. 데이터 포인트에 점을 그리고 데이터 값을 표시한다.
6. 도면을 손질하고 문자와 같은 정보를 표시하며 끝냅니다.
2. 데이터 획득
1. 데이터베이스에서 데이터를 얻어 데이터베이스 연결을 구축하고 조회하면 C#클래스를 새로 만들어서 코드 복용률을 높일 수도 있고 함수 하나만 쓸 수도 있다.다음 코드는 원작자가 쓴 C# 클래스 SQL 입니다.Connection, App코드 폴더 아래에 데이터베이스 조작을 수행하는 데 사용되는 코드는 다음과 같습니다.
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. 새 비트맵을 만들고 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;
}
예를 들면 다음과 같습니다.
새 웹 페이지는Default3.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;
}
}
이렇게 하면 접선도 그리기를 완성할 수 있다!