【问题标题】:generate a trendline from files in a chart从图表中的文件生成趋势线
【发布时间】:2018-07-12 06:25:50
【问题描述】:

我希望能够从文件夹中获取.csv 文件并将它们绘制在图表中。

目前,我只是保存文件并显示如下的单个曲线:

RunTest 函数:

public List<Tuple<double,double>> runTest()
{
    _dpg = new Root(this, "english", false);
    _dpg.Init();
    var filename = "Dpg10Export_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".csv";
    List<Tuple<double, double>> results = new List<Tuple<double, double>>();

    var measurement = new Measurement();
    var resultCode = RunMeasurement(60, 1000, 2200, ref measurement, null /* TODO: ref ProgressBar? */);

    using (var fileStream = new StreamWriter(filename))
    {
        var count = measurement.I_inc_Values.Count;

        for (var i = 0; i < count; i++)
        {
            var item = measurement.I_inc_Values[i + 1];
            var current = (float)Convert.ToDouble(item);

            item = measurement.LI_inc_Values[i + 1];
            var inductance = (float)Convert.ToDouble(item);

            var line = current.ToString() + ";" + inductance.ToString() + ";";

            fileStream.WriteLine(line); 
            currentList.Add(current);
            inductanceList.Add(inductance);
            results.Add(new Tuple<double, double>(current,inductance));
            if (i == 0 || (i + 1) % 32 == 0)
            {
                Console.WriteLine((i + 1) + ": " + line);
            }
        }
    }
    return results;
}

此代码生成的 csv 文件如下所示:

0,22 | 0,44

0,32 | 0,54

0,44 | 0,65

这些值会产生如下所示的曲线:

当你点击“get Waveform”按钮时,上面的曲线就生成了。但是,我想显示所有已生成的曲线,以及 趋势线。我将如何实现这一目标?

void BindData(List<Tuple<double,double>> results)
{
    chart.Series.Clear();
    var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
    {
        Name = "curr/induc",
        Color = System.Drawing.Color.Green,
        IsVisibleInLegend = true,
        IsXValueIndexed = true,
        ChartType = SeriesChartType.Line                
    };

    foreach (var i in results)
    {
        series1.Points.AddXY(i.Item2,i.Item1);
    }

    chart.Invalidate();
    chart.Series.Add(series1);
}

private void getWaveformBtn_Click(object sender, EventArgs e)
{
    Dpg10Client.Dpg10Settings settings  = new Dpg10Client.Dpg10Settings();
    Dpg10Instrument hej                 = new Dpg10Instrument(settings);
    List<Tuple<double,double>> results  = hej.runTest();
    double current, inductance;

    foreach(var i in results)
    {
        current = i.Item1;
        inductance = i.Item2;
        textBoxWaveformInput.Text += current.ToString() + inductance.ToString();
    }
    BindData(results);         
}

TL;DR

解析来自CSV 文件的信息以生成曲线,并根据这些文件创建趋势线。

标记为重复:该答案是关于直线的,这些值可以在曲线中波动。

【问题讨论】:

  • @edit:线性回归趋势线是直的。你需要非线性近似吗?
  • @dlatikay 是的!我猜这是你可以表达的一种方式。我现在使用虚拟数据,因为我无法访问产生这些值的机器,因此是线性示例。我还需要从文件中生成曲线,这本身就给我带来了另一个问题。
  • something like this?您正在使用的图表组件Forms.DataVisualization.Charting 似乎具有使用线性与指数趋势图的内置功能(我没有尝试此代码,不知道它是否真的有效)
  • This 可能有效,但 afaik 只能在逐点的基础上使用,也就是说,它只有在所有系列都对齐时才有效,即具有相同的 x 值。有一个插入缺失数据点的功能,但我不确定 a) 是否有帮助,也不确定 b) 来自 OP 的数据是否对齐。

标签: c# winforms charts


【解决方案1】:

有很多方法可以解决您问题中的大部分问题。

让我只处理实际上与计算多个系列的平均值有关的问题,即不会处理使用 CSV 文件中的数据创建系列。

有一个可以做各种高级数学的内置类,financial 和 statistical,但我没有找到一个可以帮助在多个系列上创建平均线/曲线的类.

所以我们自己做吧..

第一个问题是,要计算平均值,我们不仅需要数据,而且数据,即它们的 x 值,必须分组到 'bins' 中我们想要得到平均值。

除非数据已经这样分组,例如因为它们每天每个系列都有一个值,所以我们需要创建这样的组。

让我们首先收集我们要处理的所有系列的所有点;您可能需要调整循环以仅包含您的系列集..:

var allPoints = new List <DataPoint>();
for (int s = 0; s < 3; s++)  // I know I have created these three series
{
    Series ser = chartGraphic.Series["S" + (s+1)];
    allPoints.AddRange(ser.Points);
}

接下来,我们需要确定 bin 范围/大小,即确定哪些 x 值应落入同一 bin/组的值。我选择了 10 个垃圾箱。

所以我们最好得到 x 值的总范围并除以我们想要的 bin 数量..:

double xmax = allPoints.Max(x => x.XValue);
double xmin = allPoints.Min(x => x.XValue);
int bins = 10;
double groupSize = (xmax - xmin) / (bins - 1);

接下来我们进行实际的数学运算;为此,我们对我们的点进行排序,分组并为每个分组选择平均值..:

var grouped = allPoints
                .OrderBy(x => x.XValue)
                .GroupBy(x => groupSize * (int)(x.XValue /groupSize ))
                .Select(x => new { xval = x.Key, yavg = x.Average(y => y.YValues[0]) })
                .ToList();

现在我们可以将它们添加到一个新系列中以显示箱中的平均值:

 foreach (var kv in grouped) avgSeries.Points.AddXY(kv.xval + groupSize/2f, kv.yavg);

我将平均点放在 bin 的中心。

这是一个例子:

关于示例的几点说明:

平均线没有显示真正的“趋势”,因为我的数据几乎是随机的。

我已将Markers 添加到所有系列中,以使DataPoints 脱颖而出。这是我为平均值系列所做的:

avgSeries.MarkerStyle = MarkerStyle.Cross;
avgSeries.MarkerSize = 7;
avgSeries.BorderWidth = 2;

我添加了StripLine 来显示垃圾箱。方法如下:

StripLine sl = new StripLine();
sl.BackColor = Color.FromArgb(44,155,155,222);
sl.StripWidth = groupSize;
sl.Interval = groupSize * 2;
sl.IntervalOffset = chartGraphic.ChartAreas[0].AxisX.IntervalOffset;
chartGraphic.ChartAreas[0].AxisX.StripLines.Add(sl);

我还设置了一个非常低的值 -300 来演示这将如何降低其 bin 中的平均值。为了使图表仍然很好地集中在正常的数据范围内,我在 y 轴上添加了 ScaleBreakStyle:

chartGraphic.ChartAreas[0].AxisY.ScaleBreakStyle.BreakLineStyle = BreakLineStyle.Wave;
chartGraphic.ChartAreas[0].AxisY.ScaleBreakStyle.Enabled = true;

【讨论】:

  • 非常感谢您解决这个问题!与此同时,我遇到了另一个问题,并且出现了一个非常奇怪的行为,我将其发布在一个单独的线程中:stackoverflow.com/questions/51264338/…
  • 我稍微调整了代码并添加了对 stripline.offset 的更正和一些手绘注释;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多