如何在散点图中绘制超过 50,000 个值,从而节省计算机资源? [英] How can I plot more than 50,000 values in a scatter chart, saving computer resource?
问题描述
我正在使用 Visual Studio 2017 并尝试使用 C# 和 winform 制作一个在散点图中显示实时值的程序.
I'm using Visual Studio 2017 and am trying to make a program that shows real-time values in a scatter chart, using C# and winform.
使用下面的源代码,我能够让它显示实时值,每当事件发生并获得一个新值(每秒 3~5 次).
With the source code below, I was able to make it show real-time values, whenever an event occurs and it gets a new value(3~5 times a second).
valueArray 通过 GetRealTimeData 函数不断获取新值,图表显示数组中的所有元素.
valueArray continuously gets new values through GetRealTimeData function and the chart shows all the elements in the array.
valueArray[valueArray.Length - 1] = Convert.ToDouble(GetRealTimeData().Trim());
Array.Copy(valueArray, 1, valueArray, 0, valueArray.Length - 1);
this.chart1.Series["Series1"].Points.Clear();
this.chart1.Series["Series1"].Points.DataBindY(valueArray);
但是,我在使用该程序时遇到了一个问题,即使它在图表中显示 3,000 个值,它也会消耗大量计算机资源.
However, I have a problem using this program, which is it consumes much computer resource even when it shows 3,000 values in the chart.
我打算让图表代表 50,000 到 100,000 个值,但我认为它会消耗太多资源,每次获得新值时都会复制和显示旧值.
I plan to make the chart represent 50,000 to 100,000 values, but I think it uses up too much resource copying and showing old values everytime it gets a new value.
我想知道是否有任何函数或方法可以完成此类工作.如果我能得到一些建议或想法,我将不胜感激.
I'd like to know if there are any functions or methods to do this kind of job. I would appreciate it if I could get some advices or ideas.
推荐答案
我所知道的几乎没有任何理由加载任何具有 100,000 多个点的图表.您可以使用原始点的一小部分来展示您的数据,而不会丢失任何视觉信息.这是一个将 100,000 点过滤到 250 点 (0.25%) 的示例:
There's hardly any reason that I know of, to ever load any chart with 100,000+ points. You can present your data using a fraction of your original points, without any loss of visual information. Here's a sample filtering 100,000 points down to 250 points (0.25%):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
double percent = 0.0025;
List<DataPoint> original = GetData();
List<DataPoint> filtered = Filter(original, percent);
foreach (DataPoint dp in original)
chart1.Series[0].Points.Add(dp);
foreach (DataPoint dp in filtered)
chart1.Series[1].Points.Add(dp);
chart1.ChartAreas[0].AxisY.Maximum = original.Max(dp => dp.YValues[0]);
chart1.ChartAreas[0].AxisY.Minimum = original.Min(dp => dp.YValues[0]);
chart1.ChartAreas[0].AxisX.Minimum = 0;
Text = string.Format("original = {0:0,0} points, filtered = {1:0,0} points, percent = {2:P2}", original.Count, filtered.Count, percent);
}
private List<DataPoint> Filter(List<DataPoint> orig, double percent)
{
Random r = new Random(DateTime.Now.Millisecond);
List<DataPoint> filt = new List<DataPoint>(orig.ToArray());
double total = filt.Count;
while (filt.Count / total > percent)
filt.RemoveAt(r.Next(1, filt.Count - 1));
return filt;
}
private void button1_Click(object sender, EventArgs e)
{
if (chart1.Series[0].Enabled)
{
chart1.Series[0].Enabled = false;
chart1.Series[1].Enabled = true;
}
else
{
chart1.Series[0].Enabled = true;
chart1.Series[1].Enabled = false;
}
}
}
我知道您正在动态添加点,因此您必须为其添加一些逻辑.但我的观点仍然成立:您必须过滤您的数据.此外,如果你能想出一个更复杂的过滤器,你可以使用.
I understand you're adding points dynamically, so you'll have to add some logic to it. But my point still stands: you must filter your data. Also, you can use a more sophisticated filter, if you can come up with one.
这篇关于如何在散点图中绘制超过 50,000 个值,从而节省计算机资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!