基本信息
源码名称:C# 实时折线图、波形图(DataVisualization.Charting)实例源码
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2024-07-10
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
DemoRealChart折线图
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using DemoRealChart;
namespace DemoSharp
{
public partial class RealChart : Form
{
private Queue<double> dataQueue = new Queue<double>(120);
private int curValue = 0;
private int num = 5;//每次删除/增加几个点
public RealChart()
{
InitializeComponent();
InitChart();
}
/// <summary>
/// 初始化事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInit_Click(object sender, EventArgs e)
{
// InitChart();
dataQueue.Clear();
}
/// <summary>
/// 开始事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{
this.timer1.Start();
}
/// <summary>
/// 停止事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
/// <summary>
/// 定时器事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
UpdateQueueValue();
this.chart1.Series[0].Points.Clear();
for (int i = 0; i < dataQueue.Count; i )
{
this.chart1.Series[0].Points.AddXY((i 1), dataQueue.ElementAt(i));
}
}
/// <summary>
/// 初始化图表
/// </summary>
private void InitChart()
{
//定义图表区域
this.chart1.ChartAreas.Clear();
ChartArea chartArea1 = new ChartArea("C1");
this.chart1.ChartAreas.Add(chartArea1);
//定义存储和显示点的容器
this.chart1.Series.Clear();
Series series1 = new Series("S1");
series1.ChartArea = "C1";
series1.IsValueShownAsLabel = true;//标签显示数据
series1.MarkerStyle = MarkerStyle.Circle;//显示圆形数据点
this.chart1.Series.Add(series1);
//设置图表显示样式
this.chart1.ChartAreas[0].AxisY.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Maximum = 100;
this.chart1.ChartAreas[0].AxisX.Minimum = 0;
//this.chart1.ChartAreas[0].AxisX.Maximum = 150;//设定X轴最多显示150个点位,不设置为无穷大
this.chart1.ChartAreas[0].AxisX.Interval = 5;
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.Silver;
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.Silver;
//设置标题
this.chart1.Titles.Clear();
this.chart1.Titles.Add("S01");
this.chart1.Titles[0].Text = "XXX显示";
this.chart1.Titles[0].ForeColor = Color.RoyalBlue;
this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
this.chart1.Series[0].XValueType = ChartValueType.Auto;//设置x轴显示数据
// chart1.ChartAreas[0].AxisX.LabelStyle.Format = "ss.ff";//毫秒格式:hh:mm:ss.fff,后面几个f则保留几位毫秒小数,此时要注意轴的最大值和最小值不要差太大
// chart1.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Milliseconds;
// chart1.ChartAreas[0].AxisX.MajorGrid.IntervalType = DateTimeIntervalType.Milliseconds;
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;//x轴启用滚动条
chart1.ChartAreas[0].AxisY.ScaleView.Size = 100;
// chart1.ChartAreas[0].AxisY.ScrollBar.Enabled = true;
//缩放滚动条
// chart1.ChartAreas["C1"].Area3DStyle.Enable3D =true;
chart1.ChartAreas["C1"].Area3DStyle.LightStyle = LightStyle.Realistic;
chart1.ChartAreas["C1"].BackColor = System.Drawing.Color.FromArgb(((System.Byte)(64)), ((System.Byte)(165)), ((System.Byte)(191)), ((System.Byte)(228)));
chart1.ChartAreas["C1"].CursorX.AutoScroll = true;
chart1.ChartAreas["C1"].AxisX.ScrollBar.Enabled = true;
chart1.ChartAreas["C1"].CursorX.IsUserEnabled = true;
chart1.ChartAreas["C1"].CursorX.IsUserSelectionEnabled = true;
chart1.ChartAreas["C1"].AxisX.ScaleView.Zoomable = true;//启用轴的缩放视图
chart1.ChartAreas["C1"].AxisX.ScaleView.Position = 1;
chart1.ChartAreas["C1"].AxisX.ScaleView.Size = 100;//设置启动时的x轴显示数据个数
chart1.ChartAreas["C1"].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
chart1.ChartAreas["C1"].AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;
//设置图表显示样式
this.chart1.Series[0].Color = Color.Red;
if (rb1.Checked)
{
this.chart1.Titles[0].Text = string.Format("XXX {0} 显示", rb1.Text);
this.chart1.Series[0].ChartType = SeriesChartType.Line;
}
if (rb2.Checked)
{
this.chart1.Titles[0].Text = string.Format("XXX {0} 显示", rb2.Text);
this.chart1.Series[0].ChartType = SeriesChartType.Spline;
}
this.chart1.Series[0].Points.Clear();
}
//更新队列中的值
private void UpdateQueueValue()
{
//if (dataQueue.Count > 100)
//{
// //先出列
// for (int i = 0; i < num; i )
// {
// dataQueue.Dequeue();
// }
//}
if (rb1.Checked)
{
Random r = new Random();
for (int i = 0; i < num; i )
{
dataQueue.Enqueue(r.Next(0, 100));
}
}
if (rb2.Checked)
{
for (int i = 0; i < num; i )
{
//对curValue只取[0,360]之间的值
curValue = curValue % 360;
//对得到的正玄值,放大50倍,并上移50
dataQueue.Enqueue((50 * Math.Sin(curValue * Math.PI / 180)) 50);
curValue = curValue 10;
}
}
}
private void RealChart_Load(object sender, EventArgs e)
{
}
}
}