博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(C#)如何利用Graphics画出一幅图表
阅读量:6833 次
发布时间:2019-06-26

本文共 5289 字,大约阅读时间需要 17 分钟。

//获取坐标
        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            this.Text=string.Format("X={0},Y={1}",e.X,e.Y);
        }
       /// <summary>
        /// 绘制折线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            //首先确定原点
            Point centerPoint=new Point(180,340);
            //自定义一个带有箭头的画笔
            Pen pen = new Pen(Color.Black,1);
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            //得到当前窗体的Graphics对象
            Graphics g = e.Graphics;
            //画X轴和Y轴
            //g.DrawLine(pens.Black,centerPoint,new Point(centerPoint.X+600,centerPoint.Y));
            //g.DrawLine(Pens.Black, centerPoint, new Point(centerPoint.X, 40));
            g.DrawLine(pen, centerPoint, new Point(centerPoint.X + 650, centerPoint.Y));
            g.DrawLine(pen, centerPoint, new Point(centerPoint.X, 20));
            //绘制X轴的点
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(Pens.Black, new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y), new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y-5));
                g.DrawString((i + 1).ToString() + "月", this.Font, Brushes.Black, new PointF((centerPoint.X + (i + 1) * 50) - 7, centerPoint.Y + 3));
            }
            g.DrawString("X:月份",this.Font,Brushes.Black,new Point(828,355));
            //绘制Y轴的点
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(Pens.Black, new Point(centerPoint.X, centerPoint.Y - (i + 1) * 25), new Point(centerPoint.X + 5, centerPoint.Y-(i + 1) * 25));
                //g.DrawLine(Pens.Black, new Point(centerPoint.X , centerPoint.Y), new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y - 5));
                g.DrawString(string.Format("{0}",(i+1)*10), this.Font, Brushes.Black, new PointF((centerPoint.X + 5) - 35, (centerPoint.Y - (i + 1) * 25)-5));
            }
//计算十二个月销售额对应的坐标点
            double[] data = { 56.2, 66.3, 98.4, 34.5, 55.6, 87.3, 81.4, 33.3, 46.4, 34.6, 114.5, 80.4};
            PointF[] dataPoint = new PointF[data.Length];
            for (int i = 0; i < data.Length ; i++)
            {
                float y = (float)(340 - data[i] * 2.5);
                float x = centerPoint.X + (i + 1) * 50;
                PointF point = new PointF(x, y);
                dataPoint[i] = point;
            }
            //绘制十二个点的折线
            for (int i = 0; i < data.Length ; i++)
            {
                g.DrawRectangle(Pens.Black, dataPoint[i].X, dataPoint[i].Y, 2, 2);
            }
           //将十二个点串成线
            g.DrawLines(Pens.Black, dataPoint);
            //方法二:Path方法
            //GraphicsPath path = new GraphicsPath();//要导入using System.Drawing.Drawing2D;
            //for (int i = 0; i < data.Length; i++)
            //{
            //    path.AddRectangle(new RectangleF(dataPoint[i], new SizeF(2, 2)));
            //}
            //path.AddLines(dataPoint);
            //g.DrawPath(Pens.Black, path);

            g.DrawString("Y", this.Font, Brushes.Black, new Point(155,7));
            g.DrawString("销售额:单位(万元)", this.Font, Brushes.Black, new Point(14, 14));
            g.DrawString("某工厂某产品年度销售额图表",this.Font, Brushes.Black, new Point(420,14));
            pen.Dispose();
        }

