我是MSChart的新手,下面的代码适用于多系列专栏。 但我知道应该有一个较短的方式来显示没有的多系列 诉诸于创建两个数据表或更好的几个代码,如 使用DataBindTable或DataBindCrossTable。
在下面做我正在做的事情有没有更短的方式?我想要做的是显示在特定学年注册的学生人数。
String sql, sql2;
sql = @"SELECT Major, SchoolYear, COUNT(*) as TotalStudents
FROM StudentList
WHERE Major='AFT'
GROUP BY Major, Schoolyear";
sql2 = @"SELECT Major, SchoolYear, COUNT(*) as TotalStudents
FROM StudentList
WHERE Major='AFT'
GROUP BY Major, Schoolyear";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlDataAdapter dta = new SqlDataAdapter(sql, con);
DataSet dst = new DataSet();
DataTable dt;
dta.Fill(dst);
dt = dst.Tables[0];
chart1.Width = 900;
chart1.ChartAreas[0].AxisX.Title = "School Year";
chart1.ChartAreas[0].AxisY.Title = "Enrollment";
chart1.Series[0].Name = dt.Rows[0][0].ToString();
chart1.Series[0].XValueMember = "SchoolYear";
chart1.Series[0].YValueMembers = "TotalStudents";
chart1.Series[0].Points.DataBindXY(dt.CreateDataReader(),"SchoolYear", dt.CreateDataReader(), "TotalStudents");
dta = new SqlDataAdapter(sql2, con);
dst = new DataSet();
dta.Fill(dst);
dt = dst.Tables[0];
chart1.Series.Add(dt.Rows[0][0].ToString());
chart1.Series[1].XValueMember = "SchoolYear";
chart1.Series[1].YValueMembers = "TotalStudents";
chart1.Series[1].Points.DataBindXY(dt.CreateDataReader(), "SchoolYear", dt.CreateDataReader(), "TotalStudents");
答案 0 :(得分:1)
你会在下面试试吗?
String sql, sql2;
sql = @"SELECT Major, SchoolYear, COUNT(*) as TotalStudents
FROM StudentList
WHERE Major='AFT'
GROUP BY Major, Schoolyear";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlDataAdapter dta = new SqlDataAdapter(sql, con);
DataSet dst = new DataSet();
DataTable dt;
dta.Fill(dst);
dt = dst.Tables[0];
chart1.DataSource = dt;
chart1.Width = 900;
chart1.ChartAreas["abc"].AxisX.Title = "School Year";
chart1.ChartAreas["abc"].AxisY.Title = "Enrollment";
chart1.Series["xyz"].Name = dt.Rows[0][0].ToString();
chart1.Series["xyz"].XValueMember = "SchoolYear";
chart1.Series["xyz"].YValueMembers = "TotalStudents";
chart1.DataBind();
HTML:
<asp:Chart ID="chart1" runat="server" ImageStorageMode="UseImageLocation" ImageLocation="ChartPic_#SEQ(300,3)">
<ChartAreas>
<asp:ChartArea Name="abc">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>