这东西挺直观
封装个类
public class aaa
{
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private int number; public int Number
{
get { return number; }
set { number = value; }
}
}
private void Form1_Load(object sender, EventArgs e)
{
List<aaa> ooo=new List<aaa>();
SqlConnection conn = new SqlConnection("server=.;database=aaaaa;user=sa;pwd=123");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from chart";
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows)
{
while (dr.Read())
{
aaa rrr = new aaa();
rrr.Name=dr["name"].ToString();
rrr.Number = Convert.ToInt32(dr["degree"]);
ooo.Add(rrr);
}
}
cmd.Dispose();
conn.Close();
chart1.DataSource=ooo;
chart1.Series["Series1"].XValueMember ="name"; //Series是个集合[Series1]代表蓝色的柱子,那么就会有[Series2] 设置来源颜色 可以代表其他的
chart1.Series["Series1"].YValueMembers = "Number"; }
不多说,这个chart可以添加多个柱状图进行比较,他自己自动获取Y轴最大值所以所比较的两个单位在数值上不要相差很多,当然也可以设计成饼型
在Series集合里面再添加一个Series,用linq做
添加linq toSql类
private void Form1_Load(object sender, EventArgs e)
{
DataClasses1DataContext context = new DataClasses1DataContext();
List<Score> list = context.Score.Take().ToList();
chart1.DataSource = list;
chart1.Series["Series1"].XValueMember = "sno";
chart1.Series["Series1"].YValueMembers = "degree";
chart1.Series["Series2"].XValueMember = "cno";
chart1.Series["Series2"].YValueMembers = "sno";
}