c#我用chart读取数据库
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();
}
}
}
或者当数据库点读取完了让他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();
}
}
}
...全文
相关推荐
从数据库读取数据到坐标轴为时间的chart显示
C#子线程每隔200ms保存数据到access数据库,然后读取数据库里面的某个字段全部数据和时间数据,以时间为X坐标轴显示到chart波形图中
c#柱状图 vs2008
vs2008做的柱状图 直接填的数据 从数据库获取可以参照着弄 很简单的 看例子就知道了
c#_ms_chart_控件使用方法(附统计图).docx
c#_ms_chart_控件使用方法(附统计图).docx
ERP系统中几个好用的公共类
ERP系统中几个好用的公共类,包含用于连接和操作数据库的DataBase类,用来实现控件绑定到数据库、键盘输入验证、生成单据编号等功能的CommonUse类,用于画统计图的的Char类以及用于读取ini文件用的OperatorFile类等
portal-chart-api:无服务器聊天室应用
portal-chart-api:无服务器聊天室应用


