最近做的一个项目里用到这部分 打算记录下来 挖个坑
已填
Chart坐标轴横轴为时间,纵轴是数值
如果只是一次性绘图,那么遍历一遍数据即可
如果想连续绘制(比如按照时间更新绘制),就需要一个Timer控件来更新绘图的数据。
以下为项目代码:
GUI界面添加一个Chart和一个timer即可
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 chartForTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Init_Chart();//初始化chart组件有关属性
timer1.Interval = 1000;//timer1用来更新图像,每秒增加一个点
timer1.Start();//窗口初始化完成即开始绘制
}
private void Init_Chart()
{
#region chart1
chart1.Series[0].IsValueShownAsLabel = true;//让点集0在图像上显示数值
chart1.Series[0].SmartLabelStyle.Enabled = true;
chart1.Series[0].XValueType = ChartValueType.DateTime;//坐标轴type改为时间
//去掉滚动条的按钮
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.None;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = false;
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 20;
chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.ScaleView.Size = 20;
chart1.ChartAreas[0].AxisX.ScaleView.MinSize = 15;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;
chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
//这个interval可以用来修改显示间隔
chart1.ChartAreas[0].AxisX.Interval = DateTime.Parse("00:00:01").Second;
chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Near;
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;