|
| 1 | +using Syncfusion.Maui.Toolkit.Charts; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace CircleDataPoints |
| 9 | +{ |
| 10 | + public class CircleDataPointSelector : ChartInteractiveBehavior, IDrawable |
| 11 | + { |
| 12 | + |
| 13 | + private float CenterX; |
| 14 | + private float CenterY; |
| 15 | + private float radius = 0; |
| 16 | + private bool showCircle = false; |
| 17 | + |
| 18 | + public GraphicsView Graphics { get; set; } |
| 19 | + |
| 20 | + public void Draw(ICanvas canvas, RectF dirtyRect) |
| 21 | + { |
| 22 | + canvas.StrokeColor = Colors.Black; |
| 23 | + canvas.StrokeSize = 2; |
| 24 | + canvas.FillColor = Colors.Red.WithAlpha(0.2f); |
| 25 | + canvas.DrawEllipse(CenterX - radius, CenterY - radius, radius * 2, radius * 2); |
| 26 | + } |
| 27 | + |
| 28 | + protected override void OnTouchDown(ChartBase chart, float pointX, float pointY) |
| 29 | + { |
| 30 | + var seriesBounds = chart.SeriesBounds; |
| 31 | + |
| 32 | + CenterX = (float)(pointX - seriesBounds.Left); |
| 33 | + CenterY = (float)(pointY - seriesBounds.Top); |
| 34 | + radius = 0; |
| 35 | + showCircle = true; |
| 36 | + Graphics.Invalidate(); |
| 37 | + } |
| 38 | + |
| 39 | + protected override void OnTouchMove(ChartBase chart, float pointX, float pointY) |
| 40 | + { |
| 41 | + if (showCircle) |
| 42 | + { |
| 43 | + var seriesBounds = chart.SeriesBounds; |
| 44 | + |
| 45 | + // Calculate radius using exact code from clipboard |
| 46 | + float dx = (float)(pointX - seriesBounds.Left - CenterX); |
| 47 | + float dy = (float)(pointY - seriesBounds.Top - CenterY); |
| 48 | + radius = (float)Math.Sqrt(dx * dx + dy * dy); |
| 49 | + |
| 50 | + |
| 51 | + Graphics.Invalidate(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected override void OnTouchUp(ChartBase chart, float pointX, float pointY) |
| 56 | + { |
| 57 | + if (chart is SfCartesianChart cartesianChart) |
| 58 | + { |
| 59 | + var viewModel = chart.BindingContext as ScatterSeriesViewModel; |
| 60 | + |
| 61 | + // Create bounding rect for the circle in chart coordinates. |
| 62 | + var boundingRect = new Rect( |
| 63 | + CenterX - radius, |
| 64 | + CenterY - radius, |
| 65 | + radius * 2, |
| 66 | + radius * 2 |
| 67 | + ); |
| 68 | + |
| 69 | + var selectedIndexes = new List<int>(); |
| 70 | + |
| 71 | + foreach (var series in cartesianChart.Series) |
| 72 | + { |
| 73 | + if (series is ScatterSeries scatterSeries) |
| 74 | + { |
| 75 | + var rectanglePoints = scatterSeries.GetDataPoints(boundingRect); |
| 76 | + |
| 77 | + if (rectanglePoints != null && viewModel != null) |
| 78 | + { |
| 79 | + for (int i = 0; i < viewModel.Data.Count; i++) |
| 80 | + { |
| 81 | + var point = viewModel.Data[i]; |
| 82 | + |
| 83 | + if (rectanglePoints.Contains(point)) |
| 84 | + { |
| 85 | + var xAxis = cartesianChart.XAxes[0]; |
| 86 | + var yAxis = cartesianChart.YAxes[0]; |
| 87 | + |
| 88 | + // Convert data coordinates to pixel coordinates |
| 89 | + double pixelX = xAxis.ValueToPoint(point.XValue); |
| 90 | + double pixelY = yAxis.ValueToPoint(point.YValue); |
| 91 | + |
| 92 | + // Calculate distance from center to this point |
| 93 | + double distance = Math.Sqrt( |
| 94 | + Math.Pow(pixelX - CenterX, 2) + |
| 95 | + Math.Pow(pixelY - CenterY, 2)); |
| 96 | + |
| 97 | + // If distance is less than radius, point is in circle |
| 98 | + if (distance <= radius) |
| 99 | + { |
| 100 | + selectedIndexes.Add(i); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + scatterSeries.SelectionBehavior.SelectedIndexes = selectedIndexes; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + showCircle = false; |
| 112 | + Graphics.Invalidate(); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments