question

NathanSmith-2125 avatar image
0 Votes"
NathanSmith-2125 asked DanielZhang-MSFT commented

MS Chart cursor position not linked to X data

I have an MS chart that is showing logarithmic frequency data in my c# app. I want to use a cursor to select a data point. Since the cursor doesn't automatically snap to the closest data point, I had to write a routine that would do that for me.

 private void chart_CursorPositionChanged(object sender, CursorEventArgs e)
         {
             if (double.IsNaN(e.NewPosition)) return;
    
             if (chart.ChartAreas[0].CursorX.IsUserEnabled)
             {
                 double coercedPosition = chart.Series[0].Points.Aggregate((x, y) => Math.Abs(x.XValue - e.NewPosition) < Math.Abs(y.XValue - e.NewPosition) ? x : y).XValue;
                 //move the cursor to the closest point
                 chart.ChartAreas[0].CursorX.Position = coercedPosition;
                 txtZPrime.Text = SelectedStep.GetZatFreq(coercedPosition).ToString();
             }
         }

The problem is, when the cursor is moved, and I get the position of the cursor, the position is not the position of the cursor in the X data line. Instead, it's the number of major divisions from the left of the chart.

I made sure the data is charting correctly and the labels are correct for the X axis, but the cursor position is not correlated to the X data.

Any ideas what's the problem?

Thanks

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi NathanSmith-2125,
Please try to use Axis.PixelPositionToValue(Double) method to convert an absolute pixel position along an axis to an axis value.

 chart.ChartAreas[0].CursorX.Position =chart.ChartAreas[0].AxisX.PixelPositionToValue(coercedPosition);

And note that this method only works in paint events.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This did not work. The new position was converted, but it wasn't even close to the point I selected. Again, the value I get is not a pixel position on the chart area - it's more like the index of the major grid lines where the point was selected.

In other words, if there are 7 major grid lines on the chart and I select a point half way between the third and fourth grid line, the new position will be 3.5.

I hope that explains my problem better.

Thanks

0 Votes 0 ·

Ok, I think I found what's happening. It seems to be linked to the logarithmic scale for the X axis. I think the new position is actually the power of the value on the log scale.

Is there any way to configure it to give the new position as a normal value - not the power of the value?

Thanks

0 Votes 0 ·

Hi @NathanSmith-2125,
In other words, the value you want to obtain is the value on the X axis corresponding to the mouse coordinate.
If so, please refer to Mehdi LAMRANI's answer.
Best Regards,
Daniel Zhang


0 Votes 0 ·