a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c# 曲線圖生成代碼

2020-01-24 03:43:05
字體:
來源:轉載
供稿:網友
復制代碼 代碼如下:

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;
}

}
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 999国产在线 | 不卡久久| 久久久天天 | av下一页 | 一区二区三区国产精品 | 久久夜夜操 | 欧美激情自拍偷拍 | 精品乱子伦一区二区三区 | 欧美日韩a | 蜜臀av性久久久久蜜臀aⅴ流畅 | 久久一二 | 国产不卡视频在线观看 | 欧美精品网站 | 男女视频免费 | 在线一二区 | 午夜a毛片 | 玖玖国产精品视频 | 亚洲精品国品乱码久久久久 | 中文字幕在线三区 | 男女黄色免费网站 | 欧美精品成人一区二区三区四区 | 超碰免费在线观看 | 久久91 | 亚洲三级网站 | 亚洲毛片 | 成人爽a毛片一区二区免费 美女一级毛片 | 免费xxxxx在线观看网站软件 | 91网址| 久久激情视频 | 午夜激情在线 | 在线观看国产视频 | 欧美高清不卡 | 成人黄页在线观看 | 色综合天天天天做夜夜夜夜做 | 99精品欧美一区二区三区 | 欧洲一级视频 | 欧美一区2区三区4区公司二百 | 免看一级一片 | 91精品在线观看入口 | 美国特级a毛片免费网站 | 韩国女主播bj精品久久 |