C#-利用控件实现柱形图分析

此博客展示了一段C#代码,通过SqlConnection连接数据库,使用SqlCommand和SqlDataAdapter获取数据并填充到DataSet中。接着根据数据计算柱形图的高度和位置,利用Panel和Label对象创建柱形图并添加到当前容器,实现数据库数据的柱形图展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

using System.Data.SqlClient;
namespace ColumnByControl
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=Server2012;database=db_TomeOne"))    //实例化一个SqlConnection对象
            {
                int XValse = 20;
                DataSet ds = new DataSet();                                    //创建DataSet对象
                //创建SqlCommand对象
                SqlCommand cmd = new SqlCommand("select * from tb_Rectangle select Sum(t_Num) from tb_Rectangle", con);
                SqlDataAdapter da = new SqlDataAdapter();                        //创建SqlDataAdapter对象
                da.SelectCommand = cmd;
                da.Fill(ds);                                                //Fill方法填充DataSet
                Panel[] p = new Panel[ds.Tables[0].Rows.Count];                    //实例化一个Panel数组
                int Values = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());            //商品总数
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i][0].ToString();
                    float f = Convert.ToInt32(ds.Tables[0].Rows[i][1].ToString());        //获取每个商品的数量
                    Size s = new Size();                                        //创建Size对象
                    s.Width = 30;                                            //设置柱形图宽度
                    s.Height = Convert.ToInt32(f / Values * 200);                    //计算柱形图高度
                    Point pint = new Point();                                    //创建Point对象
                    pint.X = XValse;                                        //x坐标
                    pint.Y = this.Height - 50 - s.Height;                            //y坐标
                    p[i] = new Panel();                                        //实例化一个Panel对象
                    p[i].Location = pint;                                        //设置位置
                    p[i].BackColor = Color.Red;                                //设置背景颜色
                    p[i].Size = s;                                            //设置大小
                    XValse += 40;                                            //设置Xvalse变量的值
                    Label lbl = new Label();                                    //创建Label对象
                    lbl.Text = ds.Tables[0].Rows[i][0].ToString();                    //设置Label显示的文本
                    lbl.Font = new Font("宋体", 9, FontStyle.Regular);                //设置Label的Font属性
                    lbl.ForeColor = Color.White;                                //设置Label的ForeColor属性
                    p[i].Controls.Add(lbl);                                    //添加控件
                    this.Controls.Add(p[i]);                                    //将控件数组添加到当前容器中
                }
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值