日期:2014-05-20 浏览次数:20800 次
public partial class Chart2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindChart(ChartName, dt,basevalue);//传入你的chart的ID,以及之前的dt,basevale为基线的值 } } private void BindChart(System.Web.UI.DataVisualization.Charting.Chart YouChart,DataTable YouDataTable,double basevalue) { //MainSeries,MainChartArea和BaseSeries你可以根据你自己的改 YouChart.Series["MainSeries"].XValueMember = "time"; YouChart.Series["MainSeries"].YValueMembers = "count"; YouChart.ChartAreas["MainChartArea"].AxisX.Interval = 30; YouChart.ChartAreas["MainChartArea"].AxisX.IntervalType = DateTimeIntervalType.Auto; DataTable dt = new DataTable(); Random ro = new Random(); dt.Columns.Add("time"); dt.Columns.Add("count"); DateTime time = Convert.ToDateTime("08:45"); object[] rowArray = new object[2]; for (int i = 0; i <= 300; i++) { DataRow dr = dt.NewRow(); time = time.AddMinutes(1); rowArray[0] = time.ToString("hh:mm"); rowArray[1] = FindCount(time.ToString("hh:mm").ToString(), YouDataTable); dr.ItemArray = rowArray; dt.Rows.Add(dr); YouChart.Series["BaseSeries"].Points.AddXY(rowArray[0], basevalue); } YouChart.Series["MainSeries"].Points.DataBind(dt.DefaultView, "time", "count", ""); YouChart.Series["BaseSeries"].Color = Color.Red; } public double FindCount(string time, DataTable oldDataTable) { foreach (DataRow dr in oldDataTable.Rows) { if (dr[1].ToString() == time) { return double.Parse(dr[0].ToString()); } } return 0; } }