C# winform 如何在创建PlotView实例plotView = new PlotView后,每秒钟绘制5000个数据点,如何根据数据点大小不同 其上绘制数据点的颜色也不同? 示例代码
时间: 2024-10-22 12:11:53 浏览: 25
C#WinForm 198个经典实例源码(超赞),C#初学者神器
在C# WinForm中,使用`PlotView`实例绘制数据点并设置不同颜色,可以通过以下步骤实现:
1. 创建`PlotView`实例。
2. 每秒钟更新5000个数据点。
3. 根据数据点大小设置不同的颜色。
示例代码如下:
```csharp
using System;
using System.Drawing;
using System.Windows.Forms;
using OxyPlot;
using OxyPlot.Series;
using OxyPlot.WindowsForms;
public class MainForm : Form
{
private PlotView plotView;
private Random random = new Random();
public MainForm()
{
plotView = new PlotView
{
Dock = DockStyle.Fill,
};
this.Controls.Add(plotView);
var model = new PlotModel { Title = "Dynamic Data Points" };
plotView.Model = model;
var series = new ScatterSeries { MarkerType = MarkerType.Circle };
model.Series.Add(series);
Timer timer = new Timer();
timer.Interval = 1000; // 每秒钟触发一次
timer.Tick += (sender, e) => UpdateDataPoints(series);
timer.Start();
}
private void UpdateDataPoints(ScatterSeries series)
{
series.Points.Clear();
for (int i = 0; i < 5000; i++)
{
double x = random.NextDouble() * 100;
double y = random.NextDouble() * 100;
double size = random.NextDouble() * 10; // 随机大小
Color color = GetColorBasedOnSize(size); // 根据大小获取颜色
series.Points.Add(new ScatterPoint(x, y, size: size, color: color));
}
}
private Color GetColorBasedOnSize(double size)
{
if (size < 2) return Color.Red;
if (size < 5) return Color.Yellow;
if (size < 8) return Color.Green;
return Color.Blue;
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
```
阅读全文