LiveCharts2 icon indicating copy to clipboard operation
LiveCharts2 copied to clipboard

Range Selection

Open chocobot opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. I need to be able to select/highlight a particular range on my chart using the users cursor input. Ex: User should be able to highlight a region of a histogram they are interested in.

Describe the solution you'd like Support functionality for active selection

Describe alternatives you've considered have a way to retrieve the range at the cursor location so we could add a series on the fly to represent the selection?

chocobot avatar Oct 01 '21 13:10 chocobot

As a workaround, you can customize this code to make it working:

    public partial class MainWindow : Window
    {
        private System.Drawing.PointF? LastDragPosition;
        private bool started;
        private MainWindowViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
            vm = new MainWindowViewModel();
            this.DataContext = vm;

            liveChart.MouseDown += LiveChart_MouseDown;
            liveChart.MouseMove += LiveChart_MouseMove;
            liveChart.MouseUp += LiveChart_MouseUp;
        }

        private void LiveChart_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
            started = false;
        }

        private void LiveChart_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!started)
            {
                return;
            }


            var p = e.GetPosition((CartesianChart)sender);
            var chartPos = liveChart.ScaleUIPoint(new LvcPoint((float)p.X, (float)p.Y));
            LastDragPosition = new PointF((float)chartPos[0], (float)chartPos[1]);
            vm.Sections[0].Yi = LastDragPosition.Value.Y - 0.5;
            vm.Sections[0].Yj = LastDragPosition.Value.Y + 0.5;
        }

        private void LiveChart_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Mouse.OverrideCursor = System.Windows.Input.Cursors.Hand;
            started = true;
        }
    }
}

It is some basic code allowing user to move Section on Y axis. You can change it so that it creates section on the fly or you can create additional serie on the fly (as you described) using method FindPointsNearTo() available on you main serie

CodeWithMichal avatar Oct 03 '21 10:10 CodeWithMichal

There is now the ScalePixelsToData() function in all the charts, this way you can use the pointer events of the charts and scale the location to the chart scale:

https://github.com/beto-rodriguez/LiveCharts2/blob/master/samples/WPFSample/General/Scrollable/View.xaml.cs#L51

Finally you could add a section to highlight the region:

https://github.com/beto-rodriguez/LiveCharts2/blob/master/samples/WPFSample/General/Sections/View.xaml https://github.com/beto-rodriguez/LiveCharts2/blob/master/samples/ViewModelsSamples/General/Sections/ViewModel.cs

beto-rodriguez avatar Jul 09 '23 20:07 beto-rodriguez