winforms 在Telerik ChartView中放大后,我可以从图表中获得可见的数据点吗?

qnyhuwrf  于 2023-04-12  发布在  其他
关注(0)|答案(1)|浏览(174)

我有一个telerik RadChartView,它有一些数据,我的X轴是DateTimeContinuousAxis,我的Y轴是LinearAxis。这个RadChartView有一个缩放按钮,使用LassoZoomController方法进行缩放。我面临一个问题,在放大或缩小后,我想得到用户在缩放中可以看到的可见点(忽略那些在缩放中不可见的)。有没有一种方法可以在缩放后只得到图表中可见的点?
在下面的代码中,我想验证一下,只有当dateTime在我的radChartView中可见时,才将它添加到列表中

var categoricalDataPoints = radChartView.Series.Where(serie => serie.IsVisible).SelectMany(serie => serie.DataPoints)
    .OfType<CategoricalDataPoint>().ToList();

var visibleDateTimes = new List<DateTime>();

foreach (var categoricalDataPoint in categoricalDataPoints)
{
    if (categoricalDataPoint.Category is DateTime dateTime)
    {
        visibleDateTimes.Add(dateTime);
    }
}

我尝试获取第一个轴并将其解析为DateTimeContinuousAxis并使用ActualRange,但此范围考虑了图表的所有日期时间,而不仅仅是可见时间:

var categoricalDataPoints = crvMain.Series.Where(serie => serie.IsVisible).SelectMany(serie => serie.DataPoints)
    .OfType<CategoricalDataPoint>().ToList();

var visibleDateTimes = new List<DateTime>();

if (crvMain.Axes[0] is DateTimeContinuousAxis dateTimeContinuousAxis)
{
    foreach (var categoricalDataPoint in categoricalDataPoints)
    {
        if (categoricalDataPoint.Category is DateTime dateTime)
        {
            if (dateTime >= dateTimeContinuousAxis.ActualRange.Minimum && dateTime <= dateTimeContinuousAxis.ActualRange.Maximum)
            {
                visibleDateTimes.Add(dateTime);
            }
        }
    }
}
lyfkaqu1

lyfkaqu11#

Gabriel,我准备了一个示例代码片段,演示如何在执行平移/缩放操作后获取可见数据点。下面的截图说明了实现的结果:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        LineSeries lineSeries = new LineSeries();
        lineSeries.ShowLabels = true;
        lineSeries.PointSize = new SizeF(8, 8);
        for (int i = 0; i < 10; i++)
        {
            lineSeries.DataPoints.Add(new CategoricalDataPoint(6 + i * i, DateTime.Now.AddDays(i)));
        }
        DateTimeContinuousAxis continuousAxis = new DateTimeContinuousAxis();
        continuousAxis.PlotMode = AxisPlotMode.BetweenTicks;
        continuousAxis.LabelFormat = "{0:d}";
        //First assign the axis to the VerticalAxis property and then add the series to the chart
        lineSeries.HorizontalAxis = continuousAxis;
        radChartView1.Series.Add(lineSeries);

        this.radChartView1.ShowPanZoom = true;
        this.radChartView1.View.ZoomChanged += View_ZoomChanged;
        this.radChartView1.View.PanChanged += View_PanChanged;
    }

    private void View_ZoomChanged(object sender, EventArgs e)
    {
        GetVisiblePoints();
    }

    private void View_PanChanged(object sender, EventArgs e)
    {
        GetVisiblePoints();
    }

    public void GetVisiblePoints()
    {
        List<CategoricalDataPoint> visiblePoints = new List<CategoricalDataPoint>();
        IChartView view = ((IChartView)this.radChartView1.View);
        foreach (ChartSeries series in this.radChartView1.Series)
        {
            foreach (var dp in series.DataPoints)
            {
                CategoricalDataPoint cdp = (CategoricalDataPoint)dp;
                if (CheckIfDataPointIsVisible(view, cdp))
                { 
                    visiblePoints.Add(cdp); 
                }
            }
        }

        foreach (CategoricalDataPoint dp in visiblePoints)
        {
            Console.WriteLine(dp.Value +" >> "+dp.Category);
        }
    }

    private bool CheckIfDataPointIsVisible(IChartView view, CategoricalDataPoint cdp)
    {
        double width = ((ChartSeries)cdp.Presenter).Axes[1].Model.LayoutSlot.Width;
        RadRect viewport = new RadRect(-view.PlotOriginX + width, -view.PlotOriginY, view.ViewportWidth, view.ViewportHeight);
        return viewport.IntersectsWith(cdp.LayoutSlot);
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        GetVisiblePoints();
    }
}

相关问题