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

MSChart问题
我想要下面的效果:

要求:
1.X轴是固定的,从8:45到13:45,显示整点的时间
2.随时间的变化,走势图一点点延长,不是一下平铺整张图片
3.有参考线(平行于X轴的一条线)
用MSChart实现,语言ASP.NET


------解决方案--------------------

这是最终效果图,代码:
前台:
<asp:Chart ID="ChartName" runat="server" Palette="None" Height="500px" 
Width="861px">
<Titles>
<asp:Title Name="MainTitles" Text="统计图表" Font="Microsoft Sans Serif, 16pt" TextStyle="Frame">
</asp:Title>
</Titles>
<Series>
<asp:Series Name="MainSeries" ChartArea="MainChartArea" ChartType="Spline" 
XValueType="Time">
</asp:Series>
<asp:Series ChartArea="MainChartArea" ChartType="Line" Name="BaseSeries">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="MainChartArea" ShadowColor="White">
<AxisY IntervalAutoMode="VariableCount">
<MajorGrid LineColor="White" />
</AxisY>
<AxisX IntervalOffsetType="Number" IntervalType="Number">
<MajorGrid LineColor="White" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
后台:

C# code

 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;
        }  
    }