C#chart繪折線圖動態新增資料
阿新 • • 發佈:2018-12-17
C#入門基礎,實現chart控制元件動態新增資料
最終程式執行效果如圖
點選開始,將隨機生成的兩組資料新增進折線圖裡,chart1和chart2 只顯示10個數,超過10個數將第一個傳進來的數刪掉
chart3是將chart1和chart2 的所有資料顯示,兩條不同的Y軸。
勾選框可以選擇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.Windows.Forms; using System.Drawing.Drawing2D; using System.Windows.Forms.DataVisualization.Charting; namespace WindowsFormsApplication2 { public partial class Form1 : Form { private double dm = 0; private double vm = 0; //速度 private double ss, st = 0; //時間間隔 private List<double> VM = new List<double>(100); private List<double> DM = new List<double>(100); private Queue<double> Vm = new Queue<double>(100); private Queue<double> Dm = new Queue<double>(100); private bool Ready = false; private bool contrl = false; public Form1() { InitializeComponent(); } //--------距離監測圖表-----------// //入口引數: 無 // // // private void DistanceData_Line() { this.chart1.ChartAreas.Clear(); ChartArea chartarea = new ChartArea("距離監測"); this.chart1.ChartAreas.Add(chartarea); chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; chartarea.AxisX.ScrollBar.Enabled = false; //chartarea.AxisY.Minimum = 0; //chartarea.AxisY.Maximum = 10000; chartarea.AxisX.Interval = 5; chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; //chartarea.AxisX.ScrollBar.Enabled = false; //////指當前顯示的是第幾個 //chartarea.AxisX.ScaleView.Size = 10; //表頭 this.chart1.Titles.Clear(); this.chart1.Titles.Add("距離監測"); this.chart1.Titles[0].Text = "距離監測"; this.chart1.Titles[0].ForeColor = Color.Red; this.chart1.Titles[0].Font = new System.Drawing.Font("Microsoft Sans serif", 12f); //定義儲存和顯示點的容器 this.chart1.Series.Clear(); Series series = new Series("距離"); series.ChartType = SeriesChartType.Spline; series.ChartArea = "距離監測"; series.Color = Color.Yellow; series.MarkerBorderWidth = 2; series.MarkerSize = 4; series.MarkerStyle = MarkerStyle.Diamond; series.ToolTip = "距離" + "#VAL \r\n #AXISLABEL"; chart1.Series.Add(series); } //--------速度監測圖表-----------// //入口引數: 無 // private void VelocityData_Line() { //定義表區域 this.chart2.ChartAreas.Clear(); ChartArea chartarea = new ChartArea("速度監測"); this.chart2.ChartAreas.Add(chartarea); chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; chartarea.AxisX.ScrollBar.Enabled = false; //chartarea.AxisY.Minimum = 0; //chartarea.AxisY.Maximum = 100; chartarea.AxisX.Interval = 5; chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; //chartarea.AxisX.ScrollBar.Enabled = false; ////指當前顯示的是第幾個 //chartarea.AxisX.ScaleView.Size = 10; //表頭 this.chart2.Titles.Clear(); this.chart2.Titles.Add("速度監測"); this.chart2.Titles[0].Text = "速度監測"; this.chart2.Titles[0].ForeColor = Color.Red; this.chart2.Titles[0].Font = new System.Drawing.Font("Microsoft Sans serif", 12f); //定義儲存和顯示點的容器 this.chart2.Series.Clear(); Series series = new Series("速度"); chart2.Series.Add(series); series.ChartArea = "速度監測"; series.ChartType = SeriesChartType.Spline; series.MarkerBorderWidth = 2; series.MarkerSize = 4; series.MarkerStyle = MarkerStyle.Diamond; series.ToolTip = "速度" + "#VAL \r\n #AXISLABEL"; } //--------飛行資料監測圖表-----------// //入口引數: 無 // private void FlyData_Line() { //定義表區域 this.chart3.ChartAreas.Clear(); ChartArea chartarea = new ChartArea("資料監測"); this.chart3.ChartAreas.Add(chartarea); chartarea.AxisX.Maximum = VM.Count; chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; chartarea.AxisX.ScrollBar.Enabled = false; //chartarea.AxisY.Minimum = 0; //chartarea.AxisY.Maximum = 100; chartarea.AxisX.Interval = 5; chartarea.AxisX.MajorGrid.LineColor = System.Drawing.Color.Transparent; chartarea.AxisY.MajorGrid.LineColor = System.Drawing.Color.Black; //chartarea.AxisX.ScrollBar.Enabled = false; ////指當前顯示的是第幾個 //chartarea.AxisX.ScaleView.Size = 10; //表頭 this.chart3.Titles.Clear(); this.chart3.Titles.Add("資料監測"); this.chart3.Titles[0].Text = "資料監測"; this.chart3.Titles[0].ForeColor = Color.Red; this.chart3.Titles[0].Font = new System.Drawing.Font("Microsoft Sans serif", 12f); //定義儲存和顯示點的容器 this.chart3.Series.Clear(); for (int i = 0; i < 2; i++) { Series series = new Series(DATATYPE[i]); chart3.Series.Add(series); series.ChartArea = "資料監測"; series.ChartType = SeriesChartType.Spline; series.MarkerBorderWidth = 2; series.MarkerSize = 4; series.MarkerStyle = MarkerStyle.Diamond; series.ToolTip = DATATYPE[i] + "#VAL \r\n #AXISLABEL"; } this.chart3.Series[0].YAxisType = AxisType.Primary; this.chart3.Series[1].YAxisType = AxisType.Secondary; } private static string[] DATATYPE = { "速度", "距離" }; //--------飛行軌跡資料匯入圖表------------------------- // //入口引數: // //距離: dM // //速度: vM // private void ChartData() { if (Vm.Count > 10)//當資料大於十,刪除第一位數 { Vm.Dequeue(); Dm.Dequeue(); } if (Ready == true) { VM.Add(vm); DM.Add(dm); Vm.Enqueue(vm); Dm.Enqueue(dm); } for (int i = 0; i < Vm.Count; i++) { this.chart1.Series[0].Points.AddY(Vm.ElementAt(i)); this.chart2.Series[0].Points.AddY(Dm.ElementAt(i)); } for (int i = 0; i < VM.Count; i++) { //指示速度是否顯示 if (checkedListBox1.GetItemChecked(0) == true) { this.chart3.Series[0].Points.AddY(VM.ElementAt(i)); } if (checkedListBox1.GetItemChecked(0) == false) { this.chart3.Series[0].Points.Clear(); } //指示距離是否顯示 if (checkedListBox1.GetItemChecked(1) == true) { this.chart3.Series[1].Points.AddY(DM.ElementAt(i)); } if (checkedListBox1.GetItemChecked(1) == false) { this.chart3.Series[1].Points.Clear(); } } } private void button1_Click(object sender, EventArgs e) { if (contrl == false) { Ready = true; button1.Text = "停止"; contrl = true; } else { Ready = false; button1.Text = "開始"; contrl = true; } } private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, true); } timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { Random rd = new Random(); vm = rd.Next(0, 100); dm = rd.Next(0, 500); FlyData_Line(); VelocityData_Line(); DistanceData_Line(); ChartData(); } } }