|
| 1 | +--- |
| 2 | +title: Finding the Nearest ScatterDataPoint to a Mouse Click in RadChartView |
| 3 | +description: Learn how to determine the closest ScatterDataPoint in a RadChartView when the user clicks within the chart area. |
| 4 | +type: how-to |
| 5 | +page_title: How to Identify the Closest ScatterDataPoint to a Mouse Click in RadChartView |
| 6 | +slug: chartview-scatterseries-closest-point-mouseclick |
| 7 | +tags: radchartview, scatterdatapoint, winforms, mouseeventargs, chart-axes |
| 8 | +res_type: kb |
| 9 | +ticketid: 1686235 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +|Product Version|Product|Author| |
| 15 | +|----|----|----| |
| 16 | +|2025.1.211|RadTreeView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +This article will demonstrate how to determine the nearest ScatterDataPoint to a mouse click location using the mouse click position. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To achieve this, calculate the axis values corresponding to the mouse click position on the chart. Then iterate over the data points in the series and determine the closest one. |
| 25 | + |
| 26 | + Once the closest point is identified, you can create an additional ScatterSeries and add a duplicate ScatterDataPoint with the same values. This secondary series can be used to visually highlight the nearest data point. |
| 27 | + |
| 28 | +The following code snippet demonstrates a full implementation. |
| 29 | + |
| 30 | +````C# |
| 31 | +public partial class Form1 : Form |
| 32 | +{ |
| 33 | + ScatterSeries scatterSeries = new ScatterSeries(); |
| 34 | + public Form1() |
| 35 | + { |
| 36 | + InitializeComponent(); |
| 37 | + scatterSeries.Name = ""; |
| 38 | + scatterSeries.DataPoints.Add(new ScatterDataPoint(19000, 15)); |
| 39 | + scatterSeries.DataPoints.Add(new ScatterDataPoint(10000, 18)); |
| 40 | + scatterSeries.DataPoints.Add(new ScatterDataPoint(15000, 13)); |
| 41 | + scatterSeries.DataPoints.Add(new ScatterDataPoint(80000, 10)); |
| 42 | + scatterSeries.DataPoints.Add(new ScatterDataPoint(60000, 25)); |
| 43 | + scatterSeries.PointSize = new SizeF(5, 5); |
| 44 | + this.radChartView1.Series.Add(scatterSeries); |
| 45 | + |
| 46 | + this.radChartView1.MouseDown += RadChartView1_MouseDown; |
| 47 | + } |
| 48 | + private void RadChartView1_MouseDown(object sender, MouseEventArgs e) |
| 49 | + { |
| 50 | + Point pt = this.radChartView1.ChartElement.Wrapper.PointFromControl(e.Location); |
| 51 | + double verticalValue = Convert.ToDouble(GetVerticalAxisValueFromMouse(e)); |
| 52 | + double horizontalValue = Convert.ToDouble(GetHorizontalAxisValueFromMouse(e)); |
| 53 | + |
| 54 | + Debug.WriteLine("X: " + horizontalValue + " Y: " + verticalValue); |
| 55 | + |
| 56 | + ScatterDataPoint closestPoint = null; |
| 57 | + double minDistance = double.MaxValue; |
| 58 | + |
| 59 | + var seriesToRemove = this.radChartView1.Series.FirstOrDefault(s => s.Name =="Closest Point"); |
| 60 | + if (seriesToRemove != null) |
| 61 | + { |
| 62 | + this.radChartView1.Series.Remove(seriesToRemove); |
| 63 | + } |
| 64 | + foreach (ScatterDataPoint dataPoint in scatterSeries.DataPoints) |
| 65 | + { |
| 66 | + // Calculate the Euclidean distance |
| 67 | + double distance = Math.Sqrt(Math.Pow(dataPoint.XValue - horizontalValue, 2) + Math.Pow((double)dataPoint.YValue - verticalValue, 2)); |
| 68 | + // Update the closest point if the current distance is smaller |
| 69 | + if (distance < minDistance) |
| 70 | + { |
| 71 | + minDistance = distance; |
| 72 | + closestPoint = dataPoint; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if (closestPoint != null) |
| 77 | + { |
| 78 | + var newScatterSeries = new ScatterSeries(); |
| 79 | + newScatterSeries.Name = "Closest Point"; |
| 80 | + newScatterSeries.DataPoints.Add(new ScatterDataPoint(closestPoint.XValue, (double)closestPoint.YValue)); |
| 81 | + newScatterSeries.PointSize = new SizeF(15, 15); |
| 82 | + newScatterSeries.BorderColor = Color.Red; |
| 83 | + this.radChartView1.Series.Add(newScatterSeries); |
| 84 | + this.radChartView1.Invalidate(); |
| 85 | + this.radChartView1.Update(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private object GetHorizontalAxisValueFromMouse(MouseEventArgs e) |
| 90 | + { |
| 91 | + LinearAxis axis = radChartView1.Axes[0] as LinearAxis; |
| 92 | + IChartView view = (IChartView)axis.View; |
| 93 | + double delta = axis.ActualRange.Maximum - axis.ActualRange.Minimum; |
| 94 | + double totalWidth = axis.Model.LayoutSlot.Width; |
| 95 | + double ratio = 1 - (e.Location.X - this.radChartView1.Area.View.Viewport.X - view.PlotOriginX - axis.Model.LayoutSlot.X) / (totalWidth * view.ZoomWidth); |
| 96 | + double value = axis.ActualRange.Minimum + delta * ratio; |
| 97 | + return axis.ActualRange.Maximum - value; |
| 98 | + } |
| 99 | + private object GetVerticalAxisValueFromMouse(MouseEventArgs e) |
| 100 | + { |
| 101 | + LinearAxis axis = radChartView1.Axes[1] as LinearAxis; |
| 102 | + IChartView view = (IChartView)axis.View; |
| 103 | + double delta = axis.ActualRange.Maximum - axis.ActualRange.Minimum; |
| 104 | + double totalHeight = axis.Model.LayoutSlot.Height; |
| 105 | + double ratio = 1 - (e.Location.Y - this.radChartView1.Area.View.Viewport.Y - view.PlotOriginY - axis.Model.LayoutSlot.Y) / (totalHeight * view.ZoomHeight); |
| 106 | + double value = axis.ActualRange.Minimum + delta * ratio; |
| 107 | + return value; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +```` |
| 112 | + |
| 113 | +## See Also |
| 114 | + |
| 115 | +- [RadChartView Overview Documentation](https://docs.telerik.com/devtools/winforms/controls/chartview/overview) |
0 commit comments