ChartControl控件绘制柱状图

1、新建一个DevExpress窗体(不要用WinForm窗体)在这里插入图片描述

2、拖入一个chartcontrol控件

在这里插入图片描述

3、鼠标右键,点击run designer

添加两个series
在这里插入图片描述

在这里插入图片描述

4、代码设置

  • 数据库表结构,我的想法是统计办事员和售货员的人数
    在这里插入图片描述

  • 数据库查询语句

select job,count(empno) as total from emp where job='办事员' group by job;
select job,count(empno) as total from emp where job='售货员' group by job;

在这里插入图片描述
在这里插入图片描述

Form1.cs代码设置:

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

namespace DXApplication9
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
            Bind();
        }
        public void Bind()
        {
            //Series类就是向图标里填充数据的类
            Series s1 = this.chartControl1.Series[0];
            Series s2 = this.chartControl1.Series[1];
            using (SqlConnection con=new SqlConnection("Data Source=.;Initial Catalog=test_10_23;User ID=sa;Password=123456;Connection Timeout=10"))
            {
                string sql = "select job,count(empno) as total from emp where job='办事员' group by job;";
                string sql1 = "select job,count(empno) as total from emp where job='售货员' group by job;";
                SqlDataAdapter sda=new SqlDataAdapter(sql,con);
                SqlDataAdapter sda1 = new SqlDataAdapter(sql1, con);
                DataSet ds = new DataSet();
                DataSet ds1 = new DataSet();
                sda.Fill(ds);
                sda1.Fill(ds1);
                s1.DataSource = ds.Tables[0];
                s2.DataSource = ds1.Tables[0];
                s1.ArgumentDataMember = "job";//横坐标
                s2.ArgumentDataMember = "job";
                s1.ValueDataMembers[0] = "total";//纵坐标
                s2.ValueDataMembers[0] = "total";
                s1.LegendText = "办事员";
                s2.LegendText = "售货员";
            }

        }
    }
}

5、启动程序

在这里插入图片描述

posted @ 2020-11-01 10:34  别团等shy哥发育  阅读(46)  评论(0)    收藏  举报