Page 1 of 1

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

Posted: Thu Apr 25, 2019 12:44 pm
by coldsun1982
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.

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

Posted: Fri Apr 26, 2019 8:20 am
by Arction_LasseP
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.