C#画坐标系

本文介绍了一种在Windows窗体应用程序中使用C#绘制坐标轴及其刻度的方法。包括如何在面板上画出X轴和Y轴,并从零或任意值开始绘制刻度线及标注。适用于需要在界面上直观展示数据的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;  

namespace 坐标1
{
    class XYlinesFactory
    {
        #region   画出X轴与Y轴
        /// <summary>  
        /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素  
        /// </summary>  
        /// <param name="pan"></param>  
        public void DrawXY(Panel pan)
        {
            Graphics g = pan.CreateGraphics();
            //整体内缩move像素  
            float move = 50f;
            float newX = pan.Width - move;
            float newY = pan.Height - move;

            //绘制X轴,  
            PointF px1 = new PointF(move, newY);
            PointF px2 = new PointF(newX, newY);
            g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
            //绘制Y轴  
            PointF py1 = new PointF(move, move);
            PointF py2 = new PointF(move, newY);

            g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
        }
        #endregion

        /// <summary>  
        /// 画出Y轴上的分值线,从零开始  
        /// </summary>  
        /// <param name="pan"></param>  
        /// <param name="maxY"></param>  
        /// <param name="len"></param>  
        #region   画出Y轴上的分值线,从零开始
        public void DrawYLine(Panel pan, float maxY, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 0; i <= len; i++)    //len等份Y轴  
            {
                PointF px1 = new PointF(move, LenY * i / len + move);
                PointF px2 = new PointF(move + 4, LenY * i / len + move);
                string sx = (maxY - maxY * i / len).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
                StringFormat drawFormat = new StringFormat();
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("Y轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
        }
        #endregion

        /// <summary>  
        /// 画出Y轴上的分值线,从任意值开始  
        /// </summary>  
        /// <param name="pan"></param>  
        /// <param name="minY"></param>  
        /// <param name="maxY"></param>  
        /// <param name="len"></param>  
        #region   画出Y轴上的分值线,从任意值开始
        public void DrawYLine(Panel pan, float minY, float maxY, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 0; i <= len; i++)    //len等份Y轴  
            {
                PointF px1 = new PointF(move, LenY * i / len + move);
                PointF px2 = new PointF(move + 4, LenY * i / len + move);
                string sx = (maxY - (maxY - minY) * i / len).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), px1, px2);
                StringFormat drawFormat = new StringFormat();
                drawFormat.Alignment = StringAlignment.Far;
                drawFormat.LineAlignment = StringAlignment.Center;
                g.DrawString(sx, new Font("宋体", 8f), Brushes.Black, new PointF(move / 1.2f, LenY * i / len + move * 1.1f), drawFormat);
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("Y轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(move / 3, move / 2f));
        }

        #endregion
        /// <summary>  
        /// 画出X轴上的分值线,从零开始  
        /// </summary>  
        /// <param name="pan"></param>  
        /// <param name="maxX"></param>  
        /// <param name="len"></param>  
        #region   画出X轴上的分值线,从零开始
        public void DrawXLine(Panel pan, float maxX, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 1; i <= len; i++)
            {
                PointF py1 = new PointF(LenX * i / len + move, pan.Height - move - 4);
                PointF py2 = new PointF(LenX * i / len + move, pan.Height - move);
                string sy = (maxX * i / len).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
                g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(LenX * i / len + move, pan.Height - move / 1.1f));
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("X轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(pan.Width - move / 1.5f, pan.Height - move / 1.5f));
        }
        #endregion

        #region   画出X轴上的分值线,从任意值开始
        /// <summary>  
        /// 画出X轴上的分值线,从任意值开始  
        /// </summary>  
        /// <param name="pan"></param>  
        /// <param name="minX"></param>  
        /// <param name="maxX"></param>  
        /// <param name="len"></param>  
        public void DrawXLine(Panel pan, float minX, float maxX, int len)
        {
            float move = 50f;
            float LenX = pan.Width - 2 * move;
            float LenY = pan.Height - 2 * move;
            Graphics g = pan.CreateGraphics();
            for (int i = 0; i <= len; i++)
            {
                PointF py1 = new PointF(LenX * i / len + move, pan.Height - move - 4);
                PointF py2 = new PointF(LenX * i / len + move, pan.Height - move);
                string sy = ((maxX - minX) * i / len + minX).ToString();
                g.DrawLine(new Pen(Brushes.Black, 2), py1, py2);
                g.DrawString(sy, new Font("宋体", 8f), Brushes.Black, new PointF(LenX * i / len + move, pan.Height - move / 1.1f));
            }
            Pen pen = new Pen(Color.Black, 1);
            g.DrawString("X轴", new Font("宋体 ", 10f), Brushes.Black, new PointF(pan.Width - move / 1.5f, pan.Height - move / 1.5f));
        }
        #endregion  
    }
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百老

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值