|
2 | 2 |
|
3 | 3 | The [WPF Chart](https://www.syncfusion.com/wpf-controls/charts) allows you to select multiple scatter data points using the canvas and mouse drag.
|
4 | 4 |
|
| 5 | +Here are the steps to follow: |
| 6 | + |
| 7 | +**Step 1:** Create a scatter chart within a grid layout using this documentation |
| 8 | + |
| 9 | +**Step 2:** Initialize the canvas with mouse interaction events. |
| 10 | + |
| 11 | +**Step 3:** Implement the mouse interaction events to select the desired data points using the mouse drag. |
| 12 | + |
| 13 | +**C#** |
| 14 | + |
| 15 | +```csharp |
| 16 | + public partial class MainWindow : Window |
| 17 | + { |
| 18 | + #region Fields |
| 19 | + |
| 20 | + private Point startPoint; |
| 21 | + private Rectangle? currentRectangle; |
| 22 | + private Rect currentRect; |
| 23 | + |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region Constructor |
| 27 | + |
| 28 | + public MainWindow() |
| 29 | + { |
| 30 | + InitializeComponent(); |
| 31 | + } |
| 32 | + |
| 33 | + #endregion |
| 34 | + |
| 35 | + #region Methods |
| 36 | + |
| 37 | + private void OnMouseMove(object sender, MouseEventArgs e) |
| 38 | + { |
| 39 | + if (e.LeftButton == MouseButtonState.Pressed && currentRectangle != null) |
| 40 | + { |
| 41 | + // Update the size of the rectangle as the mouse is dragged. |
| 42 | + double width = e.GetPosition(drawingCanvas).X - startPoint.X; |
| 43 | + double height = e.GetPosition(drawingCanvas).Y - startPoint.Y; |
| 44 | + if (width > 0 && height > 0) |
| 45 | + { |
| 46 | + currentRect = new Rect(startPoint.X, startPoint.Y, width, height); |
| 47 | + currentRectangle.Width = width; |
| 48 | + currentRectangle.Height = height; |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void OnMouseDown(object sender, MouseButtonEventArgs e) |
| 54 | + { |
| 55 | + startPoint = e.GetPosition(drawingCanvas); |
| 56 | + |
| 57 | + // Create a new rectangle. |
| 58 | + currentRectangle = new Rectangle |
| 59 | + { |
| 60 | + Stroke = Brushes.Black, |
| 61 | + StrokeThickness = 1, |
| 62 | + Fill = Brushes.Transparent |
| 63 | + }; |
| 64 | + |
| 65 | + // Add the rectangle to the canvas. |
| 66 | + drawingCanvas.Children.Add(currentRectangle); |
| 67 | + |
| 68 | + // Set the initial position of the rectangle. |
| 69 | + Canvas.SetLeft(currentRectangle, startPoint.X); |
| 70 | + Canvas.SetTop(currentRectangle, startPoint.Y); |
| 71 | + } |
| 72 | + |
| 73 | + private void OnMouseUp(object sender, MouseButtonEventArgs e) |
| 74 | + { |
| 75 | + viewModel.SelectedDataPoints = series.GetDataPoints(currentRect); |
| 76 | + currentRectangle = null; |
| 77 | + currentRect = Rect.Empty; |
| 78 | + drawingCanvas.Children.Clear(); |
| 79 | + } |
| 80 | + |
| 81 | + #endregion |
| 82 | + } |
| 83 | +``` |
| 84 | + |
5 | 85 | The following demo visualizes how to select multiple scatter data points using mouse drag, which shows the respective values in a list view.
|
6 | 86 | 
|
7 | 87 |
|
|
0 commit comments