//获取坐标

        private void Form2_MouseMove(object sender, MouseEventArgs e)
        {
            this.Text=string.Format("X={0},Y={1}",e.X,e.Y);
        }
       /// <summary>
        /// 绘制折线图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            //首先确定原点
            Point centerPoint=new Point(180,340);
            //自定义一个带有箭头的画笔
            Pen pen = new Pen(Color.Black,1);
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
            //得到当前窗体的Graphics对象
            Graphics g = e.Graphics;
            //画X轴和Y轴
            //g.DrawLine(pens.Black,centerPoint,new Point(centerPoint.X+600,centerPoint.Y));
            //g.DrawLine(Pens.Black, centerPoint, new Point(centerPoint.X, 40));
            g.DrawLine(pen, centerPoint, new Point(centerPoint.X + 650, centerPoint.Y));
            g.DrawLine(pen, centerPoint, new Point(centerPoint.X, 20));
            //绘制X轴的点
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(Pens.Black, new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y), new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y-5));
                g.DrawString((i + 1).ToString() + "月", this.Font, Brushes.Black, new PointF((centerPoint.X + (i + 1) * 50) - 7, centerPoint.Y + 3));
            }
            g.DrawString("X:月份",this.Font,Brushes.Black,new Point(828,355));
            //绘制Y轴的点
            for (int i = 0; i < 12; i++)
            {
                g.DrawLine(Pens.Black, new Point(centerPoint.X, centerPoint.Y - (i + 1) * 25), new Point(centerPoint.X + 5, centerPoint.Y-(i + 1) * 25));
                //g.DrawLine(Pens.Black, new Point(centerPoint.X , centerPoint.Y), new Point(centerPoint.X + (i + 1) * 50, centerPoint.Y - 5));
                g.DrawString(string.Format("{0}",(i+1)*10), this.Font, Brushes.Black, new PointF((centerPoint.X + 5) - 35, (centerPoint.Y - (i + 1) * 25)-5));
            }
//计算十二个月销售额对应的坐标点
            double[] data = { 56.2, 66.3, 98.4, 34.5, 55.6, 87.3, 81.4, 33.3, 46.4, 34.6, 114.5, 80.4};
            PointF[] dataPoint = new PointF[data.Length];
            for (int i = 0; i < data.Length ; i++)
            {
                float y = (float)(340 - data[i] * 2.5);
                float x = centerPoint.X + (i + 1) * 50;
                PointF point = new PointF(x, y);
                dataPoint[i] = point;
            }
            //绘制十二个点的折线
            for (int i = 0; i < data.Length ; i++)
            {
                g.DrawRectangle(Pens.Black, dataPoint[i].X, dataPoint[i].Y, 2, 2);
            }
           //将十二个点串成线
            g.DrawLines(Pens.Black, dataPoint);
            //方法二:Path方法
            //GraphicsPath path = new GraphicsPath();//要导入using System.Drawing.Drawing2D;
            //for (int i = 0; i < data.Length; i++)
            //{
            //    path.AddRectangle(new RectangleF(dataPoint[i], new SizeF(2, 2)));
            //}
            //path.AddLines(dataPoint);
            //g.DrawPath(Pens.Black, path);
            g.DrawString("Y", this.Font, Brushes.Black, new Point(155,7));
            g.DrawString("销售额:单位(万元)", this.Font, Brushes.Black, new Point(14, 14));
            g.DrawString("某工厂某产品年度销售额图表",this.Font, Brushes.Black, new Point(420,14));
            pen.Dispose();
        }

本文转自蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366800,如需转载请自行联系原作者

你可能感兴趣的文章
JAVA学习日志(9-2-异常)
查看>>
服务端与客户端共用smarty模板技术
查看>>
单例模式
查看>>
AIX的vmstat命令详解
查看>>
快速掌握Redis——第六招:持久化 (数据备份与恢复)
查看>>
Linux 下ffmpeg视频转码实战二——参数宝典
查看>>
原生的必定美丽
查看>>
显示文件的行数
查看>>
github常用指令
查看>>
svn安装笔记
查看>>
android LinearLayout的相关错误
查看>>
谢烟客---------Linux之压缩解压缩及归档工具的使用
查看>>
常用jvm参数配置以及提供系统堆益处日志监控方法
查看>>
《WPF揭秘》勘误表
查看>>
MongoDB入门2——MongoDB数据类型
查看>>
MySQL数据库服务器优化详细
查看>>
服务器HP DL385更换RAID卡电池
查看>>
Objective-C中的引用计数
查看>>
Provisioning Services 7.8 入门系列教程之十二 实现高可用性
查看>>
死锁的原因
查看>>