WinForm Chart 图表的使用

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET Framework 4.8

预览

image

代码

可以通过设置Chart组件Series集合属性内的ChartType属性来调整图表样式。

  1. Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;
    
    namespace Chart控件
    {
    	public partial class Form1 : Form
    	{
    
    		private Random randoms = new Random();
    
    		public Form1()
    		{
    			InitializeComponent();
    			var charts = new List<Chart>()
    			{
    				chart1,
    				chart2,
    				chart3,
    				chart4,
    				chart5,
    				chart6,
    				chart7,
    				chart8,
    				chart9,
    			};
    
    			for (int i = 0; i < charts.Count(); i++)
    			{
    				List<int> XList = new List<int>();
    				List<int> YList = new List<int>();
    				for (int i1 = 0; i1 < 6; i1++)
    				{
    					XList.Add(i1);
    					YList.Add(randoms.Next(10, 100));
    				}
    				charts[i].Series["Series1"].Points.DataBindXY(XList, YList);
    			}
    			Task task = new Task(() =>
    			{
    				while (true)
    				{
    					Thread.Sleep(200);
    					for (int i = 0; i < charts.Count(); i++)
    					{
    						List<int> XList = new List<int>();
    						List<int> YList = new List<int>();
    						for (int i1 = 0; i1 < 6; i1++)
    						{
    							XList.Add(i1);
    							YList.Add(randoms.Next(10, 100));
    						}
    						this.Invoke(new Action(() =>
    						{
    							charts[i].Series["Series1"].Points.RemoveAt(0);
    							charts[i].Series["Series1"].Points.DataBindXY(XList, YList);
    						}));
    
    					}
    				}
    			});
    			task.Start();
    		}
    	}
    }
    
    
  2. Form1.Designer.cs
    namespace Chart控件
    {
    	partial class Form1
    	{
    		/// <summary>
    		/// 必需的设计器变量。
    		/// </summary>
    		private System.ComponentModel.IContainer components = null;
    
    		/// <summary>
    		/// 清理所有正在使用的资源。
    		/// </summary>
    		/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    		protected override void Dispose(bool disposing)
    		{
    			if (disposing && (components != null))
    			{
    				components.Dispose();
    			}
    			base.Dispose(disposing);
    		}
    
    		#region Windows 窗体设计器生成的代码
    
    		/// <summary>
    		/// 设计器支持所需的方法 - 不要修改
    		/// 使用代码编辑器修改此方法的内容。
    		/// </summary>
    		private void InitializeComponent()
    		{
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea9 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend9 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series9 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea8 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend8 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series8 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea7 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend7 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series7 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea6 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend6 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series6 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea5 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend5 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series5 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea4 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend4 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series4 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea3 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend3 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series3 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea2 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend2 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    			System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    			System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
    			this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
    			this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart2 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart3 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart4 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart5 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart6 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart7 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart8 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.chart9 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    			this.tableLayoutPanel1.SuspendLayout();
    			((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart2)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart3)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart4)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart5)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart6)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart7)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart8)).BeginInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart9)).BeginInit();
    			this.SuspendLayout();
    			// 
    			// tableLayoutPanel1
    			// 
    			this.tableLayoutPanel1.ColumnCount = 3;
    			this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
    			this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
    			this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33334F));
    			this.tableLayoutPanel1.Controls.Add(this.chart9, 2, 2);
    			this.tableLayoutPanel1.Controls.Add(this.chart8, 1, 2);
    			this.tableLayoutPanel1.Controls.Add(this.chart7, 0, 2);
    			this.tableLayoutPanel1.Controls.Add(this.chart6, 2, 1);
    			this.tableLayoutPanel1.Controls.Add(this.chart5, 1, 1);
    			this.tableLayoutPanel1.Controls.Add(this.chart4, 0, 1);
    			this.tableLayoutPanel1.Controls.Add(this.chart3, 2, 0);
    			this.tableLayoutPanel1.Controls.Add(this.chart2, 1, 0);
    			this.tableLayoutPanel1.Controls.Add(this.chart1, 0, 0);
    			this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
    			this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
    			this.tableLayoutPanel1.Name = "tableLayoutPanel1";
    			this.tableLayoutPanel1.RowCount = 3;
    			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
    			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
    			this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
    			this.tableLayoutPanel1.Size = new System.Drawing.Size(1082, 751);
    			this.tableLayoutPanel1.TabIndex = 0;
    			// 
    			// chart1
    			// 
    			chartArea9.Name = "ChartArea1";
    			this.chart1.ChartAreas.Add(chartArea9);
    			this.chart1.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend9.Name = "Legend1";
    			this.chart1.Legends.Add(legend9);
    			this.chart1.Location = new System.Drawing.Point(3, 3);
    			this.chart1.Name = "chart1";
    			series9.ChartArea = "ChartArea1";
    			series9.Legend = "Legend1";
    			series9.Name = "Series1";
    			this.chart1.Series.Add(series9);
    			this.chart1.Size = new System.Drawing.Size(354, 244);
    			this.chart1.TabIndex = 0;
    			this.chart1.Text = "chart1";
    			// 
    			// chart2
    			// 
    			chartArea8.Name = "ChartArea1";
    			this.chart2.ChartAreas.Add(chartArea8);
    			this.chart2.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend8.Name = "Legend1";
    			this.chart2.Legends.Add(legend8);
    			this.chart2.Location = new System.Drawing.Point(363, 3);
    			this.chart2.Name = "chart2";
    			series8.ChartArea = "ChartArea1";
    			series8.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
    			series8.Legend = "Legend1";
    			series8.Name = "Series1";
    			this.chart2.Series.Add(series8);
    			this.chart2.Size = new System.Drawing.Size(354, 244);
    			this.chart2.TabIndex = 1;
    			this.chart2.Text = "chart2";
    			// 
    			// chart3
    			// 
    			chartArea7.Name = "ChartArea1";
    			this.chart3.ChartAreas.Add(chartArea7);
    			this.chart3.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend7.Name = "Legend1";
    			this.chart3.Legends.Add(legend7);
    			this.chart3.Location = new System.Drawing.Point(723, 3);
    			this.chart3.Name = "chart3";
    			series7.ChartArea = "ChartArea1";
    			series7.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bubble;
    			series7.Legend = "Legend1";
    			series7.Name = "Series1";
    			series7.YValuesPerPoint = 2;
    			this.chart3.Series.Add(series7);
    			this.chart3.Size = new System.Drawing.Size(356, 244);
    			this.chart3.TabIndex = 2;
    			this.chart3.Text = "chart3";
    			// 
    			// chart4
    			// 
    			chartArea6.Name = "ChartArea1";
    			this.chart4.ChartAreas.Add(chartArea6);
    			this.chart4.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend6.Name = "Legend1";
    			this.chart4.Legends.Add(legend6);
    			this.chart4.Location = new System.Drawing.Point(3, 253);
    			this.chart4.Name = "chart4";
    			series6.ChartArea = "ChartArea1";
    			series6.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
    			series6.Legend = "Legend1";
    			series6.Name = "Series1";
    			this.chart4.Series.Add(series6);
    			this.chart4.Size = new System.Drawing.Size(354, 244);
    			this.chart4.TabIndex = 3;
    			this.chart4.Text = "chart4";
    			// 
    			// chart5
    			// 
    			chartArea5.Name = "ChartArea1";
    			this.chart5.ChartAreas.Add(chartArea5);
    			this.chart5.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend5.Name = "Legend1";
    			this.chart5.Legends.Add(legend5);
    			this.chart5.Location = new System.Drawing.Point(363, 253);
    			this.chart5.Name = "chart5";
    			series5.ChartArea = "ChartArea1";
    			series5.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea;
    			series5.Legend = "Legend1";
    			series5.Name = "Series1";
    			this.chart5.Series.Add(series5);
    			this.chart5.Size = new System.Drawing.Size(354, 244);
    			this.chart5.TabIndex = 4;
    			this.chart5.Text = "chart5";
    			// 
    			// chart6
    			// 
    			chartArea4.Name = "ChartArea1";
    			this.chart6.ChartAreas.Add(chartArea4);
    			this.chart6.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend4.Name = "Legend1";
    			this.chart6.Legends.Add(legend4);
    			this.chart6.Location = new System.Drawing.Point(723, 253);
    			this.chart6.Name = "chart6";
    			series4.ChartArea = "ChartArea1";
    			series4.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
    			series4.Legend = "Legend1";
    			series4.Name = "Series1";
    			this.chart6.Series.Add(series4);
    			this.chart6.Size = new System.Drawing.Size(356, 244);
    			this.chart6.TabIndex = 5;
    			this.chart6.Text = "chart6";
    			// 
    			// chart7
    			// 
    			chartArea3.Name = "ChartArea1";
    			this.chart7.ChartAreas.Add(chartArea3);
    			this.chart7.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend3.Name = "Legend1";
    			this.chart7.Legends.Add(legend3);
    			this.chart7.Location = new System.Drawing.Point(3, 503);
    			this.chart7.Name = "chart7";
    			series3.ChartArea = "ChartArea1";
    			series3.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pyramid;
    			series3.Legend = "Legend1";
    			series3.Name = "Series1";
    			this.chart7.Series.Add(series3);
    			this.chart7.Size = new System.Drawing.Size(354, 245);
    			this.chart7.TabIndex = 6;
    			this.chart7.Text = "chart7";
    			// 
    			// chart8
    			// 
    			chartArea2.Name = "ChartArea1";
    			this.chart8.ChartAreas.Add(chartArea2);
    			this.chart8.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend2.Name = "Legend1";
    			this.chart8.Legends.Add(legend2);
    			this.chart8.Location = new System.Drawing.Point(363, 503);
    			this.chart8.Name = "chart8";
    			series2.ChartArea = "ChartArea1";
    			series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100;
    			series2.Legend = "Legend1";
    			series2.Name = "Series1";
    			this.chart8.Series.Add(series2);
    			this.chart8.Size = new System.Drawing.Size(354, 245);
    			this.chart8.TabIndex = 7;
    			this.chart8.Text = "chart8";
    			// 
    			// chart9
    			// 
    			chartArea1.Name = "ChartArea1";
    			this.chart9.ChartAreas.Add(chartArea1);
    			this.chart9.Dock = System.Windows.Forms.DockStyle.Fill;
    			legend1.Name = "Legend1";
    			this.chart9.Legends.Add(legend1);
    			this.chart9.Location = new System.Drawing.Point(723, 503);
    			this.chart9.Name = "chart9";
    			series1.ChartArea = "ChartArea1";
    			series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.PointAndFigure;
    			series1.Legend = "Legend1";
    			series1.Name = "Series1";
    			series1.YValuesPerPoint = 4;
    			this.chart9.Series.Add(series1);
    			this.chart9.Size = new System.Drawing.Size(356, 245);
    			this.chart9.TabIndex = 8;
    			this.chart9.Text = "chart9";
    			// 
    			// Form1
    			// 
    			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    			this.ClientSize = new System.Drawing.Size(1082, 751);
    			this.Controls.Add(this.tableLayoutPanel1);
    			this.Name = "Form1";
    			this.Text = "Form1";
    			this.tableLayoutPanel1.ResumeLayout(false);
    			((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart2)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart3)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart4)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart5)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart6)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart7)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart8)).EndInit();
    			((System.ComponentModel.ISupportInitialize)(this.chart9)).EndInit();
    			this.ResumeLayout(false);
    
    		}
    
    		#endregion
    
    		private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart6;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart5;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart4;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart3;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart2;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart9;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart8;
    		private System.Windows.Forms.DataVisualization.Charting.Chart chart7;
    	}
    }
    
    
    
posted @ 2025-05-19 22:20  夏秋初  阅读(118)  评论(0)    收藏  举报