如何绘制平滑/圆角/曲线线图?(C#) [英] How can I draw smoothed/rounded/curved line graphs? (C#)

查看:60
本文介绍了如何绘制平滑/圆角/曲线线图?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测量一些系统性能数据以将其存储在数据库中.随着时间的推移,我从这些数据点绘制折线图.就其性质而言,这些数据点有点嘈杂,即.每个点都至少偏离局部平均值.当从一个点直接绘制到下一个线图时,它会产生锯齿状的图形.在大的时间尺度上,比如每个像素 > 10 个数据点,这个噪声被压缩到一个宽的锯齿状线区域,也就是说,高 20 像素,而不是更小尺度的 1 像素.

I'm measuring some system performance data to store it in a database. From those data points I'm drawing line graphs over time. In their nature, those data points are a bit noisy, ie. every single point deviates at least a bit from the local mean value. When drawing the line graph straight from one point to the next, it produces jagged graphs. At a large time scale like > 10 data points per pixel, this noise is compressed into a wide jagged line area that is, say, 20px high instead of 1px as in smaller scales.

我已经阅读了有关线条平滑、抗锯齿、简化以及所有这些内容的文章.但我发现的一切似乎都与其他事情有关.

I've read about line smoothing, anti-aliasing, simplifying and all these things. But everything I've found seems to be about something else.

我不需要抗锯齿,.NET 在屏幕上绘制线条时已经为我做了.

I don't need anti-aliasing, .NET already does that for me when drawing the line on the screen.

我不想简化.我需要极端值保持可见,至少其中大部分是可见的.

I don't want simplification. I need the extreme values to remain visible, at least most of them.

我认为它朝着样条曲线的方向发展,但我找不到太多示例图像来评估所描述的事物是否是我想要的.不过,我确实在 Google Books 上找到了一本非常科学的书,里面有半页长的公式,我现在不喜欢通读...

I think it goes in the direction of spline curves but I couldn't find much example images to evaluate whether the described thing is what I want. I did find a highly scientific book at Google Books though, full of half-page long formulas, which I wasn't like reading through now...

举个例子,看看Linux/Gnome的系统监视器应用程序.我用平滑线绘制了最近的 CPU/内存/网络使用情况.这可能有点过于简单化,但我会尝试一下,看看我是否可以调整它.

To give you an example, just look at Linux/Gnome's system monitor application. I draws the recent CPU/memory/network usage with a smoothed line. This may be a bit oversimplified, but I'd give it a try and see if I can tweak it.

我更喜欢 C# 代码,但其他语言的算法或代码也可以,只要我可以在没有外部引用的情况下将其移植到 C#.

I'd prefer C# code but algorithms or code in other languages is fine, too, as long as I can port it to C# without external references.

推荐答案

您可以进行一些数据平滑处理.不要使用真实数据,而是应用一个简单的平滑算法来保持峰值,就像 Savitzky-Golayfilter 一样.

You can do some data-smoothing. Instead of using the real data, apply a simple smoothing algorithm that keeps the peaks like a Savitzky-Golayfilter.

您可以在此处获取系数.

最简单的方法是:

从我链接到的网站中获取最高系数:

Take the top coefficients from the website I linked to:

// For np = 5 = 5 data points
var h = 35.0;
var coeff = new float[] { 17, 12, -3 }; // coefficients from the site
var easyCoeff = new float[] {-3, 12, 17, 12, -3}; // Its symmetrical
var center = 2; // = the center of the easyCoeff array

//现在对数据中的每个点计算一个平滑点:

// now for every point from your data you calculate a smoothed point:

smoothed[x] = 
   ((data[x - 2] * easyCoeff[center - 2]) +
    (data[x - 1] * easyCoeff[center - 1]) +
    (data[x - 0] * easyCoeff[center - 0]) +
    (data[x + 1] * easyCoeff[center + 1]) +
    (data[x + 2] * easyCoeff[center + 2])) / h;

使用5点无法平滑的前2点和后2点.

The first 2 and last 2 points you cannoth smooth when using 5 points.

如果您希望您的数据更加平滑",您可以尝试使用更大数据点的系数.

If you want your data to be more "smoothed" you can experiment with coefficents with larger data points.

现在您可以在平滑"数据上画一条线.您的 np = 点数越大,您的数据就越平滑.但是你也会失去峰值准确度,但只是简单地将一些点平均在一起时不会那么多.

Now you can draw a line through your "smoothed" data. The larger your np = number of points, the smoother your data. But you also loose peak accuracy, but not as much when simply averaging some points together.

这篇关于如何绘制平滑/圆角/曲线线图?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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