C#一个简单报表实现

public partial class FormReport : Form
    {

        Point centerPoint = new Point(50, 360);//中心点位置

        int iXLength = 400;//X轴的长度
        int iYLength = 300;//Y轴的长度

        int iXStep = 30;//每个月份X轴的步长
        int iKeduLength = 10;//刻度长度
        int iYStep = 20;//Y轴步长

        int[] reportValue = { 10, 40, 52, 68, 94, 26, 54, 30, 74, 50, 58, 99 };


        public FormReport()
        {
            InitializeComponent();
        }

        private void FormReport_MouseMove(object sender, MouseEventArgs e)
        {

            this.Text = String.Format("x = {0},y = {1}", e.X, e.Y);
        }

        private void FormReport_Paint(object sender, PaintEventArgs e)
        {     
                         

            //得到窗体的绘制对象
            Graphics g = e.Graphics;


            Pen zuobiaoPen = new Pen(Color.Black, 1);
            zuobiaoPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            //绘制X轴
            g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X + iXLength, centerPoint.Y));

            //绘制Y轴
            g.DrawLine(zuobiaoPen, centerPoint, new Point(centerPoint.X, centerPoint.Y - iYLength));

            zuobiaoPen.Dispose();


            //绘制X坐标刻度
            
            for (int i = 1; i <= 12;i++ )
            {
                Point p1 = new Point(centerPoint.X + i * iXStep, centerPoint.Y - iKeduLength);
                Point p2 = new Point(centerPoint.X + i * iXStep, centerPoint.Y);
                g.DrawLine(Pens.Black, p1, p2);

                Point ptText = new Point(p2.X - 10, p2.Y);
                g.DrawString(String.Format("{0}月", i), this.Font, Brushes.Black, ptText);
            }

            //绘制Y坐标刻度

            
            for (int i = 1; i <= 10; i++)
            {
                Point p1 = new Point(centerPoint.X + iKeduLength, centerPoint.Y - i * iYStep);
                Point p2 = new Point(centerPoint.X, centerPoint.Y - i * iYStep);
                g.DrawLine(Pens.Black, p1, p2);

                Point ptText = new Point(p2.X - 30, p2.Y-10);
                g.DrawString(String.Format("{0}万", i*10), this.Font, Brushes.Black, ptText);
            }


            DrawReport(g);
        }


        void DrawReport(Graphics g)
        {
            Point [] pts = new Point [reportValue.Length];
            const int width = 2;
            for (int i = 0; i < reportValue.Length;i++ )
            {
                pts[i].X = centerPoint.X+(i + 1) * iXStep;
                pts[i].Y = centerPoint.Y - (int)(reportValue[i] / 10.0 * iYStep);


                Point ellipseCenter = new Point(pts[i].X - width, pts[i].Y - width);
                g.DrawEllipse(Pens.Black, new Rectangle(ellipseCenter, new Size(width * 2, width*2)));
            }

            g.DrawLines(Pens.Black, pts);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值