国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# 曲線圖生成代碼

2020-01-24 03:43:05
字體:
供稿:網(wǎng)友
復(fù)制代碼 代碼如下:

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坐標(biāo)的標(biāo)題
/// </summary>
public string Xtitle
{
get { return xtitle; }
set { xtitle = value; }
}
/// <summary>
/// Y坐標(biāo)的標(biāo)題
/// </summary>
public string Ytitle
{
get { return ytitle; }
set { ytitle = value; }
}
/// <summary>
/// 副標(biāo)題
/// </summary>
public string Title2
{
get { return title2; }
set { title2 = value; }
}
/// <summary>
/// 主標(biāo)題
/// </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>
/// 創(chuàng)建并輸出圖片
/// </summary>
/// <returns>生成的文件路徑</returns>
public string Draw()
{
#region 基礎(chǔ)定義
//取得記錄數(shù)量
int count = itemlist.Count;

//記算圖表寬度
int wd = 80 + 50 * (count - 1);
//設(shè)置最小寬度為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);
//定義大標(biāo)題字體
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);
//定義藍(lán)色過渡型筆刷
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);
//繪制大標(biāo)題
g.DrawString(title, Bfont, brush, wd / 2 - title.Length * 10, 5);
//繪制小標(biāo)題
g.DrawString(title2, Tfont, Silverbrush, wd / 2 - title.Length * 10 + 40, 25);
//繪制圖片邊框
g.DrawRectangle(Bp, 0, 0, img.Width - 1, img.Height - 1);

//繪制Y坐標(biāo)線
for (int i = 0; i < (count < 12 ? 12 : count); i++)
g.DrawLine(Sp, 40 + 50 * i, 60, 40 + 50 * i, 360);
//繪制X軸坐標(biāo)標(biāo)簽
for (int i = 0; i < count; i++)
g.DrawString(itemlist[i][0].ToString(), font, brush, 30 + 50 * i, 370);
//繪制X坐標(biāo)線
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坐標(biāo)值
g.DrawString(Math.Floor(s).ToString(), font, brush, 10, 55 + 30 * i);
}


//繪制Y坐標(biāo)軸
g.DrawLine(BBp, 40, 50, 40, 360);
//繪制X坐標(biāo)軸
g.DrawLine(BBp, 40, 360, 40 + 50 * ((count < 12 ? 12 : count) - 1) + 10, 360);

#endregion

#region 繪制曲線
//定義曲線轉(zhuǎn)折點
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);
}
//繪制發(fā)送曲線
g.DrawLines(Rp, p);

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

#endregion

//繪制Y坐標(biāo)標(biāo)題
g.DrawString(ytitle, Tfont, brush, 10, 40);
//繪制X坐標(biāo)標(biāo)題
g.DrawString(xtitle, Tfont, brush, 30, 385);
//圖片質(zhì)量
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;
}

}
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南澳县| 武乡县| 全椒县| 罗甸县| 凌云县| 武穴市| 彰化市| 巴林右旗| 洛隆县| 新蔡县| 高尔夫| 堆龙德庆县| SHOW| 寻甸| 徐闻县| 修水县| 滦平县| 阿克陶县| 望江县| 延庆县| 龙州县| 普洱| 秦皇岛市| 鹿邑县| 安平县| 克拉玛依市| 庐江县| 潮安县| 轮台县| 拉萨市| 瑞丽市| 琼海市| 修水县| 清原| 安化县| 东丰县| 绵阳市| 迭部县| 义乌市| 浙江省| 瑞昌市|