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

C#.NET 用程序畫圖,曲線圖

編輯:C#入門知識

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using System.Drawing;

 

/// <summary>
/// DrawingCurve 的摘要說明
/// </summary>
public class DrawingCurve
{
    
    public int intXLong = 800;   //圖片大小 長
    public int intYLong = 600;   //圖片大小 高
    public int intXMultiple = 1;    //零刻度的值 X
    public int intYMultiple = 0;    //零刻度的值 Y
    public int intXMax = 12;    //最大刻度(點數) X
    public int intYMax = 30;    //最大刻度(點數) Y

    public int intLeft = 50;   //左邊距
    public int intRight = 120; //右邊距
    public int intTop = 30;    //上邊距
    public int intEnd = 50;    //下邊距

    public string strXText = "時間(單位:月)";    //單位 X
    public string strYText = "數量(單位:個)";    //單位 Y
    public string strTitle = "趨勢線圖";    //標題
    public DataTable tbData;    //要統計的數據


    private int intXScale = 30;    //一刻度長度 X
    private int intYScale = 30;    //一刻度高度 Y
    //private int intX = 0;   //0點 X坐標
    //private int intY = 0;   //0點 Y坐標
    public int intData = 0;    //記錄數

    public DrawingCurve()
    {

        intXScale = (intXLong - intLeft - intRight) / (intXMax + 1);//一刻度長度 X
        intYScale = (intYLong - intTop - intEnd) / (intYMax + 1);//一刻度高度 Y

        //intX = intXLong - intLeft;   //0點 X坐標
        //intY = intYLong - intEnd;   //0點 Y坐標
    }

    public Bitmap DrawingImg()
    {

        Bitmap img = new Bitmap(intXLong, intYLong); //圖片大小
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.Snow);
        g.DrawString(strTitle, new Font("宋體", 14), Brushes.Black, new Point(5, 5));
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intYLong - intEnd, intXLong - intRight, intYLong - intEnd); //繪制橫向
        g.DrawLine(new Pen(Color.Black, 2), intLeft, intTop, intLeft, intYLong - intEnd);   //繪制縱向

        //繪制縱坐標
        g.DrawString(strYText, new Font("宋體", 12), Brushes.Black, new Point(intLeft, intTop));//Y 單位
        Point p1 = new Point(intLeft - 10, intYLong - intEnd);
        for (int j = 0; j <= intYMax; j++)
        {
            p1.Y = intYLong - intEnd - j * intYScale;
            Point pt = new Point(p1.X + 10, p1.Y);
            //繪制縱坐標的刻度和直線
            g.DrawLine(Pens.Black, pt, new Point(p1.X + 15, p1.Y));
            //繪制縱坐標的文字說明
            g.DrawString(Convert.ToString(j + intYMultiple), new Font("宋體", 12), Brushes.Black, new Point(p1.X - 25, p1.Y - 8));
        }

        //繪制橫坐標
        g.DrawString(strXText, new Font("宋體", 12), Brushes.Black, new Point(intXLong - intRight, intYLong - intEnd));//X 單位
        Point p = new Point(intLeft, intYLong - intEnd);
        for (int i = 0; i < intXMax; i++)
        {
            p.X = intLeft + i * intXScale;
            //繪制橫坐標刻度和直線
            g.DrawLine(Pens.Black, p, new Point(p.X, p.Y - 5));
            //繪制橫坐標的文字說明
            g.DrawString(Convert.ToString(i + intXMultiple), new Font("宋體", 12), Brushes.Black, p);
        }


        intData = tbData.Rows.Count;
        if (intData > 0)
        {
            //趨勢線圖
            for (int i = 0; i < intData - 1; i++)
            {
                DataRow Row1 = tbData.Rows[i];
                DataRow Row2 = tbData.Rows[i + 1];
                //定義起點
                Point rec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row1[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row1[1].ToString()) - intYMultiple) * intYScale));
                //定義終點
                Point dec = new Point(Convert.ToInt32(intLeft + ((TurnNumber(Row2[0].ToString()) - intXMultiple) * intXScale)), Convert.ToInt32(intYLong - intEnd - (TurnNumber(Row2[1].ToString()) - intYMultiple) * intYScale));
                //繪制趨勢折線
                g.DrawLine(new Pen(Color.Red), rec, dec);
  &

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