using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace ChartControl
{
public partial class Form1 : Form
{
/// <summary>
/// 图表对象
/// </summary>
Series series;
/// <summary>
/// X轴最大点数
/// </summary>
int xValue = 60;
/// <summary>
/// 开始时间
/// </summary>
DateTime sDate;
/// <summary>
/// 随机数
/// </summary>
Random rand = new Random();
//增加队列
Queue<double> thinkness = new Queue<double>(30);
Queue<double> recordTime = new Queue<double>(30);
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化Chart
/// </summary>
public void InitChart()
{
series = chart1.Series[0];
//清空原先数据缓存
series.Points.Clear();
//定义图表尺寸
chart1.Width = Width - 90;
chart1.Height = Height - 110;
//以下按照先绘制chartArea,然后再绘制Series的步骤画图
//chartArea背景颜色
chart1.BackColor = Color.Azure;
//X轴设置
chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:00:02").Second;
//Y轴设置
chart1.ChartAreas[0].AxisY.Title = "数据点";
chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Center;
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = true;//显示横着的分割线
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 90;
chart1.ChartAreas[0].AxisY.Interval = 20;
//Series绘制
series.ChartType = SeriesChartType.Spline;
series.XValueType = ChartValueType.DateTime;
series.IsValueShownAsLabel = true;//显示数据点的值
series.MarkerStyle = MarkerStyle.Diamond;//显示标记,菱形
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void UpdateChartData()
{
if (thinkness.Count > 60)
{
for (int i = 0; i < 2; i++)
{
thinkness.Dequeue();
recordTime.Dequeue();
}
}
else
{
thinkness.Enqueue(rand.Next(20, 70));
recordTime.Enqueue(DateTime.Now.ToOADate());
}
}
private void ReferenceLine(double refernceValue, Chart charContor)
{
/* 绘制参考线*/
double max = refernceValue;
StripLine stripMax = new StripLine();
stripMax.Text = string.Format("最大:{0:F}", max);//展示文本
stripMax.BackColor = Color.Yellow;//背景色
stripMax.Interval = 0;//间隔
stripMax.IntervalOffset = max;//偏移量
stripMax.StripWidth = 0.001;//线宽
stripMax.ForeColor = Color.White;//前景色
stripMax.TextAlignment = StringAlignment.Near;//文本对齐方式
charContor.ChartAreas[0].AxisY.StripLines.Add(stripMax);//添加到ChartAreas中
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
ReferenceLine(Convert.ToDouble(textBox1.Text.Trim()), chart1);
}
private void Form1_Load(object sender, EventArgs e)
{
InitChart();
timer1.Interval = 1000;
}
private void timer1_Tick(object sender, EventArgs e)
{
//获取实时数据最近的60个数据
UpdateChartData();
//重新绘制曲折线图
this.chart1.Series[0].Points.Clear();
this.chart1.Series[0].Points.DataBindXY(recordTime, thinkness);
}
}
}
Chart
最新推荐文章于 2024-07-18 16:58:58 发布