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 = "售货员";
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?