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

发布于 2024-09-14 21:55:52 字数 1175 浏览 5 评论 0原文

对于我的项目,我需要向双 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 值添加到第二条曲线列表中?

For my project, I need to add and update real-time data to my dual y-axis graph. The Y and Y2 values share the same X value, and I created it already. 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. How can I get the Y2 value added to the second curve list in my graph?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-09-21 21:55:52

我自己找到了一个可能的解决方案。以下是我的代码更改:

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 值曲线,依此类推。

我希望这可以帮助任何有相同或类似问题的人。

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]". So [0] is my curve with the Y values and [1] is my curve with the Y2 values, and so on.

I hope this helps anybody who has the same or similar problem.

温柔女人霸气范 2024-09-21 21:55:52

这是上面的“更好”的实现:

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

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文