C# 画坐标,graphics,C# 坐标自定义X轴、Y轴

原创 2012年08月27日 21:08:02

先看效果:

其中X轴,Y轴都是可以自定义的,可以从零开始,也可以从任意位置开始。

看坐标的画法:,

下面通过五个函数,来分别说明,注意这里坐标的定位是通过外层的Panel来实现的。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace PointToPic
{
    class XYLinesFactory
    {
        #region   画出X轴与Y轴
        /// <summary>
        /// 在任意的panel里画一个坐标,坐标所在的四边形距离panel边50像素
        /// </summary>
        /// <param name="pan"></param>
        public static 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 static 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 static 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 static 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 static 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

    }
}


调用部分:一个主窗体,里面放一个Panel,就可以调用了:这里的Panel的名字是pnlPic,然后写她的Paint事件就可以了

  private void pnlPic_Paint(object sender, PaintEventArgs e)
        {   
            XYLinesFactory.DrawXY(pnlPic);
            XYLinesFactory.DrawYLine(pnlPic, 3, 4, 5);
            XYLinesFactory.DrawXLine(pnlPic, 1.2f, 8);
        }


 

相关文章推荐

C# 画图控件CHART 自定义坐标轴刻度标签

项目中用到柱形图,当数据列比较多的时候, X轴的刻度标签会被精简为几个,无法在每个柱子下方显示其刻度值。这时候可以用自定义标签解决问题。...

c#改变坐标轴的标注。

I have a chart it looks like this  I want to change Y axis numbering and label it 1% 10% 33.% etc ...

vs2010 c# graphics绘制坐标系

在txt文件里写入坐标,绘制图形。 鼠标位于坐标系内,显示2条红色十字线,和坐标值,离开坐标系,2者消失。...

C# gdi画坐标轴

gdi坐标轴需要自己画,画好XY两条线以及箭头,便可着手思考画刻度还有所画点与坐标的配合 考虑清楚,你需要最大坐标(Xmax、Ymax)是多少,需要几个大刻度(count),每个大刻度下对应多少小刻...
  • Eriliz
  • Eriliz
  • 2015年05月06日 16:10
  • 665

vs2010 c# graphics 坐标系画曲线

坐标为每10s传感器采集的数据 代码:using System; using System.Collections; using System.Collections.Generic; using ...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

c#绘制坐标系

#region   画出X轴与Y轴           private void  DrawXY()            {                 int   MaxX;     ...

C#Windows窗体界面设计_06_通过调整坐标系来调整数据

binzhouweichao@163.com 2013-10-19 修改前面的项目。 在之前的项目中,对数据data的处理是通过手动运算的方法,来放大足够的倍数,以取得合适的视图。 本节,对原始数据d...

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

GDI+坐标变换(C#)

平移 拉伸 坐标重置
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C# 画坐标,graphics,C# 坐标自定义X轴、Y轴
举报原因:
原因补充:

(最多只允许输入30个字)