简体   繁体   English

如何使用C#在双Y轴ZedGraph图形中添加实时数据?

[英]How to add real-time data in a dual-Y-axis ZedGraph graph using C#?

For my project, I need to add and update real-time data to my dual y-axis graph. 对于我的项目,我需要添加和更新实时数据到我的双y轴图。 The Y and Y2 values share the same X value, and I created it already. Y和Y2值共享相同的X值,我已经创建了它。 Now I have a function that adds the new point pairs to the curve lists. 现在我有一个函数可以将新的点对添加到曲线列表中。

Here is my problem: My Y and Y2 values are always added to the curve list of the first curve. 这是我的问题:我的Y和Y2值总是被添加到第一条曲线的曲线列表中。 How can I get the Y2 value added to the second curve list in my graph? 如何将Y2值添加到图表中的第二个曲线列表中?

Here is my function code: 这是我的功能代码:

    private void AddDataToGraph(ZedGraphControl zg1, XDate xValue, double yValue1, double yValue2)
    {
        // Make sure that the curvelist has at least one curve.
        if (zg1.GraphPane.CurveList.Count <= 0)
            return;

        // Get the first CurveItem in the graph.
        LineItem curve = zg1.GraphPane.CurveList[0] as LineItem;

        if (curve == null)
            return;

        // Get the PointPairList.
        IPointListEdit list = curve.Points as IPointListEdit;
        IPointListEdit list2 = curve.Points as IPointListEdit;

        // If this is null, it means the reference at curve.Points does not
        // support IPointListEdit, so we won't be able to modify it.
        if (list == null || list2 == null)
            return;

        // Add new data points to the graph.
        list.Add(xValue, yValue1);
        list2.Add(xValue, yValue2);

        // Force redraw.
        zg1.Invalidate();
    }

How can he Y2 values be added to the 2nd curve list? 如何将Y2值添加到第二曲线列表?

Found a possible solution myself. 我自己找到了一个可能的解 Here are my code changes: 这是我的代码更改:

private void AddDataToGraph(ZedGraphControl zg1, XDate xValue, double yValue1, double yValue2)
    {
        // Make sure that the curvelist has at least one curve
        if (zg1.GraphPane.CurveList.Count <= 0)
            return;

        // Get the first CurveItem in the graph
        LineItem curve = zg1.GraphPane.CurveList[0] as LineItem;
        LineItem curve2 = zg1.GraphPane.CurveList[1] as LineItem;

        if (curve == null || curve2 == null)
            return;

        // Get the PointPairList
        IPointListEdit list = curve.Points as IPointListEdit;
        IPointListEdit list2 = curve2.Points as IPointListEdit;
        // If this is null, it means the reference at curve.Points does not
        // support IPointListEdit, so we won't be able to modify it
        if (list == null || list2 == null)
            return;

        // add new data points to the graph
        list.Add(xValue, yValue1);
        list2.Add(xValue, yValue2);

        // force redraw
        zg1.Invalidate();
    }

The important thing is to use the index in the "CurveList[i]". 重要的是使用“CurveList [i]”中的索引。 So [0] is my curve with the Y values and [1] is my curve with the Y2 values, and so on. 所以[0]是带有Y值的曲线,[1]是带有Y2值的曲线,依此类推。

I hope this helps anybody who has the same or similar problem. 我希望这可以帮助任何有相同或类似问题的人。

Here's a "nicer" implementation of above: 这是上面的“更好”的实现:

    private void AddDataToGraph(ZedGraphControl zg1, XDate xValue, double[] yValues)
    {
        GraphPane myPane = zg1.GraphPane;
        // Make sure that the curvelist has at least one curve
        if (myPane.CurveList.Count <= 0)
            return;
        else if (myPane.CurveList.Count != yValues.Length)
            return;

        for (int i = 0; i < yValues.Length; i++ )
        {
            ((IPointListEdit)myPane.CurveList[i].Points).Add(xValue, yValues[i]);
        }
        // force redraw
        zg1.Invalidate();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM