欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

C#坐标轴画图

最编程 2024-04-01 12:22:20
...

原文连接(http://blog.sina.com.cn/s/blog_60a9a03f0100jf05.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*Bitmap bitmap = new Bitmap(this.Height, this.Width); // 画布的大小
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawString("GDI+ 技术", new Font("宋体", 12), new SolidBrush(Color.Red), this.Width / 3, 0);

            g.DrawLine(new Pen(Color.Yellow, 12), 0, 40, this.Width, 40);

            g.DrawLine(new Pen(Color.Red, 0.1F), 0, 80, this.Width, 80);
            this.BackgroundImage = bitmap;
            g.Dispose();//*/

            //数据初始化   
            string[] month = new string[12] { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
            float[] d = new float[12] { 20.5F, 60, 10.8F, 15.6F, 30, 70.9F, 50.3F, 30.7F, 70, 50.4F, 30.8F, 20 };
            //画图初始化   
            Bitmap bMap = new Bitmap(500, 500);
            Graphics gph = Graphics.FromImage(bMap);
            gph.Clear(Color.White);

            PointF cPt = new PointF(40, 420);//中心点(坐标轴原点)   
            PointF[] xPt = new PointF[3]{
             new   PointF(cPt.Y+15,cPt.Y),
             new   PointF(cPt.Y,cPt.Y-8),
             new   PointF(cPt.Y,cPt.Y+8)};//(X轴右边三角形的三个点坐标)   
            PointF[] yPt = new PointF[3]{
             new   PointF(cPt.X,cPt.X-15),
             new   PointF(cPt.X+8,cPt.X),
             new   PointF(cPt.X-8,cPt.X)};//Y轴三角形   
            gph.DrawString("某工厂某产品月生产量图表", new Font("宋体", 14),
             Brushes.Black, new PointF(cPt.X + 12, cPt.X));//图表标题   
            //画X轴   
            gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y);//画x轴
            gph.DrawPolygon(Pens.Black, xPt);//画X轴箭头三角形
            gph.FillPolygon(new SolidBrush(Color.Black), xPt);//X轴箭头填充黑色
            gph.DrawString("月份", new Font("宋体", 12),
             Brushes.Black, new PointF(cPt.Y + 12, cPt.Y - 10));
            //画Y轴   
            gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X);
            gph.DrawPolygon(Pens.Black, yPt);
            gph.FillPolygon(new SolidBrush(Color.Black), yPt);
            gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(6, 7));
            for (int i = 1; i <= 12; i++)
            {
                //画Y轴刻度   
                if (i < 11)
                {
                    gph.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black,
                     new PointF(cPt.X - 30, cPt.Y - i * 30 - 6));//Y轴坐标
                    gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30);//Y轴刻度
                }
                //画X轴项目   
                gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black,
                 new PointF(cPt.X + i * 30 - 5, cPt.Y + 5));//写出数字一、二、三、……
                gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11),
                 Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 20));//写出月、月、月、……
                if (month[i - 1].Length > 2)
                    gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11),
                     Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 35));
                //画点   
                gph.DrawEllipse(Pens.Black, cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 - 1.5F, 10, 3);//坐标点处画椭圆
                gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30 - 1.5F, cPt.Y - d[i - 1] * 3 + 1.5F, 3, 3);
                //画数值   
                gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black,
                 new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3));//坐标点处的数字
                //画折线   
                if (i > 1)
                    gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3);
            }
            //显示在pictureBox1控件中
            //this..Image = bMap;
            
            //保存输出图片   
            bMap.Save("E:\\test.bmp");  
        }
    }
}