c# 曲線圖產生代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing.Imaging;
using System.Collections;

namespace Curve
{
public class CurveDrawing
{
string title, title2, ytitle, xtitle;
/// <summary>
/// X座標的標題
/// </summary>
public string Xtitle
{
get { return xtitle; }
set { xtitle = value; }
}
/// <summary>
/// Y座標的標題
/// </summary>
public string Ytitle
{
get { return ytitle; }
set { ytitle = value; }
}
/// <summary>
/// 副標題
/// </summary>
public string Title2
{
get { return title2; }
set { title2 = value; }
}
/// <summary>
/// 主標題
/// </summary>
public string Title
{
get { return title; }
set { title = value; }
}
double yMax, yMin;
List<ArrayList> itemlist;

public CurveDrawing(List<ArrayList> itemlist, string title, string title2 = "")
{
this.itemlist = itemlist;
this.title = title;
this.title2 = title2;

yMax = -100000000;
yMin = 100000000;
for (int i = 0; i < itemlist.Count; i++)
{
if (Convert.ToDouble(itemlist[i][1]) > yMax)
yMax = Convert.ToDouble(itemlist[i][1]);
if (Convert.ToDouble(itemlist[i][1]) < yMin)
yMin = Convert.ToDouble(itemlist[i][1]);
}
}
/// <summary>
/// 建立並輸出圖片
/// </summary>
/// <returns>產生的檔案路徑</returns>
public string Draw()
{
#region 基礎定義
//取得記錄數量
int count = itemlist.Count;

//記算圖表寬度
int wd = 80 + 50 * (count - 1);
//設定最小寬度為640
if (wd < 640) wd = 640;
//產生Bitmap對像
Bitmap img = new Bitmap(wd, 400);
//定義黑色畫筆
Pen Bp = new Pen(Color.Black);
//加粗的黑色
Pen BBp = new Pen(Color.Black, 2);
//定義紅色畫筆
Pen Rp = new Pen(Color.Red);
//定義銀灰色畫筆
Pen Sp = new Pen(Color.Silver);
//定義大標題字型
Font Bfont = new Font("黑體", 12, FontStyle.Bold);
//定義一般字型
Font font = new Font("Arial", 8);
//定義大點的字型
Font Tfont = new Font("Arial", 9);
//定義黑色過渡型筆刷
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Black, Color.Black, 1.2F, true);
//定義藍色過渡型筆刷
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);
LinearGradientBrush Silverbrush = new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.Silver, Color.Silver, 1.2F, true);
#endregion

//產生繪圖對像
try
{
using (Graphics g = Graphics.FromImage(img))
{
#region 繪製圖表
//繪製底色
g.DrawRectangle(new Pen(Color.White, 400), 0, 0, img.Width, img.Height);
//繪製大標題
g.DrawString(title, Bfont, brush, wd / 2 - title.Length * 10, 5);
//繪製小標題
g.DrawString(title2, Tfont, Silverbrush, wd / 2 - title.Length * 10 + 40, 25);
//繪製圖片邊框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);

//繪製Y座標線
for (int i = 0; i < (count < 12 ? 12 : count); i++)
g.DrawLine(Sp, 40 + 50 * i, 60, 40 + 50 * i, 360);
//繪製X軸座標標籤
for (int i = 0; i < count; i++)
g.DrawString(itemlist[i][0].ToString(), font, brush, 30 + 50 * i, 370);
//繪製X座標線
for (int i = 0; i < 11; i++)
{
g.DrawLine(Sp, 40, 60 + 30 * i, 40 + 50 * ((count < 12 ? 12 : count) - 1), 60 + 30 * i);
double s = yMax - (yMax + Math.Abs(yMin)) / 10 * i;//最大的Y座標值
g.DrawString(Math.Floor(s).ToString(), font, brush, 10, 55 + 30 * i);
}

//繪製Y座標軸
g.DrawLine(BBp, 40, 50, 40, 360);
//繪製X座標軸
g.DrawLine(BBp, 40, 360, 40 + 50 * ((count < 12 ? 12 : count) - 1) + 10, 360);

#endregion

#region 繪製曲線
//定義曲線轉折點
Point[] p = new Point[count];
for (int i = 0; i < count; i++)
{
p[i].X = 40 + 50 * i;
p[i].Y = 360 - (int)(((Convert.ToDouble(itemlist[i][1]) + Math.Abs(yMin)) / ((yMax + Math.Abs(yMin)) / 10)) * 30);
}
//繪製發送曲線
g.DrawLines(Rp, p);

for (int i = 0; i < count; i++)
{
//繪製發送記錄點的數值
g.DrawString(itemlist[i][1].ToString(), font, Bluebrush, p[i].X + 5, p[i].Y - 10);
//繪製發送記錄點
g.DrawRectangle(Rp, p[i].X - 2, p[i].Y - 2, 4, 4);
}

#endregion

//繪製Y座標標題
g.DrawString(ytitle, Tfont, brush, 10, 40);
//繪製X座標標題
g.DrawString(xtitle, Tfont, brush, 30, 385);
//圖片品質
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//儲存繪製的圖片
string basePath = HttpContext.Current.Server.MapPath("/Curve/"),
fileName = Guid.NewGuid() + ".jpg";

using (FileStream fs = new FileStream(basePath + fileName, FileMode.CreateNew))
{
if (!System.IO.Directory.Exists(basePath))
System.IO.Directory.CreateDirectory(basePath);
img.Save(fs, ImageFormat.Jpeg);
return "/Curve/" + fileName;
}
}

}
catch (Exception)
{
throw;
}

}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.