How to get the location when using mouse wheel in chart?

A forum dedicated to WPF version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
coldsun1982
Posts: 12
Joined: Thu Feb 28, 2019 1:57 am

How to get the location when using mouse wheel in chart?

Post by coldsun1982 » Thu Apr 25, 2019 12:44 pm

Hi, When I use mouse wheel in chart to zoom in or zoom out, how could I get the location of my mouse movement? I creatchart such as, and add two event:sb_Scroll & ExampleThreadFedScrollBar_RangeChanged, can I get the location of mouse according to the property of e?

Code: Select all

            HorizontalScrollBar sb = new HorizontalScrollBar(chart1);
            sb.Minimum = 0;
            sb.Maximum = (ulong)xLength1;
            sb.Scroll += sb_Scroll;
            sb.LargeChange = sb.Maximum;
            sb.Offset = new PointIntXY(sb, 0, 40);
            chart1.HorizontalScrollBars.Add(sb);
            chart1.ViewXY.XAxes[0].RangeChanged += ExampleThreadFedScrollBar_RangeChanged;

        private void sb_Scroll(Object sender, Arction.Wpf.Charting.ScrollEventArgs e)
        {...}
        private void ExampleThreadFedScrollBar_RangeChanged(Object sender, RangeChangedEventArgs e)
        {...}
My purpose is: if I get the location of mouse, I can set the HorizontalScrollBar's value = the location mouse, my xAxis.ValueType = AxisValueType.DateTime, I can't set correctly HorizontalScrollBar's value as your code sample "Scroll bars" or "Histortic data review". I add 500 points in chart, my operations is:

Code: Select all

double xmin = chart1.ViewXY.XAxes[0].DateTimeToAxisValue(fist_time);
double xmax = chart1.ViewXY.XAxes[0].DateTimeToAxisValue(last_time);

_xLength = xmax - xmin;
_scrollPosition = 0;

AxisX xAxis = chart1.ViewXY.XAxes[0];
xAxis.ValueType = AxisValueType.DateTime;
xAxis.ScrollMode = XAxisScrollMode.None;
xAxis.LabelsTimeFormat = "MM-dd HH:mm:ss";
chart1.ViewXY.XAxes[0].SetRange(xmin, xmax);

HorizontalScrollBar sb = new HorizontalScrollBar(chart1);
sb.Minimum = 0;
sb.Maximum = (ulong)(xmax-xmin);
sb.Scroll += sb_Scroll;
sb.LargeChange = sb.Maximum;
chart1.HorizontalScrollBars.Add(sb);
chart1.ViewXY.XAxes[0].RangeChanged += ExampleThreadFedScrollBar_RangeChanged;

private void ExampleThreadFedScrollBar_RangeChanged(Object sender, RangeChangedEventArgs e)
{
    double d = e.NewMax - e.NewMin;
    if (d < _xLength)
    {
	// Zooming in.
	_xLength = d;
	chart1.HorizontalScrollBars[0].Value = (ulong)(e.NewMin);
     }
     else if (d > _xLength)
     {
        // Zooming out.
        _xLength = d;
        chart1.HorizontalScrollBars[0].Value = _scrollPosition;
      }
}

private void sb_Scroll(Object sender, Arction.Wpf.Charting.ScrollEventArgs e)
{
     _scrollPosition = e.NewValue;
     chart1.BeginUpdate();
     chart1.ViewXY.XAxes[0].SetRange((double)e.NewValue + xmin, (double)e.NewValue + _xLength1 + xmin);
     chart1.HorizontalScrollBars[0].Value = scrollPosition;
     chart1.EndUpdate();
}
when I zoom in or out, the HorizontalScrollBars locates uncorrectly. It appears far left or far right. Not as "Scroll bars" , the bar have a gap to far both side.
Attachments
It is my project
It is my project
project.png (3.38 KiB) Viewed 4364 times
It is your code example &quot;Scroll bars&quot;
It is your code example "Scroll bars"
example.png (4.63 KiB) Viewed 4364 times

Arction_LasseP
Posts: 141
Joined: Wed Mar 27, 2019 1:05 pm

Re: How to get the location when using mouse wheel in chart?

Post by Arction_LasseP » Fri Apr 26, 2019 8:20 am

Hello,

There is e.GetPosition(_chart), which gets the current mouse position. However, this is only usable in events that are based on mouse actions (e is MouseEventArgs or MouseButtonEventArgs). Scroll and RangeChanged are different kinds of events and thus don't have this option.

To get the mouse position inside these events (or anywhere in code) you can use Mouse.GetPosition(_chart), which gives the mouse position coordinates relative to the top-left corner of the chart.

Hope this is helpful.

Post Reply