日期:2014-05-18  浏览次数:21590 次

为什么我的Chart控件上显示不出曲线?
DateTime dt;
Chart cht1=new Chart();
Series s1=new Series();

  private void MainForm_Load(object sender, EventArgs e)
{
  s1 = new Series();
  s1.ChartType = SeriesChartType.Line;
  s1.Color = Color.Gold;  
  s1.BorderWidth = 1;  
  s1.ShadowOffset = 0;  
  s1.IsVisibleInLegend = false;  
  s1.MarkerStyle = MarkerStyle.Circle;  
  s1.MarkerSize = 3;
  s1.XValueType = ChartValueType.Time;
  s1.YValueType = ChartValueType.Auto;
  cht1.Location = pt;
  cht1.Width = 445;
  cht1.Height = 172;
  cht1.BackColor = Color.Azure;
  cht1.Titles.Add("Power history curve");
  cht1.Series.Add(s1);
  Controls.Add(cht1);
}


private void timer1_Tick(object sender, EventArgs e)//一个定时器,每1秒中进来一次
  {
  dt = DateTime.Now;
  dt.ToLongTimeString();
  cht1.Series[0].Points.AddXY(dt.ToLongTimeString(), 10);//每次定时到时都画一个纵坐标为10的点
  }


为什么我能看到Chart控件显示在面板上,但是看不到画出的曲线呢?

------解决方案--------------------
你没有再Load事件中加Timer的Tick事件

private void MainForm_Load(object sender, EventArgs e)
{
s1 = new Series();
s1.ChartType = SeriesChartType.Line;
s1.Color = Color.Gold;
s1.BorderWidth = 1;
s1.ShadowOffset = 0;
s1.IsVisibleInLegend = false;
s1.MarkerStyle = MarkerStyle.Circle;
s1.MarkerSize = 3;
s1.XValueType = ChartValueType.Time;
s1.YValueType = ChartValueType.Auto;
cht1.Location = pt;
cht1.Width = 445;
cht1.Height = 172;
cht1.BackColor = Color.Azure;
cht1.Titles.Add("Power history curve");
cht1.Series.Add(s1);
Controls.Add(cht1);
  
System.Timers.Timer timer1 = new System.Timers.Timer(1000);
timer1 .Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1 .AutoReset = true;
timer1 .Enabled = true;
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
dt = DateTime.Now;
dt.ToLongTimeString();
cht1.Series[0].Points.AddXY(dt.ToLongTimeString(), 10);//每次定时到时都画一个纵坐标为10的点
}



------解决方案--------------------
呵呵 用DEVexpress吧 那个强大多了
------解决方案--------------------
用這個 http://www.arction.com/
------解决方案--------------------
C# code

private void MainForm_Load(object sender, EventArgs e)
{
  s1 = new Series();
  s1.ChartType = SeriesChartType.Line;
  s1.Color = Color.Gold;   
  s1.BorderWidth = 1;   
  s1.ShadowOffset = 0;   
  s1.IsVisibleInLegend = false;   
  s1.MarkerStyle = MarkerStyle.Circle;   
  s1.MarkerSize = 3;
  s1.XValueType = ChartValueType.Time;
  s1.YValueType = ChartValueType.Auto;
  cht1.Location = pt;
  cht1.Width = 445;
  cht1.Height = 172;
  cht1.BackColor = Color.Azure;
  cht1.Titles.Add("Power history curve");
  cht1.Series.Add(s1);
  Controls.Add(cht1);


   //加上这个试试
for (int i = 0; i < cht1.Series.Count; i++)
            {
                cht1.Series[i].ChartType = SeriesChartType.Line;
            }

}