程式碼:
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace Sample { public partial class Form1 : Form { private Chart _chart; private Thread _thread; private Series _series1 = new Series("第一條線", 1000); public Form1() { InitializeComponent(); Init(); } private void Init() { _chart = chart1; //標題 chart1.Titles.Add("標題"); chart1.Series.Add(_series1); //設定線條顏色 _series1.Color = Color.Blue; //設定字型 _series1.Font = new System.Drawing.Font("新細明體", 14); //折線圖 _series1.ChartType = SeriesChartType.Line; //將數值顯示在線上 _series1.IsValueShownAsLabel = true; } private void button1_Click(object sender, EventArgs e) { _thread = new Thread(new ThreadStart(Run)); _thread.Start(); } private void Run() { int index = 0; while (true) { _chart.Invoke(new MethodInvoker(delegate() { _chart.Series[0].Points.AddXY(index, index * 2); index++; })); Thread.Sleep(1000); } } } }
執行結果: