1.效果图
2.源码比较简单直接贴了
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; using System.Threading; namespace TEST { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { DataColumn dctime = new DataColumn("尺寸", Type.GetType("System.String")); DataColumn dcCity = new DataColumn("个数", Type.GetType("System.String")); ReadDBInforAuto.Columns.Add(dctime); ReadDBInforAuto.Columns.Add(dcCity); ThreadStart ts = new ThreadStart(RunMain); Thread td = new Thread(ts); td.Start(); } Dictionary dicNum = new Dictionary(); List ListNum = new List(); void RunMain() { while (true) { double data =Math.Round( NormalDistribution()[0],1); if (ListNum.Contains(data)) { dicNum[data] += 1; } else { ListNum.Add(data); ListNum.Sort(); dicNum.Add(data,1); } DrawLine(); Thread.Sleep(50); } } public static double[] NormalDistribution() {
Random rand = new Random(); double[] y; double u1, u2, v1=0, v2=0, s = 0, z1=0, z2=0; while (s > 1 || s == 0) { u1 = rand.NextDouble(); u2 = rand.NextDouble(); v1 = 2 * u1 - 1; v2 = 2 * u2 - 1; s = v1 * v1 + v2 * v2; } z1 = Math.Sqrt(-2 * Math.Log(s) / s) * v1; z2 = Math.Sqrt(-2 * Math.Log(s) / s) * v2; y = new double[] { z1, z2 }; return y; //返回两个服从正态分布N(0,1)的随机数z0 和 z1 } DataTable ReadDBInforAuto = new DataTable(); void DrawLine() { BeginInvoke(new Action(() => { DataTable tableInfo = new DataTable(); DataColumn dctime = new DataColumn("尺寸", Type.GetType("System.String")); DataColumn dcOK = new DataColumn("个数", Type.GetType("System.String")); tableInfo.Columns.Add(dctime); tableInfo.Columns.Add(dcOK); foreach (double ke in ListNum) { DataRow dr1 = tableInfo.NewRow(); dr1["尺寸"] = ke; dr1["个数"] = dicNum[ke]; tableInfo.Rows.Add(dr1); } chart1.DataSource = tableInfo; chart1.ChartAreas[0].AxisX.Title = "尺寸"; chart1.ChartAreas[0].AxisY.Title = "个数"; //chart1.ChartAreas[0].AxisY.LabelStyle.Format = "0%"; // Set series members names for the X and Y values chart1.Series["个数"].XValueMember = "尺寸"; chart1.Series["个数"].YValueMembers = "个数"; // Data bind to the selected data source chart1.DataBind(); // Set series chart type chart1.Series["个数"].ChartType = SeriesChartType.StackedColumn;
chart1.Series["个数"].IsValueShownAsLabel = false;
// Enable X axis margin chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true; chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0; // Enable 3D, and show data point marker lines //chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true; chart1.Series["个数"]["ShowMarkerLines"] = "True"; chart1.Series["个数"].ToolTip = "个数:#VAL 尺寸:#AXISLABEL"; })); } } }