欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#繪制曲線圖的方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(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軸刻度開始值
 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>
  /// 畫曲線圖
  /// </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 類提供將對(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軸刻度開始值
  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軸刻度開始值
  /// </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軸刻度開始值
   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ū)域,畫出邊框,初始標(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ì)象中畫圖)
   objGraphics = Graphics.FromImage(objBitmap);
   //根據(jù)給定顏色(LightGray)填充圖像的矩形區(qū)域 (背景)
   objGraphics.DrawRectangle(new Pen(BorderColor, 1), 0, 0, Width - 1, Height - 1); //畫邊框
   objGraphics.FillRectangle(new SolidBrush(BgColor), 1, 1, Width - 2, Height - 2); //填充邊框
   //畫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);
   //畫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; //將要畫刻度的長(zhǎng)度分段,并乘以50,以10為單位畫刻度線。
   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è)刻度分五部分畫,但因?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);
     //畫網(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ò)范圍,不畫任何刻度文字
     }
     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; //將要畫刻度的長(zhǎng)度分段,并乘以50,以10為單位畫刻度線。
   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è)刻度分五部分畫,但因?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);
     //畫網(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>
  /// 畫曲線
  /// </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ú)刷新下載并提示下載完成的開發(fā)思路

欄    目:C#教程

下一篇:C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解

本文標(biāo)題:C#繪制曲線圖的方法

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6923.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有