簡體   English   中英

如何使用C#在雙Y軸ZedGraph圖形中添加實時數據?

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

對於我的項目,我需要添加和更新實時數據到我的雙y軸圖。 Y和Y2值共享相同的X值,我已經創建了它。 現在我有一個函數可以將新的點對添加到曲線列表中。

這是我的問題:我的Y和Y2值總是被添加到第一條曲線的曲線列表中。 如何將Y2值添加到圖表中的第二個曲線列表中?

這是我的功能代碼:

    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();
    }

如何將Y2值添加到第二曲線列表?

我自己找到了一個可能的解 這是我的代碼更改:

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();
    }

重要的是使用“CurveList [i]”中的索引。 所以[0]是帶有Y值的曲線,[1]是帶有Y2值的曲線,依此類推。

我希望這可以幫助任何有相同或類似問題的人。

這是上面的“更好”的實現:

    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