如何在散点图中绘制超过50,000个值,从而节省计算机资源? [英] How can I plot more than 50,000 values in a scatter chart, saving computer resource?

查看:127
本文介绍了如何在散点图中绘制超过50,000个值,从而节省计算机资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