本文主要是介绍c# 画散点图——大量点4万个--动态刷新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码如下,用的devexpress chartcontrol,代码如下:
public partial class Form1 : Form{public Form1(){InitializeComponent();}int row = 0;//点数int value = 0;//数值Random rd = new Random();//随机值const int nnnn = 34;// 创建一个点图ChartControl pointChart = new ChartControl();DevExpress.XtraCharts.XYDiagram xyDiagram1 = new DevExpress.XtraCharts.XYDiagram();List<Series> ls = new List<Series>();private void addPoint(object source, System.Timers.ElapsedEventArgs e){try{lock(pointChart){BeginInvoke((Action)(() => {if (pointChart.Series.Count > nnnn - 1 && pointChart.Series[0].View.GetType().Equals(typeof(PointSeriesView))){for (int j = 0; j < nnnn; j++){pointChart.Series[j].DataSource = GenerateCluster(rd, 140, 1280, 100, 1240, 1024);}foreach (Series series in pointChart.Series){series.ArgumentDataMember = "Argument";series.ValueDataMembers.AddRange("Value");}}}));}}catch (Exception ex){MessageBox.Show(ex.ToString());}}public static SimpleDataPoint[] GenerateCluster(Random random, int xPlus, int xMinus, int yPlus, int yMinus, int count){SimpleDataPoint[] seriesPoints = new SimpleDataPoint[count];int deltaX = xMinus - xPlus;int deltaY = yMinus - yPlus;int centerX = xMinus / 2 + xPlus / 2;int centerY = yMinus / 2 + yPlus / 2;for (int i = 0; i < count; i++){int half = i / 2 + 1;double ratio = Math.Max(2.1, (double)count / half);int xOffset = (int)(deltaX / ratio);int yOffset = (int)(deltaY / ratio);int delta = xMinus - xOffset - centerX;int rx, ry;do{rx = random.Next(xPlus + xOffset, xMinus - xOffset);ry = random.Next(yPlus + yOffset, yMinus - yOffset);}while (delta * delta < Math.Pow((centerX - rx), 2) + Math.Pow((centerY - ry), 2));seriesPoints[i] = new SimpleDataPoint(rx, ry);}return seriesPoints;}private void Form1_Load(object sender, EventArgs e){for(int i = 0; i< nnnn; i++){string name = "series " + (i+1);// 创建一条曲线Series series1 = new Series(name, ViewType.Point);//设置序列的数字参数比例类型,默认情况下是定性的。series1.ArgumentScaleType = ScaleType.Numerical;//将线条加入到表中//pointChart.Series.Add(series1);ls.Add(series1);//访问该系列的特定于视图类型的选项。PointSeriesView myView1 = (PointSeriesView)series1.View;myView1.PointMarkerOptions.Kind = MarkerKind.Circle;//myView1.PointMarkerOptions.StarPointCount = 5;myView1.PointMarkerOptions.Size = 4;myView1.PointMarkerOptions.BorderColor = Color.FromArgb(i*10);//点的颜色}this.pointChart.SeriesSerializable = ls.ToArray();//创建上下限线条//XYDiagram diagram = (XYDiagram)pointChart.Diagram;//diagram.DefaultPane.BackColor = Color.LemonChiffon;//背景颜色//diagram.AxisY.ConstantLines.Clear();//ConstantLine constantLine1 = new ConstantLine("上限", 30);//constantLine1.Color = Color.Black;//直线颜色//constantLine1.Title.TextColor = Color.Black;//直线文本字体颜色 //diagram.AxisY.ConstantLines.Add(constantLine1);//ConstantLine constantLine2 = new ConstantLine("下限", 20);//constantLine2.Color = Color.Black;//constantLine2.Title.TextColor = Color.Black;//diagram.AxisY.ConstantLines.Add(constantLine2);//隐藏图例(如有必要)。//pointChart.Legend.Visible = true;pointChart.Legend.Visibility = DefaultBoolean.True;pointChart.SeriesTemplate.LabelsVisibility = DefaultBoolean.True;pointChart.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Left;pointChart.Legend.AlignmentVertical = LegendAlignmentVertical.Top;pointChart.Legend.Direction = LegendDirection.LeftToRight;this.pointChart.Legend.BackColor = System.Drawing.Color.Transparent;this.pointChart.Legend.Border.Visibility = DevExpress.Utils.DefaultBoolean.False;this.pointChart.BorderOptions.Visibility = DevExpress.Utils.DefaultBoolean.False;this.pointChart.CrosshairOptions.CrosshairLabelMode = DevExpress.XtraCharts.CrosshairLabelMode.ShowForNearestSeries;this.pointChart.CrosshairOptions.HighlightPoints = true;this.pointChart.CrosshairOptions.LinesMode = DevExpress.XtraCharts.CrosshairLinesMode.Auto;this.pointChart.CrosshairOptions.ShowArgumentLabels = true;this.pointChart.CrosshairOptions.ShowCrosshairLabels = true;this.pointChart.CrosshairOptions.ShowValueLabels = true;this.pointChart.CrosshairOptions.ShowGroupHeaders = true;this.pointChart.CrosshairOptions.ShowValueLine = true;//设置Y轴最小值和最大值,即默认情况下Y轴显示的范围AxisRange DIA = (AxisRange)((XYDiagram)pointChart.Diagram).AxisX.Range;DIA.SetMinMaxValues(0, 1024);//将图表添加到界面。pointChart.Dock = DockStyle.Fill;this.Controls.Add(pointChart);//定时器System.Timers.Timer t = new System.Timers.Timer(1000);//实例化Timer类,设置间隔时间为1000毫秒;t.Elapsed += new System.Timers.ElapsedEventHandler(addPoint);//到达时间的时候执行事件;t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;t.Start();//启动}}public class SimpleDataPoint{public double Argument { get; private set; }public double Value { get; private set; }public SimpleDataPoint(double arg, double val){Argument = arg;Value = val;}}
效果图如下:
这篇关于c# 画散点图——大量点4万个--动态刷新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!