日期:2014-05-20  浏览次数:21038 次

asp.net C# mschart绑定datatable 问题
datatable如下
companyid year count
001 2011 100
001 2012 200
002 2012 130
要设置X轴为year,要怎么绑定

------解决方案--------------------
例子
====================
C# code

            DataTable dt = new DataTable();
            dt.Columns.Add("Time", typeof(DateTime));
            dt.Columns.Add("Value", typeof(int));

            dt.Rows.Add(new DateTime(2012, 08, 16, 11, 05, 12), 14);


            dt.Rows.Add(new DateTime(2012, 08, 16, 12, 05, 12), 14);
            dt.Rows.Add(new DateTime(2012, 08, 16, 12, 30, 12), 12);
            dt.Rows.Add(new DateTime(2012, 08, 16, 12, 45, 12), 11);
            dt.Rows.Add(new DateTime(2012, 08, 16, 12, 59, 12), 16);


            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 05, 12), 13);
            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 10, 12), 18);
            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 15, 12), 15);
            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 20, 12), 12);
            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 35, 12), 13);
            dt.Rows.Add(new DateTime(2012, 08, 16, 13, 50, 12), 21);

            dt.Rows.Add(new DateTime(2012, 08, 16, 14, 50, 12), 21);

            //格式化标签和间隔
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = @"{yyyy-MM-dd HH时}";            
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;
            chart1.ChartAreas[0].AxisX.Interval = 1;

            //绑定数据
            chart1.Series[0].Points.DataBindXY(dt.DefaultView, "Time", dt.DefaultView, "Value");