C# 设置Chart的X轴为时间轴​​​​​​​详情

作者:IC00 时间:2022-03-14 12:48:30 

前言

将Chart的X轴设置为时间轴是一个说简单不简单的问题,说难也不难的问题,你用过之后呢就感觉很容易,你没用过呢,就比较难,所以这个是很值得我们学习的,我看了别的社区也没有具体讲,所以我想分享一下,万一我自己忘记了,也可以翻这篇文章去复习,我们一起来学习一下吧,虽然这篇文章比较简单,也是值得学习的,创作不易,点赞关注评论收藏,谢谢大家啦!!!

界面设计

对界面的设计,使用timer定时器按照每秒循环生成随机数添加进Chart里面,使我们实现每秒添加值,形成曲线运动,开始按钮是对于定时器的控制,然后我们将代码复制到我们的项目中对于chart的折线图设置,就可以实现时间轴为X轴。注意:虽然我们是有AddXY的方法,但是如果你不设置X轴就会出现你添加的时间是有问题的,另外,如果我们要把自己的时间添加进去需要对时间进行.ToOADate()操作才可以。

C# 设置Chart的X轴为时间轴​​​​​​​详情

C# 设置Chart的X轴为时间轴​​​​​​​详情

效果展示

效果展示就是下图,最下面的是启用了系统滚动条,样子有点丑

C# 设置Chart的X轴为时间轴​​​​​​​详情

C# 设置Chart的X轴为时间轴​​​​​​​详情

如果你的时间不是采用系统时间添加,可能会出现添加时间不进去或者出现错误,如果你想添加你自己的时间需要对这个时间进行“.ToOADate()”操作,datetime.AddSeconds(1).ToOADate(),假设datetime是你的时间,AddSeconds(1)是代表在你的datetime的基础上加一秒,.ToOADate()这是自动化日期,类似时间戳,datetime.ToOADate(),也可直接这样不用加一秒,就是你的那个时间,注意一定要加.ToOADate()!!!!

代码逻辑

复制那段对于chart的折线图的设置,直接用我都有备注,注意:里面有条语句chart1.ChartAreas[0].AxisX.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last)是关于滚动条视图的,就是说我们的数据可以一直往前跑我们可以看得到,实现数据滚动, 但是建议跟着我那个if一起用避免报错,代码如下,你们可以直接复制。

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 IC00test823
{
? ?public partial class Form1 : Form
?  {
? ? ? ?public Form1()
? ? ?  {
? ? ? ? ? ?InitializeComponent();
? ? ?  }
?
? ? ? ?private void Form1_Load(object sender, EventArgs e)
? ? ?  {
? ? ? ? ? ?chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Size = 10;//x坐标显示的个数------------控制这个数量的大小进行缩放 ? ?
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;//设置x轴间隔值单位:秒
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//设置X轴的值的间隔大小
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = false;//是否在轴末尾显示标记
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";//设置X轴的数据样式
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Seconds;
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScaleView.SizeType = DateTimeIntervalType.Seconds; //度量单位
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 1;
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSizeType = DateTimeIntervalType.Seconds;
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.Enabled = AxisEnabled.True;//将X轴始终展示
? ? ? ? ? ?chart1.ChartAreas[0].AxisY.Enabled = AxisEnabled.True;//将Y轴始终展示
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;//设置X轴网格线颜色
? ? ? ? ? ?chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;//设置Y轴网格线颜色
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;//关闭系统的滚动条,也可以不关闭,就可以滑动
? ? ? ? ? ?chart1.Series[0].BorderWidth = 2;//线宽
? ? ?  }
? ? ? ?private void button1_Click(object sender, EventArgs e)
? ? ?  {
? ? ? ? ? ?timer1.Enabled = !timer1.Enabled;
? ? ?  }
? ? ? ?private void timer1_Tick(object sender, EventArgs e)
? ? ?  {
? ? ? ? ? ?Random random = new Random();
? ? ? ? ? ?chart1.Series[0].Points.AddXY(DateTime.Now,random.Next(1,20));
? ? ? ? ? ?if(chart1.ChartAreas[0].AxisX.ScaleView.Size>0)
? ? ? ? ?  {
? ? ? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);
? ? ? ? ?  }
? ? ?  }
?  }
}

来源:https://juejin.cn/post/7135810520640126990

标签:C#,Chart,时间轴​​​​​​​
0
投稿

猜你喜欢

  • Android递归方式删除某文件夹下的所有文件(.mp3文件等等)

    2022-06-05 06:43:46
  • 解决Android从相册中获取图片出错图片却无法裁剪问题的方法

    2023-09-14 23:41:15
  • java 优雅关闭线程池的方案

    2022-03-20 23:05:44
  • javassist使用指南

    2023-03-04 17:03:00
  • Java集合遍历实现方法及泛型通配

    2022-02-26 13:55:54
  • 在service层注入mapper时报空指针的解决

    2021-09-07 03:39:21
  • C# 添加对System.Configuration.dll文件的引用操作

    2022-03-05 22:20:31
  • spring boot实战之本地jar包引用示例

    2021-11-01 20:44:45
  • C# 中 System.Index 结构体和 Hat 运算符(^)的使用示例

    2023-02-27 11:40:23
  • C#中TextBox的横线样式及占位提示详解

    2023-05-17 10:33:27
  • Java编程语言特性和优势

    2021-11-14 06:54:27
  • IDEA项目maven project没有出现plugins和Dependencies问题

    2021-08-08 10:59:53
  • Java 8中的18个常用日期处理(收藏)

    2023-03-02 02:01:54
  • android的RecyclerView实现拖拽排序和侧滑删除示例

    2023-10-06 01:33:32
  • C#算法之回文数

    2022-06-26 20:02:38
  • android和服务器的URLEncodedUtils乱码编码问题的解决方案

    2021-11-19 08:13:12
  • 探讨:如何使用委托,匿名方法对集合进行万能排序

    2022-06-15 02:17:23
  • spring整合JMS实现同步收发消息(基于ActiveMQ的实现)

    2022-06-09 06:00:36
  • 关于RedisTemplate之opsForValue的使用说明

    2023-07-09 16:53:04
  • SpringSceurity实现短信验证码登陆

    2023-06-23 00:37:35
  • asp之家 软件编程 m.aspxhome.com