網(wǎng)站首頁 編程語言 正文
前言
將Chart的X軸設置為時間軸是一個說簡單不簡單的問題,說難也不難的問題,你用過之后呢就感覺很容易,你沒用過呢,就比較難,所以這個是很值得我們學習的,我看了別的社區(qū)也沒有具體講,所以我想分享一下,萬一我自己忘記了,也可以翻這篇文章去復習,我們一起來學習一下吧,雖然這篇文章比較簡單,也是值得學習的,創(chuàng)作不易,點贊關注評論收藏,謝謝大家啦!!!
界面設計
對界面的設計,使用timer定時器按照每秒循環(huán)生成隨機數(shù)添加進Chart里面,使我們實現(xiàn)每秒添加值,形成曲線運動,開始按鈕是對于定時器的控制,然后我們將代碼復制到我們的項目中對于chart的折線圖設置,就可以實現(xiàn)時間軸為X軸。注意:雖然我們是有AddXY的方法,但是如果你不設置X軸就會出現(xiàn)你添加的時間是有問題的,另外,如果我們要把自己的時間添加進去需要對時間進行.ToOADate()操作才可以。
效果展示
效果展示就是下圖,最下面的是啟用了系統(tǒng)滾動條,樣子有點丑
如果你的時間不是采用系統(tǒng)時間添加,可能會出現(xiàn)添加時間不進去或者出現(xiàn)錯誤,如果你想添加你自己的時間需要對這個時間進行“.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)是關于滾動條視圖的,就是說我們的數(shù)據(jù)可以一直往前跑我們可以看得到,實現(xiàn)數(shù)據(jù)滾動, 但是建議跟著我那個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坐標顯示的個數(shù)------------控制這個數(shù)量的大小進行縮放 ? ?
? ? ? ? ? ?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軸的數(shù)據(jù)樣式
? ? ? ? ? ?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軸網(wǎng)格線顏色
? ? ? ? ? ?chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;//設置Y軸網(wǎng)格線顏色
? ? ? ? ? ?chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = false;//關閉系統(tǒng)的滾動條,也可以不關閉,就可以滑動
? ? ? ? ? ?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
相關推薦
- 2022-04-08 iOS開發(fā)實現(xiàn)簡單計算器功能_IOS
- 2022-11-04 一文理解Redux及其工作原理_React
- 2022-07-22 git倉庫的第一次上傳以及修改上傳項目
- 2023-03-27 Python?tkinter中l(wèi)abel控件動態(tài)改變值問題_python
- 2022-05-25 Maven打包時排除指定的目錄或指定的類
- 2022-08-15 使用element中el-table設置type=“expand“展開行隱藏小箭頭的方法(列表單選、
- 2023-11-15 Latex文獻報錯 Something‘s wrong--perhaps a missing \it
- 2022-04-09 使用docker-compose一鍵部署開源博客wordpress
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支