效果圖:sql
1.首先,在工具箱找到chart控件,拖到窗體中。工具
2.關於chart控件的細節設計:this
series集合設計: chartType能夠選擇折線圖、柱狀圖、圓餅圖等等。spa
isValueShowAsLabel:設置是否顯示數據點信息設計
3.數據綁定(datatable);orm
string sql = "";
string s1 = "";
string m1 = "";
sql = string.Format("select 做業時間,sum(做業用時) as 總做業用時 from 學生做業 a, 學生科目表 b where a.科目ID = b.ID and a.學生ID ={0} and 做業時間 between '{1}' and '{2}' group by 做業時間", cbx學生.SelectedValue,dtpstartTime.Value.ToLongDateString().ToString(),dtpendTime.Value.ToLongDateString().ToString());
OleDbDataAdapter sda = new OleDbDataAdapter(sql, objcnn);
// 2.建立數據集:
DataSet ds = new DataSet();
// 3.填充數據集:
sda.Fill(ds);
// 4.綁定:DataGridView
DataTable dt = ds.Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string s = dt.Rows[i]["做業時間"].ToString();
string m = dt.Rows[i]["總做業用時"].ToString();
s1 += s + ",";
m1 += m + ",";blog
//主要將dt中的數據串成一個字符串;
}
s1 = s1.Substring(0, s1.Length - 1);
m1 = m1.Substring(0, m1.Length - 1);字符串
//主要字符串最後的逗號;
List<string> list = new List<string>(s1.Split(','));
List<string> x = new List<string>(s1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));string
//主要將字符串轉成list集合的形式;it
List<string> listy = new List<string>(m1.Split(','));
List<string> y = new List<string>(m1.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries));
this.chart_時間.Series["日期/完成做業時間"].Points.DataBindXY(x, y); //x、y是設置x、y軸的數據 }