zoukankan      html  css  js  c++  java
  • WinForm绘制折线统计图

    效果图

     

     public partial class 折线图 : Form
        {
            string[] strYue = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" };
            string[] strShuLiang = { "100", "68", "87", "67", "10", "50", "90", "60", "20", "30", "78", "56" };
            DataTable dt = new DataTable();
            public 折线图()
            {
                InitializeComponent();
                DataColumn dc = new DataColumn("YueFen", typeof(string));
                dt.Columns.Add(dc);
                DataColumn dc1 = new DataColumn("ShuLiang", typeof(string));
                dt.Columns.Add(dc1);
                //加载数据
                for (int i = 0; i < 12; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["YueFen"] = strYue[i];
                    dr["ShuLiang"] = strShuLiang[i];
                    dt.Rows.Add(dr);
                }
                comboBox1.SelectedItem = "上半年";
                btn_ChaXun_Click(null,null);
               
            }
    
            private void btn_ChaXun_Click(object sender, EventArgs e)
            {
    
                List<Point> pos = new List<Point>();
                int i1, i2;
                Bitmap bitM = new Bitmap(this.panel1.Width, this.panel1.Height);
                Graphics g = Graphics.FromImage(bitM);
                g.Clear(Color.White);
                Brush b = new SolidBrush(Color.Blue);
                Font f = new Font("Arial", 9, FontStyle.Regular);
                Pen p = new Pen(b);
                //绘制横线
                //找出折线统计图的0,0点
                int ZheX = 40;
                int ZheY = 0;
                int y = 40;
    
                for (int i = 0; i < 6; i++)
                {
                    g.DrawLine(new Pen(Color.Red), 40, y, 260, y);
                    g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32);
                    y += 40;
    
                }
                ZheY = y - 40;
                //绘制竖线
                for (int i = 0; i < 6; i++)
                {
                    g.DrawLine(new Pen(Color.Red), ZheX + 40 * i, 20, 40 + 40 * i, 240);
                    //  g.DrawString(i * 20 + "", new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 10, this.panel1.Height - y - 32);
                    if (comboBox1.Text == "上半年")
                    {
                        g.DrawString(strYue[i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8);
                    }
                    else
                    {
                        g.DrawString(strYue[6 + i], new Font("宋体", 12, FontStyle.Regular), new SolidBrush(Color.Red), 20 + 40 * i, ZheY + 8);
                    }
    
                }
    
                if (comboBox1.Text == "上半年")
                {
                    //加载右侧List
                    listBox1.Items.Clear();
                    for (int i = 0; i < 6; i++)
                    {
    
                        listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "");
    
                    }
                    i1 = 0;
                    i2 = 6;
    
                }
                else
                {
                    //加载右侧List
                    listBox1.Items.Clear();
                    for (int i = 6; i < 12; i++)
                    {
    
                        listBox1.Items.Add(strYue[i] + " : " + strShuLiang[i] + "");
    
                    }
                    i1 = 6;
                    i2 = 12;
                }
                for (int i = i1; i < i2; i++)
                {
                    //根据新的0,0点和横线的增量获取新的Y值
                    //新的Y值=数量/绘制的文字数量的增量(0,20,40)  *  40(绘制的图中的px的增量,40px)
                    decimal d = (decimal.Parse(dt.Rows[i]["ShuLiang"].ToString()) / 20) * 40;
                    int d1 = (int)d;
                    Point p1;
                    if (i1 >= 6)
                    {
                         p1 = new Point((i + 1-6) * 40, ZheY - d1);
                    }
                    else
                    {
                         p1 = new Point((i + 1) * 40, ZheY - d1);
                    }
                   
                    pos.Add(p1);
                }
              
               
    
                //连线  
                for (int i = 0; i < 5; i++)
                {
                    g.DrawLine(p, pos[i], pos[i + 1]);
                }
                this.panel1.BackgroundImage = bitM;
            }
           
        }
  • 相关阅读:
    Windows 显示隐藏文件
    Python 程序一行代码解决乘法口诀表
    【转发】基于Bert-NER构建特定领域的中文信息抽取框架(上)
    【转发】GET和POST两种基本请求方法的区别
    【转发】实现yolo3模型训练自己的数据集总结
    第十章集合总结
    2016-2017 201671010134 异常处理
    JAVA基础编程
    2016-2017 201671010134 第六章总结
    java总结
  • 原文地址:https://www.cnblogs.com/zyadmin/p/8425516.html
Copyright © 2011-2022 走看看