如何仅获取可见曲线数据的PointPairList,而不是Curveitem列表中的所有点?缩放或平移时,此列表应动态更改。

谢谢。

最佳答案

似乎没有ZedGraph API方法可以为您执行此操作。但是,下面的代码演示了一个VisiblePoints方法,该方法返回PointPairList并在一个非常特定的ZedGraph用例下工作。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ZedGraph;

namespace ZedGraphMain
{
    public class ZedGraphDemo : Form
    {
        public ZedGraphDemo()
        {
            var graphControl = new ZedGraphControl();
            graphControl.Dock = DockStyle.Fill;
            graphControl.IsShowPointValues = true;
            Controls.Add(graphControl);
            var points = new PointPairList();
            for (double i = 0; i <= 5.0; i += 1)
                points.Add(i, i);

            var curve = graphControl.GraphPane.AddCurve("A Label", points, Color.ForestGreen);

            graphControl.RestoreScale(graphControl.GraphPane);
            graphControl.ZoomEvent += (_, __, ___) => LogVisibility(graphControl, curve, points);
        }

        private static PointPairList VisiblePoints(ZedGraphControl control, LineItem lineItem, PointPairList points)
        {
            var pointPairList = new PointPairList();
            pointPairList.AddRange(points.Where(pp => IsVisible(control, lineItem, pp)).ToList());
            return pointPairList;
        }

        private static bool IsVisible(ZedGraphControl control, LineItem lineItem, PointPair point)
        {
            GraphPane pane = control.GraphPane;
            Scale xScale = lineItem.GetXAxis(pane).Scale;
            Scale yScale = lineItem.GetYAxis(pane).Scale;
            return point.X > xScale.Min && point.X < xScale.Max && point.Y > yScale.Min && point.Y < yScale.Max;
        }

        private static void LogVisibility(ZedGraphControl control, LineItem lineItem, PointPairList points)
        {
            List<PointPair> visiblePoints = VisiblePoints(control, lineItem, points);
            Console.Out.WriteLine(DateTime.Now + ": " + string.Join(",", visiblePoints.Select(pp => string.Format("({0:N1}, {1:N1})", pp.X, pp.Y))));
        }
    }
}

10-08 04:28