c#我用chart读取数据库显示点,当点都读取完了怎么让他停止,等再插入数据的时候再重新显示
或者当数据库点读取完了让他Y变成0,等再插入数据的时候显示新点
我把X轴设成时间了,当数据库数据读完了,却变成这样了
正常应该像如下图片
下面是我写的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient; //需要添加的命名空间
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace 实时曲线
{
public partial class Form1 : Form
{
static int aaa = 0;
public Form1()
{
InitializeComponent();
InitChart();
}
private void InitChart()
{
DateTime time = DateTime.Now;
textBox1.Text = "开始时间:" + time.ToString();
Series series = chart1.Series[0];
series.ChartType = SeriesChartType.Spline;
this.chart1.ChartAreas[0].AxisY.Maximum = 10;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
this.chart1.ChartAreas[0].AxisX.ScaleView.Size = 5;
this.chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
this.chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
this.chart1.Series[0].Color = Color.Red;
}
private void timer1_Tick(object sender, EventArgs e)
{
//this.chart1.Series[0].Points.Clear();
string con, sql;
con = "Server=.;Database=newListen;Trusted_Connection=SSPI";
SqlConnection mycon = new SqlConnection(con);
mycon.Open();
sql = "select * from receive_data";
SqlDataAdapter myda = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
DataSet myds = new DataSet();
myda.Fill(dt);
myda.Fill(myds, "receive_data");
Series series = chart1.Series[0];
foreach (DataRow row in dt.Rows)
{
List<short> www = new List<short>();
short point = short.Parse(row["net1_receive_count"].ToString());
series.Points.AddXY(DateTime.Now, point);
www.Add(point);
aaa++;
if (aaa == www.Count)
{
textBox1.Text = aaa.ToString() + www.Count.ToString();
this.timer1.Stop();
}
}
this.chart1.ChartAreas[0].AxisX.ScaleView.Position = series.Points.Count - 6;
//throw new NotImplementedException();
}
private void chart1_Click(object sender, EventArgs e)
{
}
private void rb1_CheckedChanged(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
}
}