Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZedGraph: Get PointPairList of visible curve points only

How do I get a PointPairList for the visible curve data only and not all points in the Curveitem list? This list should change dynamically when zooming or panning.

Thanks.

like image 879
mraviator Avatar asked Dec 05 '25 02:12

mraviator


1 Answers

There doesn't appear to be a ZedGraph API method that does this for you. The code below, though, demonstrates a VisiblePoints method that returns a PointPairList and works under one very specific ZedGraph use case.

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))));
        }
    }
}
like image 109
Garrett Smith Avatar answered Dec 06 '25 18:12

Garrett Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!