C#繪制曲線圖的方法
本文實(shí)例講述了C#繪制曲線圖的方法。分享給大家供大家參考。具體如下:
1. 曲線圖效果:
2. C#代碼:
/// <summary>
/// 自動(dòng)根據(jù)參數(shù)調(diào)整圖像大小
/// </summary>
public void Fit()
{
//計(jì)算字體距離
intFontSpace = FontSize + 5;
//計(jì)算圖像邊距
float fltSpace = Math.Min(Width / 6, Height / 6);
XSpace = fltSpace;
YSpace = fltSpace;
//計(jì)算X軸刻度寬度
XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
//計(jì)算Y軸刻度寬度和Y軸刻度開(kāi)始值
float fltMinValue = 0;
float fltMaxValue = 0;
for (int i = 0; i < Values.Length; i++)
{
if (Values[i] < fltMinValue)
{
fltMinValue = Values[i];
}
else if (Values[i] > fltMaxValue)
{
fltMaxValue = Values[i];
}
}
if (YSliceBegin > fltMinValue)
{
YSliceBegin = fltMinValue;
}
int intYSliceCount = (int)(fltMaxValue / YSliceValue);
if (fltMaxValue % YSliceValue != 0)
{
intYSliceCount++;
}
YSlice = (Height - 2 * YSpace) / intYSliceCount;
}
3. 數(shù)據(jù)縮小一個(gè)級(jí)別的效果:
4. 完整代碼 DrawingCurve.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
namespace SarchPMS.Business.Draw
{
public class DrawingCurve : DrawingChart
{
/// <summary>
/// 畫(huà)曲線圖
/// </summary>
/// <param name="dsParameter"></param>
/// <returns></returns>
public override Bitmap DrawImage(DataSet dsParameter)
{
Curve2D cuv2D = new Curve2D();
cuv2D.Fit();
return cuv2D.CreateImage();
}
}
public class Curve2D
{
private Graphics objGraphics; //Graphics 類(lèi)提供將對(duì)象繪制到顯示設(shè)備的方法
private Bitmap objBitmap; //位圖對(duì)象
private float fltWidth = 480; //圖像寬度
private float fltHeight = 248; //圖像高度
private float fltXSlice = 50; //X軸刻度寬度
private float fltYSlice = 50; //Y軸刻度寬度
private float fltYSliceValue = 20; //Y軸刻度的數(shù)值寬度
private float fltYSliceBegin = 0; //Y軸刻度開(kāi)始值
private float fltTension = 0.5f;
private string strTitle = "曲線圖"; //標(biāo)題
private string strXAxisText = "月份"; //X軸說(shuō)明文字
private string strYAxisText = "萬(wàn)元"; //Y軸說(shuō)明文字
private string[] strsKeys = new string[] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; //鍵
private float[] fltsValues = new float[] { 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 99.5f, 36.4f, 78.2f, 56.4f, 45.8f, 66.5f, 20.0f, 30.0f, 50.0f, 55.4f, 21.6f, 12.8f }; //值
private Color clrBgColor = Color.Snow; //背景色
private Color clrTextColor = Color.Black; //文字顏色
private Color clrBorderColor = Color.Black; //整體邊框顏色
private Color clrAxisColor = Color.Black; //軸線顏色
private Color clrAxisTextColor = Color.Black; //軸說(shuō)明文字顏色
private Color clrSliceTextColor = Color.Black; //刻度文字顏色
private Color clrSliceColor = Color.Black; //刻度顏色
private Color[] clrsCurveColors = new Color[] { Color.Red, Color.Blue }; //曲線顏色
private float fltXSpace = 100f; //圖像左右距離邊緣距離
private float fltYSpace = 100f; //圖像上下距離邊緣距離
private int intFontSize = 9; //字體大小號(hào)數(shù)
private float fltXRotateAngle = 30f; //X軸文字旋轉(zhuǎn)角度
private float fltYRotateAngle = 0f; //Y軸文字旋轉(zhuǎn)角度
private int intCurveSize = 2; //曲線線條大小
private int intFontSpace = 0; //intFontSpace 是字體大小和距離調(diào)整出來(lái)的一個(gè)比較適合的數(shù)字
#region 公共屬性
/// <summary>
/// 圖像的寬度
/// </summary>
public float Width
{
set
{
if (value < 100)
{
fltWidth = 100;
}
else
{
fltWidth = value;
}
}
get
{
if (fltWidth <= 100)
{
return 100;
}
else
{
return fltWidth;
}
}
}
/// <summary>
/// 圖像的高度
/// </summary>
public float Height
{
set
{
if (value < 100)
{
fltHeight = 100;
}
else
{
fltHeight = value;
}
}
get
{
if (fltHeight <= 100)
{
return 100;
}
else
{
return fltHeight;
}
}
}
/// <summary>
/// X軸刻度寬度
/// </summary>
public float XSlice
{
set { fltXSlice = value; }
get { return fltXSlice; }
}
/// <summary>
/// Y軸刻度寬度
/// </summary>
public float YSlice
{
set { fltYSlice = value; }
get { return fltYSlice; }
}
/// <summary>
/// Y軸刻度的數(shù)值寬度
/// </summary>
public float YSliceValue
{
set { fltYSliceValue = value; }
get { return fltYSliceValue; }
}
/// <summary>
/// Y軸刻度開(kāi)始值
/// </summary>
public float YSliceBegin
{
set { fltYSliceBegin = value; }
get { return fltYSliceBegin; }
}
/// <summary>
/// 張力系數(shù)
/// </summary>
public float Tension
{
set
{
if (value < 0.0f && value > 1.0f)
{
fltTension = 0.5f;
}
else
{
fltTension = value;
}
}
get
{
return fltTension;
}
}
/// <summary>
/// 標(biāo)題
/// </summary>
public string Title
{
set { strTitle = value; }
get { return strTitle; }
}
/// <summary>
/// 鍵,X軸數(shù)據(jù)
/// </summary>
public string[] Keys
{
set { strsKeys = value; }
get { return strsKeys; }
}
/// <summary>
/// 值,Y軸數(shù)據(jù)
/// </summary>
public float[] Values
{
set { fltsValues = value; }
get { return fltsValues; }
}
/// <summary>
/// 背景色
/// </summary>
public Color BgColor
{
set { clrBgColor = value; }
get { return clrBgColor; }
}
/// <summary>
/// 文字顏色
/// </summary>
public Color TextColor
{
set { clrTextColor = value; }
get { return clrTextColor; }
}
/// <summary>
/// 整體邊框顏色
/// </summary>
public Color BorderColor
{
set { clrBorderColor = value; }
get { return clrBorderColor; }
}
/// <summary>
/// 軸線顏色
/// </summary>
public Color AxisColor
{
set { clrAxisColor = value; }
get { return clrAxisColor; }
}
/// <summary>
/// X軸說(shuō)明文字
/// </summary>
public string XAxisText
{
set { strXAxisText = value; }
get { return strXAxisText; }
}
/// <summary>
/// Y軸說(shuō)明文字
/// </summary>
public string YAxisText
{
set { strYAxisText = value; }
get { return strYAxisText; }
}
/// <summary>
/// 軸說(shuō)明文字顏色
/// </summary>
public Color AxisTextColor
{
set { clrAxisTextColor = value; }
get { return clrAxisTextColor; }
}
/// <summary>
/// 刻度文字顏色
/// </summary>
public Color SliceTextColor
{
set { clrSliceTextColor = value; }
get { return clrSliceTextColor; }
}
/// <summary>
/// 刻度顏色
/// </summary>
public Color SliceColor
{
set { clrSliceColor = value; }
get { return clrSliceColor; }
}
/// <summary>
/// 曲線顏色
/// </summary>
public Color[] CurveColors
{
set { clrsCurveColors = value; }
get { return clrsCurveColors; }
}
/// <summary>
/// X軸文字旋轉(zhuǎn)角度
/// </summary>
public float XRotateAngle
{
get { return fltXRotateAngle; }
set { fltXRotateAngle = value; }
}
/// <summary>
/// Y軸文字旋轉(zhuǎn)角度
/// </summary>
public float YRotateAngle
{
get { return fltYRotateAngle; }
set { fltYRotateAngle = value; }
}
/// <summary>
/// 圖像左右距離邊緣距離
/// </summary>
public float XSpace
{
get { return fltXSpace; }
set { fltXSpace = value; }
}
/// <summary>
/// 圖像上下距離邊緣距離
/// </summary>
public float YSpace
{
get { return fltYSpace; }
set { fltYSpace = value; }
}
/// <summary>
/// 字體大小號(hào)數(shù)
/// </summary>
public int FontSize
{
get { return intFontSize; }
set { intFontSize = value; }
}
/// <summary>
/// 曲線線條大小
/// </summary>
public int CurveSize
{
get { return intCurveSize; }
set { intCurveSize = value; }
}
#endregion
/// <summary>
/// 自動(dòng)根據(jù)參數(shù)調(diào)整圖像大小
/// </summary>
public void Fit()
{
//計(jì)算字體距離
intFontSpace = FontSize + 5;
//計(jì)算圖像邊距
float fltSpace = Math.Min(Width / 6, Height / 6);
XSpace = fltSpace;
YSpace = fltSpace;
//計(jì)算X軸刻度寬度
XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
//計(jì)算Y軸刻度寬度和Y軸刻度開(kāi)始值
float fltMinValue = 0;
float fltMaxValue = 0;
for (int i = 0; i < Values.Length; i++)
{
if (Values[i] < fltMinValue)
{
fltMinValue = Values[i];
}
else if (Values[i] > fltMaxValue)
{
fltMaxValue = Values[i];
}
}
if (YSliceBegin > fltMinValue)
{
YSliceBegin = fltMinValue;
}
int intYSliceCount = (int)(fltMaxValue / YSliceValue);
if (fltMaxValue % YSliceValue != 0)
{
intYSliceCount++;
}
YSlice = (Height - 2 * YSpace) / intYSliceCount;
}
/// <summary>
/// 生成圖像并返回bmp圖像對(duì)象
/// </summary>
/// <returns></returns>
public Bitmap CreateImage()
{
InitializeGraph();
int intKeysCount = Keys.Length;
int intValuesCount = Values.Length;
if (intValuesCount % intKeysCount == 0)
{
int intCurvesCount = intValuesCount / intKeysCount;
for (int i = 0; i < intCurvesCount; i++)
{
float[] fltCurrentValues = new float[intKeysCount];
for (int j = 0; j < intKeysCount; j++)
{
fltCurrentValues[j] = Values[i * intKeysCount + j];
}
DrawContent(ref objGraphics, fltCurrentValues, clrsCurveColors[i]);
}
}
else
{
objGraphics.DrawString("發(fā)生錯(cuò)誤,Values的長(zhǎng)度必須是Keys的整數(shù)倍!", new Font("宋體", FontSize + 5), new SolidBrush(TextColor), new Point((int)XSpace, (int)(Height / 2)));
}
return objBitmap;
}
/// <summary>
/// 初始化和填充圖像區(qū)域,畫(huà)出邊框,初始標(biāo)題
/// </summary>
private void InitializeGraph()
{
//根據(jù)給定的高度和寬度創(chuàng)建一個(gè)位圖圖像
objBitmap = new Bitmap((int)Width, (int)Height);
//從指定的 objBitmap 對(duì)象創(chuàng)建 objGraphics 對(duì)象 (即在objBitmap對(duì)象中畫(huà)圖)
objGraphics = Graphics.FromImage(objBitmap);
//根據(jù)給定顏色(LightGray)填充圖像的矩形區(qū)域 (背景)
objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width - 1, Height - 1); //畫(huà)邊框
objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2); //填充邊框
//畫(huà)X軸,注意圖像的原始X軸和Y軸計(jì)算是以左上角為原點(diǎn),向右和向下計(jì)算的
float fltX1 = XSpace;
float fltY1 = Height - YSpace;
float fltX2 = Width - XSpace + XSlice / 2;
float fltY2 = fltY1;
objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
//畫(huà)Y軸
fltX1 = XSpace;
fltY1 = Height - YSpace;
fltX2 = XSpace;
fltY2 = YSpace - YSlice / 2;
objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor), 1), fltX1, fltY1, fltX2, fltY2);
//初始化軸線說(shuō)明文字
SetAxisText(ref objGraphics);
//初始化X軸上的刻度和文字
SetXAxis(ref objGraphics);
//初始化Y軸上的刻度和文字
SetYAxis(ref objGraphics);
//初始化標(biāo)題
CreateTitle(ref objGraphics);
}
/// <summary>
/// 初始化軸線說(shuō)明文字
/// </summary>
/// <param name="objGraphics"></param>
private void SetAxisText(ref Graphics objGraphics)
{
float fltX = Width - XSpace + XSlice / 2 - (XAxisText.Length - 1) * intFontSpace;
float fltY = Height - YSpace - intFontSpace;
objGraphics.DrawString(XAxisText, new Font("宋體", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);
fltX = XSpace + 5;
fltY = YSpace - YSlice / 2 - intFontSpace;
for (int i = 0; i < YAxisText.Length; i++)
{
objGraphics.DrawString(YAxisText[i].ToString(), new Font("宋體", FontSize), new SolidBrush(AxisTextColor), fltX, fltY);
fltY += intFontSpace; //字體上下距離
}
}
/// <summary>
/// 初始化X軸上的刻度和文字
/// </summary>
/// <param name="objGraphics"></param>
private void SetXAxis(ref Graphics objGraphics)
{
float fltX1 = XSpace;
float fltY1 = Height - YSpace;
float fltX2 = XSpace;
float fltY2 = Height - YSpace;
int iCount = 0;
int iSliceCount = 1;
float Scale = 0;
float iWidth = ((Width - 2 * XSpace) / XSlice) * 50; //將要畫(huà)刻度的長(zhǎng)度分段,并乘以50,以10為單位畫(huà)刻度線。
float fltSliceHeight = XSlice / 10; //刻度線的高度
objGraphics.TranslateTransform(fltX1, fltY1); //平移圖像(原點(diǎn))
objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend); //旋轉(zhuǎn)圖像
objGraphics.DrawString(Keys[0].ToString(), new Font("宋體", FontSize), new SolidBrush(SliceTextColor), 0, 0);
objGraphics.ResetTransform(); //重置圖像
for (int i = 0; i <= iWidth; i += 10) //以10為單位
{
Scale = i * XSlice / 50;//即(i / 10) * (XSlice / 5),將每個(gè)刻度分五部分畫(huà),但因?yàn)閕以10為單位,得除以10
if (iCount == 5)
{
objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 + Scale, fltY1 + fltSliceHeight * 1.5f, fltX2 + Scale, fltY2 - fltSliceHeight * 1.5f);
//畫(huà)網(wǎng)格虛線
Pen penDashed = new Pen(new SolidBrush(AxisColor));
penDashed.DashStyle = DashStyle.Dash;
objGraphics.DrawLine(penDashed, fltX1 + Scale, fltY1, fltX2 + Scale, YSpace - YSlice / 2);
//這里顯示X軸刻度
if (iSliceCount <= Keys.Length - 1)
{
objGraphics.TranslateTransform(fltX1 + Scale, fltY1);
objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend);
objGraphics.DrawString(Keys[iSliceCount].ToString(), new Font("宋體", FontSize), new SolidBrush(SliceTextColor), 0, 0);
objGraphics.ResetTransform();
}
else
{
//超過(guò)范圍,不畫(huà)任何刻度文字
}
iCount = 0;
iSliceCount++;
if (fltX1 + Scale > Width - XSpace)
{
break;
}
}
else
{
objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 + Scale, fltY1 + fltSliceHeight, fltX2 + Scale, fltY2 - fltSliceHeight);
}
iCount++;
}
}
/// <summary>
/// 初始化Y軸上的刻度和文字
/// </summary>
/// <param name="objGraphics"></param>
private void SetYAxis(ref Graphics objGraphics)
{
float fltX1 = XSpace;
float fltY1 = Height - YSpace;
float fltX2 = XSpace;
float fltY2 = Height - YSpace;
int iCount = 0;
float Scale = 0;
int iSliceCount = 1;
float iHeight = ((Height - 2 * YSpace) / YSlice) * 50; //將要畫(huà)刻度的長(zhǎng)度分段,并乘以50,以10為單位畫(huà)刻度線。
float fltSliceWidth = YSlice / 10; //刻度線的寬度
string strSliceText = string.Empty;
objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace); //平移圖像(原點(diǎn))
objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋轉(zhuǎn)圖像
objGraphics.DrawString(YSliceBegin.ToString(), new Font("宋體", FontSize), new SolidBrush(SliceTextColor), 0, 0);
objGraphics.ResetTransform(); //重置圖像
for (int i = 0; i < iHeight; i += 10)
{
Scale = i * YSlice / 50; //即(i / 10) * (YSlice / 5),將每個(gè)刻度分五部分畫(huà),但因?yàn)閕以10為單位,得除以10
if (iCount == 5)
{
objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);
//畫(huà)網(wǎng)格虛線
Pen penDashed = new Pen(new SolidBrush(AxisColor));
penDashed.DashStyle = DashStyle.Dash;
objGraphics.DrawLine(penDashed, XSpace, fltY1 - Scale, Width - XSpace + XSlice / 2, fltY2 - Scale);
//這里顯示Y軸刻度
strSliceText = Convert.ToString(YSliceValue * iSliceCount + YSliceBegin);
objGraphics.TranslateTransform(XSpace - intFontSize * strSliceText.Length, fltY1 - Scale); //平移圖像(原點(diǎn))
objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //旋轉(zhuǎn)圖像
objGraphics.DrawString(strSliceText, new Font("宋體", FontSize), new SolidBrush(SliceTextColor), 0, 0);
objGraphics.ResetTransform(); //重置圖像
iCount = 0;
iSliceCount++;
}
else
{
objGraphics.DrawLine(new Pen(new SolidBrush(SliceColor)), fltX1 - fltSliceWidth, fltY1 - Scale, fltX2 + fltSliceWidth, fltY2 - Scale);
}
iCount++;
}
}
/// <summary>
/// 畫(huà)曲線
/// </summary>
/// <param name="objGraphics"></param>
private void DrawContent(ref Graphics objGraphics, float[] fltCurrentValues, Color clrCurrentColor)
{
Pen CurvePen = new Pen(clrCurrentColor, CurveSize);
PointF[] CurvePointF = new PointF[Keys.Length];
float keys = 0;
float values = 0;
for (int i = 0; i < Keys.Length; i++)
{
keys = XSlice * i + XSpace;
values = (Height - YSpace) + YSliceBegin - YSlice * (fltCurrentValues[i] / YSliceValue);
CurvePointF[i] = new PointF(keys, values);
}
objGraphics.DrawCurve(CurvePen, CurvePointF, Tension);
}
/// <summary>
/// 初始化標(biāo)題
/// </summary>
/// <param name="objGraphics"></param>
private void CreateTitle(ref Graphics objGraphics)
{
objGraphics.DrawString(Title, new Font("宋體", FontSize), new SolidBrush(TextColor), new Point((int)(Width - XSpace) - intFontSize * Title.Length, (int)(YSpace - YSlice / 2 - intFontSpace)));
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
上一篇:實(shí)現(xiàn)ASP.NET無(wú)刷新下載并提示下載完成的開(kāi)發(fā)思路
欄 目:C#教程
下一篇:C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
本文標(biāo)題:C#繪制曲線圖的方法
本文地址:http://m.jygsgssxh.com/a1/C_jiaocheng/6923.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子


