C\#곡선 도 를 그 리 는 방법

19887 단어 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\#프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기