程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#繪制曲線圖的辦法

C#繪制曲線圖的辦法

編輯:C#入門知識

C#繪制曲線圖的辦法。本站提示廣大學習愛好者:(C#繪制曲線圖的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#繪制曲線圖的辦法正文


本文實例講述了C#繪制曲線圖的辦法。分享給年夜家供年夜家參考。詳細以下:

1. 曲線圖後果:

2. C#代碼:

/// <summary>
/// 主動依據參數調劑圖象年夜小
/// </summary>
public void Fit()
{
 //盤算字體間隔
 intFontSpace = FontSize + 5;
 //盤算圖象邊距
 float fltSpace = Math.Min(Width / 6, Height / 6);
 XSpace = fltSpace;
 YSpace = fltSpace;
 //盤算X軸刻度寬度
 XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
 //盤算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. 數據減少一個級其余後果:

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 類供給將對象繪制到顯示裝備的辦法
  private Bitmap objBitmap; //位圖對象
  private float fltWidth = 480; //圖象寬度
  private float fltHeight = 248; //圖象高度
  private float fltXSlice = 50; //X軸刻度寬度
  private float fltYSlice = 50; //Y軸刻度寬度
  private float fltYSliceValue = 20; //Y軸刻度的數值寬度
  private float fltYSliceBegin = 0; //Y軸刻度開端值
  private float fltTension = 0.5f;
  private string strTitle = "曲線圖"; //題目
  private string strXAxisText = "月份"; //X軸解釋文字
  private string strYAxisText = "萬元"; //Y軸解釋文字
  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; //軸解釋文字色彩
  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; //字體年夜小號數
  private float fltXRotateAngle = 30f; //X軸文字扭轉角度
  private float fltYRotateAngle = 0f; //Y軸文字扭轉角度
  private int intCurveSize = 2; //曲線線條年夜小
  private int intFontSpace = 0; //intFontSpace 是字體年夜小和間隔調劑出來的一個比擬合適的數字
  #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軸刻度的數值寬度
  /// </summary>
  public float YSliceValue
  {
   set { fltYSliceValue = value; }
   get { return fltYSliceValue; }
  }
  /// <summary>
  /// Y軸刻度開端值
  /// </summary>
  public float YSliceBegin
  {
   set { fltYSliceBegin = value; }
   get { return fltYSliceBegin; }
  }
  /// <summary>
  /// 張力系數
  /// </summary>
  public float Tension
  {
   set
   {
    if (value < 0.0f && value > 1.0f)
    {
     fltTension = 0.5f;
    }
    else
    {
     fltTension = value;
    }
   }
   get
   {
    return fltTension;
   }
  }
  /// <summary>
  /// 題目
  /// </summary>
  public string Title
  {
   set { strTitle = value; }
   get { return strTitle; }
  }
  /// <summary>
  /// 鍵,X軸數據
  /// </summary>
  public string[] Keys
  {
   set { strsKeys = value; }
   get { return strsKeys; }
  }
  /// <summary>
  /// 值,Y軸數據
  /// </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軸解釋文字
  /// </summary>
  public string XAxisText
  {
   set { strXAxisText = value; }
   get { return strXAxisText; }
  }
  /// <summary>
  /// Y軸解釋文字
  /// </summary>
  public string YAxisText
  {
   set { strYAxisText = value; }
   get { return strYAxisText; }
  }
  /// <summary>
  /// 軸解釋文字色彩
  /// </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軸文字扭轉角度
  /// </summary>
  public float XRotateAngle
  {
   get { return fltXRotateAngle; }
   set { fltXRotateAngle = value; }
  }
  /// <summary>
  /// Y軸文字扭轉角度
  /// </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>
  /// 字體年夜小號數
  /// </summary>
  public int FontSize
  {
   get { return intFontSize; }
   set { intFontSize = value; }
  }
  /// <summary>
  /// 曲線線條年夜小
  /// </summary>
  public int CurveSize
  {
   get { return intCurveSize; }
   set { intCurveSize = value; }
  }
  #endregion
  /// <summary>
  /// 主動依據參數調劑圖象年夜小
  /// </summary>
  public void Fit()
  {
   //盤算字體間隔
   intFontSpace = FontSize + 5;
   //盤算圖象邊距
   float fltSpace = Math.Min(Width / 6, Height / 6);
   XSpace = fltSpace;
   YSpace = fltSpace;
   //盤算X軸刻度寬度
   XSlice = (Width - 2 * XSpace) / (Keys.Length - 1);
   //盤算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圖象對象
  /// </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("產生毛病,Values的長度必需是Keys的整數倍!", new Font("宋體", FontSize + 5), new SolidBrush(TextColor), new Point((int)XSpace, (int)(Height / 2)));
   }
   return objBitmap;
  }
  /// <summary>
  /// 初始化和填充圖象區域,畫出邊框,初始題目
  /// </summary>
  private void InitializeGraph()
  {
   //依據給定的高度和寬度創立一個位圖圖象
   objBitmap = new Bitmap((int)Width, (int)Height);
   //從指定的 objBitmap 對象創立 objGraphics 對象 (即在objBitmap對象中繪圖)
   objGraphics = Graphics.FromImage(objBitmap);
   //依據給定色彩(LightGray)填充圖象的矩形區域 (配景)
   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軸盤算是以左上角為原點,向右和向下盤算的
   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);
   //初始化軸線解釋文字
   SetAxisText(ref objGraphics);
   //初始化X軸上的刻度和文字
   SetXAxis(ref objGraphics);
   //初始化Y軸上的刻度和文字
   SetYAxis(ref objGraphics);
   //初始化題目
   CreateTitle(ref objGraphics);
  }
  /// <summary>
  /// 初始化軸線解釋文字
  /// </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; //將要畫刻度的長度分段,並乘以50,以10為單元畫刻度線。
   float fltSliceHeight = XSlice / 10; //刻度線的高度
   objGraphics.TranslateTransform(fltX1, fltY1); //平移圖象(原點)
   objGraphics.RotateTransform(XRotateAngle, MatrixOrder.Prepend); //扭轉圖象
   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),將每一個刻度分五部門畫,但由於i以10為單元,得除以10
    if (iCount == 5)
    {
     objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 + Scale, fltY1 + fltSliceHeight * 1.5f, fltX2 + Scale, fltY2 - fltSliceHeight * 1.5f);
     //畫網格虛線
     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
     {
      //跨越規模,不畫任何刻度文字
     }
     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; //將要畫刻度的長度分段,並乘以50,以10為單元畫刻度線。
   float fltSliceWidth = YSlice / 10; //刻度線的寬度
   string strSliceText = string.Empty;
   objGraphics.TranslateTransform(XSpace - intFontSpace * YSliceBegin.ToString().Length, Height - YSpace); //平移圖象(原點)
   objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //扭轉圖象
   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),將每一個刻度分五部門畫,但由於i以10為單元,得除以10
    if (iCount == 5)
    {
     objGraphics.DrawLine(new Pen(new SolidBrush(AxisColor)), fltX1 - fltSliceWidth * 1.5f, fltY1 - Scale, fltX2 + fltSliceWidth * 1.5f, fltY2 - Scale);
     //畫網格虛線
     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); //平移圖象(原點)
     objGraphics.RotateTransform(YRotateAngle, MatrixOrder.Prepend); //扭轉圖象
     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>
  /// 初始化題目
  /// </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)));
  }
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved